Sign in to follow this  
Followers 0
Appie78

Random direction

4 posts in this topic

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!!!

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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

Share this post


Link to post
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!!!

Share this post


Link to post
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
Sign in to follow this  
Followers 0