66 lines
1.6 KiB
Plaintext
66 lines
1.6 KiB
Plaintext
' version 16-10-2016
|
|
' compile with: fbc -s gui
|
|
|
|
' a cell size of 4 x 4 pixels is used
|
|
' In FreeBASIC the 0,0 is the top left corner
|
|
|
|
ScreenRes 400,400,8 ' give a 100 by 100 field
|
|
Dim As UByte Ptr p = ScreenPtr
|
|
If p = 0 Then End ' p does not point to screen
|
|
|
|
Palette 0, 0, 0, 0 ' index 0 = black
|
|
Palette 255, 255, 255, 255 ' index 225 = white
|
|
|
|
Line (0, 0) - (799, 799), 255, bf ' draw box and fill it with white color
|
|
|
|
Dim As Integer count, offset, x = 199, y = 199
|
|
Dim As UByte col ' = color
|
|
' direction, 0 = up, 1 = right, 2 = down, 3 = left
|
|
Dim As UByte d ' d = 0, looking up
|
|
|
|
Do
|
|
offset = x + y * 400
|
|
col = p[offset]
|
|
|
|
If col = 0 Then
|
|
d = (d -1) And 3
|
|
Else
|
|
d = (d +1) And 3
|
|
EndIf
|
|
|
|
col = col Xor 255 ' flip the color
|
|
|
|
ScreenLock ' don't update screen while we are drawing
|
|
|
|
' draw a 4*4 block and paint it with palette color [0 | 255]
|
|
Line (x, y) - (x +3, y -3), col, bf
|
|
|
|
ScreenUnLock ' allow screen update's
|
|
|
|
'Sleep 100 ' slow the program down if needed
|
|
|
|
' true = 0, false = -1
|
|
If (d And 1) = 1 Then
|
|
x = x + (d = 1) * 4 - (d = 3) * 4
|
|
Else
|
|
y = y - (d = 0) * 4 + (d = 2) * 4
|
|
End If
|
|
|
|
count += 1
|
|
' update step count window title bar
|
|
WindowTitle "Langton's ant step: " + Str(count)
|
|
|
|
' has user clicked on close window "X" then end program
|
|
If InKey = Chr(255) + "k" Then End
|
|
|
|
Loop Until x < 1 Or x > 398 Or y < 1 Or y > 398
|
|
|
|
' display total count in window title bar
|
|
WindowTitle "Langton's ant has left the field in " + Str(count) + " steps"
|
|
|
|
' empty keyboard buffer
|
|
While InKey <> "" : Wend
|
|
'Print : Print "hit any key to end program"
|
|
Sleep
|
|
End
|