Langton's ant: Difference between revisions
Content added Content deleted
Line 2,555: | Line 2,555: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{incorrect|Scala|The chirality of the output is reversed because the code toggles the color of the ant's location *after* moving instead of *before* moving.}} |
|||
<lang scala>class Langton(matrix:Array[Array[Char]], ant:Ant) { |
<lang scala>class Langton(matrix:Array[Array[Char]], ant:Ant) { |
||
import Langton._ |
import Langton._ |
||
Line 2,561: | Line 2,560: | ||
val cols=matrix(0).size |
val cols=matrix(0).size |
||
def isValid = 0 <= ant. |
def isValid = 0 <= ant.row && ant.row < cols && 0 <= ant.col && ant.col < rows |
||
def isBlack=matrix(ant. |
def isBlack=matrix(ant.row)(ant.col)==BLACK |
||
def changeColor(c:Char)={matrix(ant. |
def changeColor(c:Char)={matrix(ant.row)(ant.col)=c; matrix} |
||
def evolve():Langton={ |
def evolve():Langton={ |
||
val (newCol, newAnt)=if(isBlack) (WHITE, ant.turnLeft |
val (newCol, newAnt)=if(isBlack) (WHITE, ant.turnLeft) else (BLACK, ant.turnRight) |
||
new Langton(changeColor(newCol), newAnt) |
new Langton(changeColor(newCol), newAnt.move) |
||
} |
} |
||
override def toString()=matrix map (_.mkString("")) mkString "\n" |
override def toString()=matrix map (_.mkString("")) mkString "\n" |
||
} |
} |
||
case class Ant( |
case class Ant(row:Int, col:Int, d:Int=0) { |
||
def turnLeft=Ant( |
def turnLeft=Ant(row,col,(d-1)&3) |
||
def turnRight=Ant( |
def turnRight=Ant(row,col,(d+1)&3) |
||
def move=d match { |
def move=d match { |
||
case 0 => Ant( |
case 0 => Ant(row-1,col,d) // north |
||
case 1 => Ant( |
case 1 => Ant(row,col+1,d) // east |
||
case 2 => Ant( |
case 2 => Ant(row+1,col,d) // south |
||
case 3 => Ant( |
case 3 => Ant(row,col-1,d) // west |
||
} |
} |
||
} |
} |
||
Line 2,602: | Line 2,600: | ||
Output: |
Output: |
||
<pre style="height: 40ex; overflow: scroll">Out of bounds after 11669 moves |
<pre style="height: 40ex; overflow: scroll">Out of bounds after 11669 moves |
||
........................................................................ |
.................................................................................................... |
||
....................................................................... |
.................................................................................................... |
||
...................................................................... |
.................................................................................................... |
||
..................................................................... |
.................................................................................................... |
||
.................................................................... |
.................................................................................................... |
||
................................................................... |
.................................................................................................... |
||
.................................................................. |
.................................................................................................... |
||
................................................................. |
.................................................................................................... |
||
................................................................ |
.................................................................................................... |
||
............................................................... |
.................................................................................................... |
||
.............................................................. |
.................................................................................................... |
||
............................................................. |
.................................................................................................... |
||
............................................................ |
.................................................................................................... |
||
........................................................... |
.................................................................................................... |
||
.......................................................... |
.................................................................................................... |
||
......................................................... |
.................................................................................................... |
||
........................................................ |
.................................................................................................... |
||
....................................................... |
.................................................................................................... |
||
...................................................... |
.................................................................................................... |
||
..................................................... |
.................................................................................................... |
||
.................................................... |
.................................................................................................... |
||
................................................... |
.................................................................................................... |
||
.................................................. |
.................................................................................................... |
||
................................................. |
.................................................................................................... |
||
................................................ |
.................................................................................................... |
||
............................................... |
.................................................................................................... |
||
.............................................. |
.................................................................................................... |
||
............................................. |
.................................................................................................... |
||
..........................................##..############..##...................................... |
|||
...........................................# |
.........................................#..####..........#..##..................................... |
||
........................................###...##............##.#.................................... |
|||
..................................... |
........................................#.#..#.........#..#....#.................................... |
||
....................................##..## |
....................................##..##.#.#.........###.......#.................................. |
||
.................................# |
.................................###.#..#...#.....#.....##.##..###.................................. |
||
................................ |
..................................#.#..###..##.####.##...#.#..#.##..##.............................. |
||
.................................# |
..................................#.###.##..#.##..###.#.#.....###...###............................. |
||
................................#. |
................................#.....#...#####.#.#..####..#...###.#.#.#............................ |
||
...............................###.##...#.####..##.##.######.#.###.#...#............................ |
|||
................................. |
...............................#.###.#.##.#.#.##.##.##.#...#####.###.##............................. |
||
..................................#...# |
...................................#.#...#.##.###...#...#.#..####....#.##........................... |
||
.............................. |
................................#..#.........##.##...#..##.....##.#.....##.......................... |
||
............................. |
...............................###...#.#.##.###..#..##.....#...###.##..##.#......................... |
||
............................ |
..............................#..###..##...##.##...###..#....#..##.####...#......................... |
||
............................#...#... |
.............................###...#...#.#..#.#.####.##..#.##.###..#.....#.......................... |
||
............................#..###..#.##....#..#.###..#......###.##.#..#..##........................ |
|||
.............................#.##.#.##..## |
...........................###...#.....#.##.#.##..##..#####.####..####.##...#....................... |
||
............................###.. |
..........................#..###..#.#.#..#.###.#.#.##......##...#.#.#....#...#...................... |
||
.........................###...#..##.###..##.#...##.......####.####...#......#...................... |
|||
............................#..... |
........................#..###..#.#..#...##..###########.#..####..#....#....#....................... |
||
............................#.... |
.......................###...#..##......#.####..##..#########..#..##....#..##....................... |
||
.......................... |
......................#..###..#.#...##..#.##...##.##.###.###...#..#.##..####.#...................... |
||
........................ |
.....................###...#..##...#..#.######.##.#.##.#.#....###.###...##...#...................... |
||
............................#.....## |
....................#..###..#.#...#.....#####.#.#####.....#.#..##.#....##...#....................... |
||
............................#.....#.# |
...................###...#..##....#.....#.##.#####.##..#.#...#..#..##.#..#..#....................... |
||
............................#......###. |
..................#..###..#.#.....#....#...####.#..#####.##...##########...##....................... |
||
............................# |
.................###...#..##......#.##...##...#..#...####..#...##.####.##........................... |
||
.......................... |
................#..###..#.#........#####.#..##...##.#...#....#.#..#..#..#.#......................... |
||
............................ |
...............###...#..##..........##..##.#.#.#....##.##.#.#.##..#..##..##......................... |
||
............................. |
..............#..###..#.#.................#..#....#.########.#.#.##..####.#......................... |
||
................................. |
.............###...#..##..................#..#...#.......##.##...#..#..##.#......................... |
||
............................ |
............#..###..#.#....................#..#..#......#..##..##...##.####......................... |
||
............................ |
...........###...#..##......................##...#.......##..##....#...#.###........................ |
||
............................. |
..........#..###..#.#............................#.##..####....####.###.####........................ |
||
.............................. |
.........###...#..##..............................##..####....##..#.##.#.#..#....................... |
||
................................. |
........#..###..#.#................................##....##....##.###.##.#####...................... |
||
................................ |
.......###...#..##................................................#.##.#..####...................... |
||
...................................... |
......#..###..#.#.....................................................##.##.##...................... |
||
.................................... |
.....###...#..##......................................................##............................ |
||
.................................. |
....#..###..#.#.....................................................#.##..####.#.................... |
||
.................................. |
...###...#..##.....................................................#..#.###..###.................... |
||
................................... |
..#..###..#.#......................................................#.##.#..#..#..................... |
||
.................................... |
.###...#..##........................................................##......##...................... |
||
....................................... |
#..###..#.#..........................................................##............................. |
||
........................................ |
.###.#..##.......................................................................................... |
||
......................................... |
#.#.#.#.#........................................................................................... |
||
............................................ |
.####.##............................................................................................ |
||
............................................. |
.#.##.#............................................................................................. |
||
.............................................. |
..####.............................................................................................. |
||
..................................................................... |
...##............................................................................................... |
||
.................................................................... |
.................................................................................................... |
||
.................................................................................................... |
.................................................................................................... |
||
.................................................................................................... |
.................................................................................................... |