Appie78 Posted February 23, 2009 Share Posted February 23, 2009 Hi All, Is it possible to use the Random() function to give a direction(left, right, up and down) without the next direction being the opposite? Thanks Electron microscopes rule!!! Link to comment Share on other sites More sharing options...
FireFox Posted February 23, 2009 Share Posted February 23, 2009 (edited) @pokerface Just assign numbers to your directions... Local $left = 1, $up = 2, $right = 3, $down = 4 Local $lastrdm = 5 While 1 Sleep(250) $rdm = Random(1, 4, 1) If $rdm = $up And $lastrdm <> $down Then Send('{UP}') $lastrdm = $up ElseIf $rdm = $down And $lastrdm <> $up Then Send('{DOWN}') $lastrdm = $down ElseIf $rdm = $left And $lastrdm <> $right Then Send('{LEFT}') $lastrdm = $left ElseIf $rdm = $right And $lastrdm <> $left Then Send('{RIGHT}') $lastrdm = $right EndIf WEnd Cheers, FireFox. Edited February 23, 2009 by FireFox Link to comment Share on other sites More sharing options...
Malkey Posted February 23, 2009 Share Posted February 23, 2009 Hi All, Is it possible to use the Random() function to give a direction(left, right, up and down) without the next direction being the opposite? ThanksHere is another attempt. Local $CurrentDirect = "left" Local $NewDirect $NewDirect = 'Current direction is "' & $CurrentDirect & '"' & @CRLF & @CRLF & _ " Random directions follow:- " & @CRLF For $y = 1 To 20 $NewDirect &= @tab & RandDirect($CurrentDirect) & @CRLF Next MsgBox(0, "", $NewDirect) ; Returns random direction but NOT opposite direction. Func RandDirect($Direct) Local $dir[5][2] = [["", 0],["left", 180],["right", 0],["up", 90],["down", 270]] Local $CurrIndex, $ranDir For $x = 1 To 4 If $dir[$x][0] = $Direct Then $CurrIndex = $x Next Do $ranDir = Random(1, 4, 1) Until Abs($dir[$ranDir][1] - $dir[$CurrIndex][1]) <> 180 Return $dir[$ranDir][0] EndFunc ;==>RandDirect Link to comment Share on other sites More sharing options...
Appie78 Posted February 24, 2009 Author Share Posted February 24, 2009 Thanks guys, I tried both of the suggestions but both had the same problem that the directions where wrong! Now I've got this bit of code figured out: If $LastX = 4 Then $Direction = Random(0,3,1) ElseIf $LastX = 0 Then $Direction = Random(5,7,1) If $Direction = 5 Then $Direction = 0 ElseIf $Direction = 6 Then $direction = 2 ElseIf $direction = 7 Then $direction = 3 EndIf ElseIf $LastX = 1 Then $direction = Random(1,3,1) ElseIf $LastX = 2 Then $direction = Random(0,2,1) ElseIf $LastX = 3 Then $direction = Random(8,10,1) If $direction = 8 Then $direction = 0 ElseIf $direction = 9 Then $direction = 1 ElseIf $direction = 10 Then $direction = 3 EndIf EndIf It's a bit meesy but it works like a charm!! Thanks, Pokerface Electron microscopes rule!!! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now