Langton's ant: Difference between revisions
Content added Content deleted
(Add Seed7 example) |
(Updated first D entry) |
||
Line 1,296: | Line 1,296: | ||
=={{header|D}}== |
=={{header|D}}== |
||
===Textual Version=== |
===Textual Version=== |
||
<lang d>void main() { |
<lang d>void main() @safe { |
||
import std.stdio, std.traits; |
import std.stdio, std.traits; |
||
enum width = 75, height = 52; |
enum width = 75, height = 52; |
||
enum maxSteps = 12_000; |
enum maxSteps = 12_000; |
||
enum Direction { up, right, down, left } |
enum Direction { up, right, down, left } |
||
enum Color : char { white = '.', black = '#' } |
enum Color : char { white = '.', black = '#' } |
||
uint x = width / 2, y = height / 2; |
uint x = width / 2, y = height / 2; |
||
auto M = new Color[][](height, width); |
auto M = new Color[][](height, width); |
||
auto dir = Direction.up; |
auto dir = Direction.up; |
||
with (Color) |
with (Color) |
||
for (int i = 0; i < maxSteps && x < width && y < height; i++) { |
for (int i = 0; i < maxSteps && x < width && y < height; i++) { |
||
immutable turn = M[y][x] == black; |
immutable turn = M[y][x] == black; |
||
dir = [EnumMembers!Direction][(dir + (turn ? 1 : -1)) & 3]; |
dir = [EnumMembers!Direction][(dir + (turn ? 1 : -1)) & 3]; |
||
M[y][x] = (M[y][x] == black) ? white : black; |
M[y][x] = (M[y][x] == black) ? white : black; |
||
final switch(dir) with (Direction) { |
final switch(dir) with (Direction) { |
||
case up: y--; break; |
case up: y--; break; |
||
case right: x--; break; |
case right: x--; break; |
||
case down: y++; break; |
case down: y++; break; |
||
case left: x++; break; |
case left: x++; break; |
||
} |
} |
||
} |
} |
||
writefln("%(%-(%c%)\n%)", M); |
writefln("%(%-(%c%)\n%)", M); |
||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |