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.x && ant.x < cols && 0 <= ant.y && ant.y < rows
def isValid = 0 <= ant.row && ant.row < cols && 0 <= ant.col && ant.col < rows
def isBlack=matrix(ant.x)(ant.y)==BLACK
def isBlack=matrix(ant.row)(ant.col)==BLACK
def changeColor(c:Char)={matrix(ant.x)(ant.y)=c; matrix}
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.move) else (BLACK, ant.turnRight.move)
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(x:Int, y:Int, d:Int=0) {
case class Ant(row:Int, col:Int, d:Int=0) {
def turnLeft=Ant(x,y,(d-1)&3)
def turnLeft=Ant(row,col,(d-1)&3)
def turnRight=Ant(x,y,(d+1)&3)
def turnRight=Ant(row,col,(d+1)&3)
def move=d match {
def move=d match {
case 0 => Ant(x,y-1,d) // north
case 0 => Ant(row-1,col,d) // north
case 1 => Ant(x+1,y,d) // east
case 1 => Ant(row,col+1,d) // east
case 2 => Ant(x,y+1,d) // south
case 2 => Ant(row+1,col,d) // south
case 3 => Ant(x-1,y,d) // west
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
........................................................................#.#.........................
....................................................................................................
.......................................................................#.#.##.......................
....................................................................................................
......................................................................##.###.#......................
....................................................................................................
.....................................................................#.###.####.....................
....................................................................................................
....................................................................##..#.#####.....................
....................................................................................................
...................................................................#.##.##...#......................
....................................................................................................
..................................................................##..#...###.......................
....................................................................................................
.................................................................#.##.##...#........................
....................................................................................................
................................................................##..#...###.........................
....................................................................................................
...............................................................#.##.##...#..........................
....................................................................................................
..............................................................##..#...###...........................
....................................................................................................
.............................................................#.##.##...#............................
....................................................................................................
............................................................##..#...###.............................
....................................................................................................
...........................................................#.##.##...#..............................
....................................................................................................
..........................................................##..#...###...............................
....................................................................................................
.........................................................#.##.##...#................................
....................................................................................................
........................................................##..#...###.................................
....................................................................................................
.......................................................#.##.##...#..................................
....................................................................................................
......................................................##..#...###...................................
....................................................................................................
.....................................................#.##.##...#....................................
....................................................................................................
....................................................##..#...###.....................................
....................................................................................................
...................................................#.##.##...#......................................
....................................................................................................
..................................................##..#...###.......................................
....................................................................................................
.................................................#.##.##...#........................................
....................................................................................................
................................................##..#...###.........................................
....................................................................................................
...............................................#.##.##...#..........................................
....................................................................................................
..............................................##..#...###...........................................
....................................................................................................
.............................................#.##.##...#............................................
....................................................................................................
............................................##..#...###.............................................
..........................................##..############..##......................................
...........................................#.##.##...#..............................................
.........................................#..####..........#..##.....................................
..........................................##..#...###...............................................
........................................###...##............##.#....................................
.....................................##..#.##.##...#................................................
........................................#.#..#.........#..#....#....................................
....................................##..##..#...###.................................................
....................................##..##.#.#.........###.......#..................................
.................................#...##..##.##...#..................................................
.................................###.#..#...#.....#.....##.##..###..................................
.................................###..#...#...###...####............................................
..................................#.#..###..##.####.##...#.#..#.##..##..............................
.................................#...####.##...#...#....#...........................................
..................................#.###.##..#.##..###.#.#.....###...###.............................
................................#.##.#......#.#...#....###..........................................
................................#.....#...#####.#.#..####..#...###.#.#.#............................
................................##.#..##.#.....##.#....###..........................................
...............................###.##...#.####..##.##.######.#.###.#...#............................
...................................##.....#.#.##...#....#...........................................
...............................#.###.#.##.#.#.##.##.##.#...#####.###.##.............................
..................................#...#..#####.#......#.#...........................................
...................................#.#...#.##.###...#...#.#..####....#.##...........................
..............................######.##..........#####...#..........................................
................................#..#.........##.##...#..##.....##.#.....##..........................
.............................##.#.##...#.#.#.##.#..##..###..........................................
...............................###...#.#.##.###..#..##.....#...###.##..##.#.........................
............................#.##....###..#...#.#######.#..##........................................
..............................#..###..##...##.##...###..#....#..##.####...#.........................
............................#...#...##.#..#...##.######..#..#.......................................
.............................###...#...#.#..#.#.####.##..#.##.###..#.....#..........................
.............................#...#######.######..#.##.#.#....#......................................
............................#..###..#.##....#..#.###..#......###.##.#..#..##........................
.............................#.##.#.##..##....####.#.##.####.#......................................
...........................###...#.....#.##.#.##..##..#####.####..####.##...#.......................
............................###....##.######.#..#...####....#.......................................
..........................#..###..#.#.#..#.###.#.#.##......##...#.#.#....#...#......................
............................###...##..##..#.###.#.##.#...#..........................................
.........................###...#..##.###..##.#...##.......####.####...#......#......................
............................#.....#.##.##..#....#######.............................................
........................#..###..#.#..#...##..###########.#..####..#....#....#.......................
............................#.....#..##.##.##.####..##.##..####.....................................
.......................###...#..##......#.####..##..#########..#..##....#..##.......................
............................#....####.#....###.##.###...#.#....#....................................
......................#..###..#.#...##..#.##...##.##.###.###...#..#.##..####.#......................
............................#......#.#....#####.#.#.###.......###...................................
.....................###...#..##...#..#.######.##.#.##.#.#....###.###...##...#......................
............................#.....##.###.##...#.##.####.###...#.#...................................
....................#..###..#.#...#.....#####.#.#####.....#.#..##.#....##...#.......................
............................#.....#.#.#.####....####..##.##.........................................
...................###...#..##....#.....#.##.#####.##..#.#...#..#..##.#..#..#.......................
............................#......###.....###..###...##..#....#....................................
..................#..###..#.#.....#....#...####.#..#####.##...##########...##.......................
............................#..##...###......#..####.###.##...##....................................
.................###...#..##......#.##...##...#..#...####..#...##.####.##...........................
............................#...##.###.##.#..#...#.....####.#.##....................................
................#..###..#.#........#####.#..##...##.#...#....#.#..#..#..#.#.........................
............................#...###..#..#..#.#..####.##...##.####...................................
...............###...#..##..........##..##.#.#.#....##.##.#.#.##..#..##..##.........................
.............................#.#.....#.#.....#.#.##.#.#..###.##.#...................................
..............#..###..#.#.................#..#....#.########.#.#.##..####.#.........................
.................................##.###..#.#..##.##....#..#.#.......................................
.............###...#..##..................#..#...#.......##.##...#..#..##.#.........................
............................#.#..#....#....#.#####..#....#.##.......................................
............#..###..#.#....................#..#..#......#..##..##...##.####.........................
............................###......###..#.##.##....#..#.##.#......................................
...........###...#..##......................##...#.......##..##....#...#.###........................
.............................#....##..##...###..#..#..#..#...#.#....................................
..........#..###..#.#............................#.##..####....####.###.####........................
..............................##.#.#######.###.######.#####.#.###...................................
.........###...#..##..............................##..####....##..#.##.#.#..#.......................
.................................#####.#####..##...#####....#.#.#...................................
........#..###..#.#................................##....##....##.###.##.#####......................
................................###.###..##.#..#......#...##..#.....................................
.......###...#..##................................................#.##.#..####......................
......................................#.#...#########.#####...####..................................
......#..###..#.#.....................................................##.##.##......................
....................................###..###.#...#.#.###.....#..#....##.............................
.....###...#..##......................................................##............................
..................................##.....##.###...##.###...##.####..#..#............................
....#..###..#.#.....................................................#.##..####.#....................
..................................###.##..#..#....#...#####.#.##.#....###...........................
...###...#..##.....................................................#..#.###..###....................
...................................#..#...#....#.....##..##...#.#.#####.#...........................
..#..###..#.#......................................................#.##.#..#..#.....................
....................................##.#.#..##..#...#.##..####.######...............................
.###...#..##........................................................##......##......................
.......................................###...#...####..##.###.#......##.............................
#..###..#.#..........................................................##.............................
........................................#..#..#...##.#...#..#####.#..#..............................
.###.#..##..........................................................................................
.........................................##.#.....#.....#######.###.##..............................
#.#.#.#.#...........................................................................................
............................................#....##...#......##.##..#.#.............................
.####.##............................................................................................
.............................................#..##..###........####.#..#............................
.#.##.#.............................................................................................
..............................................##..##............###.##.#............................
..####..............................................................................................
.....................................................................##.............................
...##...............................................................................................
....................................................................##..............................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................