Langton's ant: Difference between revisions

Line 173:
...........................................................................
...........................................................................</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>link graphics,printf
 
procedure main(A)
e := ( 0 < integer(\A[1])) | 100 # 100 or whole number from command line
LangtonsAnt(e)
end
 
record antrec(x,y,nesw)
 
procedure LangtonsAnt(e)
size := sprintf("size=%d,%d",e,e)
label := sprintf("Langton's Ant %dx%d [%d]",e,e,0)
&window := open(label,"g","bg=white",size) |
stop("Unable to open window")
 
ant := antrec(e/2,e/2,?4%4)
board := list(e)
every !board := list(e,"w")
k := 0
repeat {
k +:= 1
WAttrib("fg=red")
DrawPoint(ant.x,ant.y)
cell := board[ant.x,ant.y]
if cell == "w" then { # white cell
WAttrib("fg=black")
ant.nesw := (ant.nesw + 1) % 4 # . turn right
}
else { # black cell
WAttrib( "fg=white")
ant.nesw := (ant.nesw + 3) % 4 # . turn left = 3 x right
}
board[ant.x,ant.y] := map(cell,"wb","bw") # flip colour
DrawPoint(ant.x,ant.y)
case ant.nesw of { # go
0: ant.y -:= 1 # . north
1: ant.x +:= 1 # . east
2: ant.y +:= 1 # . south
3: ant.x -:= 1 # . west
}
if 0 < ant.x <= e & 0 < ant.y <= e then next
else break
}
label := sprintf("label=Langton's Ant %dx%d [%d]",e,e,k)
WAttrib(label)
WDone()
printf("Langton's Ant exited the field after %d rounds.\n",k)
end</lang>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
[http://www.cs.arizona.edu/icon/library/src/procs/graphics.icn graphics.icn provides graphics support (WDone)]
 
=={{header|Processing}}==
Line 258 ⟶ 316:
set(x,y,white?#ffffff:#000000);
}</lang>
 
=={{header|Python}}==
{{trans|D}}
Anonymous user