Jump to content

Random direction


Appie78
 Share

Recommended Posts

@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 by FireFox
Link to comment
Share on other sites

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

Here 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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...