Langton's ant: Difference between revisions

D version
No edit summary
(D version)
Line 84:
return 0;
}</lang>
 
=={{header|D}}==
A basic version.
{{trans|Processing}}
<lang D>import std.stdio, std.algorithm;
 
enum Direction { up, right, down, left }
enum Turn : bool { left = false, right = true }
enum Color : char { white = '.', black = '#' }
 
void main() {
enum width = 75, height = 52;
enum nsteps = 12_000;
 
auto M = new Color[][](height, width);
int x = width / 2;
int y = height / 2;
Direction dir = Direction.up;
 
for (int i = 0; i < nsteps &&
x >= 0 && y >= 0 && x < width && y < height; i++) {
Turn turn = M[y][x] == Color.black ? Turn.left : Turn.right;
M[y][x] = M[y][x] == Color.black ? Color.white : Color.black;
 
dir = cast(Direction)((4 + dir + (turn ? +1 : -1)) % 4);
final switch(dir) {
case Direction.up: y--; break;
case Direction.right: x--; break;
case Direction.down: y++; break;
case Direction.left: x++; break;
}
}
 
foreach (row; M)
writeln(cast(char[])row);
}</lang>
Output:
<pre>...........................................................................
...........................................................................
...........................................................................
...........................................................................
..........................##..############..##.............................
.........................##..#..........####..#............................
........................#.##............##...###...........................
........................#....#..#.........#..#.#...........................
......................#.......###.........#.#.##..##.......................
......................###..##.##.....#.....#...#..#.###....................
..................##..##.#..#.#...##.####.##..###..#.#.....................
.................###...###.....#.#.###..##.#..##.###.#.....................
................#.#.#.###...#..####..#.#.#####...#.....#...................
................#...#.###.#.######.##.##..####.#...##.###..................
.................##.###.#####...#.##.##.##.#.#.##.#.###.#..................
...............##.#....####..#.#...#...###.##.#...#.#......................
..............##.....#.##.....##..#...##.##.........#..#...................
.............#.##..##.###...#.....##..#..###.##.#.#...###..................
.............#...####.##..#....#..###...##.##...##..###..#.................
..............#.....#..###.##.#..##.####.#.#..#.#...#...###................
............##..#..#.##.###......#..###.#..#....##.#..###..#...............
...........#...##.####..####.#####..##..##.#.##.#.....#...###..............
..........#...#....#.#.#...##......##.#.#.###.#..#.#.#..###..#.............
..........#......#...####.####.......##...#.##..###.##..#...###............
...........#....#....#..####..#.###########..##...#..#.#..###..#...........
...........##..#....##..#..#########..##..####.#......##..#...###..........
..........#.####..##.#..#...###.###.##.##...##.#..##...#.#..###..#.........
..........#...##...###.###....#.#.##.#.##.######.#..#...##..#...###........
...........#...##....#.##..#.#.....#####.#.#####.....#...#.#..###..#.......
...........#..#..#.##..#..#...#.#..##.#####.##.#.....#....##..#...###......
...........##...##########...##.#####..#.####...#....#.....#.#..###..#.....
...............##.####.##...#..####...#..#...##...##.#......##..#...###....
.............#.#..#..#..#.#....#...#.##...##..#.#####........#.#..###..#...
.............##..##..#..##.#.#.##.##....#.#.#.##..##..........##..#...###..
.............#.####..##.#.#.########.#....#..#.................#.#..###..#.
.............#.##..#..#...##.##.......#...#..#..................##..#...###
.............####.##...##..##..#......#..#..#....................#.#..##...
............###.#...#....##..##.......#...##......................##..#..##
............####.###.####....####..##.#............................#.#.#.#.
...........#..#.#.##.#..##....####..##..............................##.####
..........#####.##.###.##....##....##................................#.##.#
..........####..#.##.#................................................####.
..........##.##.##.....................................................##..
................##.........................................................
........#.####..##.#.......................................................
........###..###.#..#......................................................
.........#..#..#.##.#......................................................
..........##......##.......................................................
.................##........................................................
...........................................................................
...........................................................................
...........................................................................</pre>
 
=={{header|Processing}}==
Anonymous user