Langton's ant: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Simple Version) |
m (Added Sidef language) |
||
Line 3,925: | Line 3,925: | ||
.................................................................................................... |
.................................................................................................... |
||
....................................................................................................</pre> |
....................................................................................................</pre> |
||
=={{header|Sidef}}== |
|||
{{trans|Perl 6}} |
|||
<lang ruby># Using screen coordinates - 0,0 in upper-left, +X right, +Y down - |
|||
# these directions (right, up, left, down) are counterclockwise |
|||
# so advance through the array to turn left, retreat to turn right |
|||
const dirs = [[1,0], [0,-1], [-1,0], [0,1]]; |
|||
const size = 100; |
|||
enum |White, Black|; |
|||
# initialize the plane with white values |
|||
var plane = []; |
|||
{|i| plane[i-1] = [White]*size} * size; |
|||
# start out in approximate middle |
|||
[size/2->to_i]*2 » (\var x, \var y); |
|||
# point in a random direction |
|||
var dir = dirs.len.rand.int; |
|||
# initialize the moves counter |
|||
var moves = 0; |
|||
while true { |
|||
(x >= 0) && (y >= 0) && (x < size) && (y < size) || break; |
|||
given(plane[x][y]) |
|||
when (White) do { |
|||
dir--; plane[x][y] = Black; |
|||
} |
|||
when (Black) do { |
|||
dir++; plane[x][y] = White; |
|||
} |
|||
end; |
|||
moves++; |
|||
[\x, \y]:dirs[dir %= dirs.len] each {|a,b| *a += b }; |
|||
} |
|||
say "Out of bounds after #{moves} moves at (#{x}, #{y})"; |
|||
plane.map{.map {|square| square == Black ? '#' : '.' }}.each{.join.say};</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |