Sign in to follow this  
Followers 0
Bowmore

Langton's Ant

1 post in this topic

This script is an implementation of Langton's Ant.

I was fascinated watching the behavior generated from 2 simple rules.

The ant seems to move randomly but then heads in a straight line.

Global $aAntLocation[2]
Global $sAntDirection = 'E'
Global $iAntColour = 0x00000
Global $iForeColour = 0x000000
Global $iBackColour = 0x0000FF

$aAntLocation[0] = @DesktopWidth / 2
$aAntLocation[1] = @DesktopHeight / 2

While 1
    _KeepOnDesktop($aAntLocation)
    If $iAntColour = $iForeColour Then
        _PixelSetColor($aAntLocation[0], $aAntLocation[1], $iBackColour)
        Switch $sAntDirection
            Case 'N'
                $aAntLocation[0] -= 1
                $sAntDirection = 'W'
            Case 'E'
                $aAntLocation[1] -= 1
                $sAntDirection = 'N'
            Case 'S'
                $aAntLocation[0] += 1
                $sAntDirection = 'E'
            Case 'W'
                $aAntLocation[1] += 1
                $sAntDirection = 'S'
        EndSwitch
    Else
        _PixelSetColor($aAntLocation[0], $aAntLocation[1], $iForeColour)
        Switch $sAntDirection
            Case 'N'
                $aAntLocation[0] += 1
                $sAntDirection = 'E'
            Case 'E'
                $aAntLocation[1] += 1
                $sAntDirection = 'S'
            Case 'S'
                $aAntLocation[0] -= 1
                $sAntDirection = 'W'
            Case 'W'
                $aAntLocation[1] -= 1
                $sAntDirection = 'N'
        EndSwitch
    EndIf
    $iAntColour = PixelGetColor($aAntLocation[0], $aAntLocation[1])
;~  Sleep(10)
WEnd


Func _KeepOnDesktop(ByRef $aAntLocation)
    If $aAntLocation[0] = 0 Then
        $aAntLocation[0] = 10
    ElseIf $aAntLocation[0] = @DesktopWidth Then
        $aAntLocation[0] = @DesktopWidth - 10
    EndIf

    If $aAntLocation[1] = 0 Then
        $aAntLocation[1] = 10
    ElseIf $aAntLocation[1] = @DesktopHeight Then
        $aAntLocation[1] = @DesktopHeight - 10
    EndIf
EndFunc   ;==>_KeepOnDesktop

Func _PixelSetColor($XCoord, $YCoord, $Color)
    Local $dc = DllCall("user32.dll", "int", "GetDC", "hwnd", 0)
    If Not IsArray($dc) Then Return -1
    DllCall("gdi32.dll", "long", "SetPixel", "long", $dc[0], "long", $XCoord, "long", $YCoord, "long", _
            "0x" & StringRegExpReplace(Hex($Color, 6), "(..)(..)(..)", "\3\2\1")) ; Change to 0xBBGGRR hex colour format.
    DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "hwnd", $dc[0])
EndFunc   ;==>_PixelSetColor

See here https://secure.wikimedia.org/wikipedia/en/wiki/Langton's_ant for an explanation


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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