Langton's ant: Difference between revisions

Simpler D version
(→‎{{header|C Sharp|C#}}: Output was double spaced - fixed and eliminated blank lines at top of output.)
(Simpler D version)
Line 391:
=={{header|D}}==
A basic textual version.
<lang Dd>import std.stdio, std.algorithm, std.traits, std.string;
 
enum Direction { up, right, down, left }
enum Turn : bool { left = false, right = true }
enum Color : char { white = '.', black = '#' }
 
Line 400 ⟶ 399:
enum width = 75, height = 52;
enum nsteps = 12_000;
intuint x = width / 2, y = height / 2;
 
auto M = new Color[][](height, width);
int x = width / 2;
int y = height / 2;
auto dir = Direction.up;
 
for (int i = 0; i < nsteps && x < width && y < height; i++) {
immutable turn = M[y][x] == Color.black;
x >= 0 && y >= 0 && x < width && y < height; i++) {
immutable turndir = M[yEnumMembers!Direction][x](dir ==+ Color.black(turn ? Turn.left1 : Turn.right-1)) & 3];
M[y][x] = (M[y][x] == Color.black) ? Color.white : Color.black;
 
immutable d = (4 + dir + (turn == Turn.right ? 1 : -1)) % 4;
dir = [EnumMembers!Direction][d];
final switch(dir) {
case Direction.up: y--; break;
case Direction.right: x--++; break;
case Direction.down: y++; break;
case Direction.left: x++--; break;
}
}
 
writeln(join(cast(char[][])M, "\n")row);
foreach (row; M)
writeln(cast(char[])row);
}</lang>
Output:
Line 477 ⟶ 470:
...........................................................................
...........................................................................</pre>
 
=={{header|Go}}==
[[file:GoAnt.png|right|thumb|Output png]]
Anonymous user