Langton's ant: Difference between revisions

Line 2,555:
 
=={{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) {
import Langton._
Line 2,561 ⟶ 2,560:
val cols=matrix(0).size
def isValid = 0 <= ant.xrow && ant.xrow < cols && 0 <= ant.ycol && ant.ycol < rows
def isBlack=matrix(ant.xrow)(ant.ycol)==BLACK
def changeColor(c:Char)={matrix(ant.xrow)(ant.ycol)=c; matrix}
def evolve():Langton={
val (newCol, newAnt)=if(isBlack) (WHITE, ant.turnLeft.move) else (BLACK, ant.turnRight.move)
new Langton(changeColor(newCol), newAnt.move)
}
override def toString()=matrix map (_.mkString("")) mkString "\n"
}
 
case class Ant(xrow:Int, ycol:Int, d:Int=0) {
def turnLeft=Ant(xrow,ycol,(d-1)&3)
def turnRight=Ant(xrow,ycol,(d+1)&3)
def move=d match {
case 0 => Ant(x,yrow-1,col,d) // north
case 1 => Ant(xrow,col+1,y,d) // east
case 2 => Ant(x,yrow+1,col,d) // south
case 3 => Ant(xrow,col-1,y,d) // west
}
}
Line 2,602 ⟶ 2,600:
Output:
<pre style="height: 40ex; overflow: scroll">Out of bounds after 11669 moves
........................................................................#.#...........................
.......................................................................#.#.##...........................
......................................................................##.###.#............................
.....................................................................#.###.####.............................
....................................................................##..#.#####.............................
...................................................................#.##.##...#............................
..................................................................##..#...###.............................
.................................................................#.##.##...#..............................
................................................................##..#...###...............................
...............................................................#.##.##...#................................
..............................................................##..#...###.................................
.............................................................#.##.##...#..................................
............................................................##..#...###...................................
...........................................................#.##.##...#....................................
..........................................................##..#...###.....................................
.........................................................#.##.##...#......................................
........................................................##..#...###.......................................
.......................................................#.##.##...#........................................
......................................................##..#...###.........................................
.....................................................#.##.##...#..........................................
....................................................##..#...###...........................................
...................................................#.##.##...#............................................
..................................................##..#...###.............................................
.................................................#.##.##...#..............................................
................................................##..#...###...............................................
...............................................#.##.##...#................................................
..............................................##..#...###.................................................
.............................................#.##.##...#..................................................
............................................##..#...###########.......##......................................
.........................................#..#.##.##...#.......#..##.....................................
..........................................##..#...###............##.#....................................
.....................................##...#.##.#.#...#......#..#....#....................................
....................................##..##..#...###.........###.......#..................................
.................................#...##..##.#.#...#.....#.....##.##.....###..................................
.................................###..#...#...###..##.####.##...#.#..#.##......##..............................
..................................#...####.##..#.##..###.#....#.....###.........###.............................
................................#.##.#...#...#.####..#.#....####..#...###.#.#.......#............................
................................##.#..##.#..#.####..##.##.######..#.###.#.............#............................
...............................#.###..#.##.#...#.##.##.##...#...#####.###..............##.............................
...................................#.#...#..##.###.#...#...#.#..####....#..........##...........................
..............................######.#.#..#.........###.##...#..##.....##.#........##..........................
.............................##.#.###...#.#.##.##.#..##..###.....#...###.##..##......#.........................
............................#.#.#....###..##...##.#######.#..###..#....#..##.####......#.........................
.............................###...#...##.#..#...##.####.##..#.##.###..#...........#..........................
.............................#...#######.######..#.##.#.#..#..#.###..#......###.##.#..#..##........................
...........................###...#.....#.##.#.##..##....####.#.####..####.##...............#.......................
..........................#..###....##.######.#..#...###.#.#...##......##...#.#.#....#...#......................
............................###...##..##..#.###.#.##.#...##.......####.####...#.........#......................
........................#..###..#.#..#..#.##..##..#....########.#..####..#....#.............#.......................
.......................###...#..##.....#..##.##.##.##.##..##.##..#####..#..##....#......##.......................
......................#..###...#.#....###..#.#.#...###.##.###...#.#.#...#..#.##..####.........#......................
.....................###.....#..##....#..#.#....#####.##.#.##.#.#.....###.###...##..........#......................
....................#..###..#.#...#.....##.###.##...#.##.####.###....#.#..##.#....##.....#.......................
...................###...#..##....#.....#.#.#.####...#.####..##.##...#..#..##.#..#........#.......................
..................#..###..#.#.....#....#...####...#..###..##.##...##..#....#######.............##.......................
.................###...#..##......#..##...###....#..#...####..#...##.##...##.........##...........................
................#..###..#.#.......#...##.###.##.#..#...#.....####.#.#..#....#.#..#..#..#.#.........................
...............###...#..##........#...###..#.#.#..#.#....##.##.##...##.####..#..##......##.........................
..............#..###..#.#..........#.#.....#.#..#...#.#.##.#.#..####.#.#.##..####........#.........................
.............###...#..##...............##.###..#..#..##.##....#...##.##...#..#..##.......#.........................
............#..###..#.#...........#.#..#....#..#..#.####.#..#....#..##..##...##.........####.........................
...........###...#..##............###......###..#.##.##....#..#.##.#....##..##....#...#.###........................
..........#..###..#.#..............#....##..##...###..#..#.#.#..#...#.###....####.###.......####........................
.........###...#..##.........................##.#.#######.###.######.#####.#.####....##..#.##.#.#...#.......................
........#..###..#.#....................#####.#####..##...#####....#.#.##....##....##.###.##...#####......................
.......###...#..##....................###.###..##.#..#......#...##..#...........#.##.#..####......................
......#..###..#.#...........................#.#...#########.#####...####..................##.##.##......................
.....###...#..##...................................###..###.#...#.#.###.....#..#....##.............................
....#..###..#.#.........................##.....##.###...##.###...##.####..#..#..........#.##..####.#....................
...###...#..##..........................###.##..#..#....#...#####.#.##.#....###........#..#.###..###....................
..#..###..#.#.................................#..#...#....#.....##..##...#.#.###.##.#..#....#.....................
.###...#..##..............................##.#.#..##..#...#.##..####.######.............##......##......................
#..###..#.#..........................................###...#...####..##.###.#......##.............................
.###.#..##............................................#..#..#...##.#...#..#####.#..#..............................
#.#.#.#.#................................................##.#.....#.....#######.###.##..............................
.####.##..............................................#....##...#......##.##..#.#.............................
.#.##.#..................................................#..##..###........####.#..#............................
..####..................................................##..##............###.##.#............................
...##..................................................................##.............................
....................................................................##................................
....................................................................................................
....................................................................................................
Anonymous user