Langton's ant: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: used proper English; changed ''leftest'' to ''leftmost''. -- ~~~~)
(add scala)
Line 2,462: Line 2,462:
# 100
# 100
</pre>
</pre>

=={{header|Scala}}==
<lang scala>class Langton(matrix:Array[Array[Char]], ant:Ant) {
import Langton._
val rows=matrix.size
val cols=matrix(0).size
def isValid = 0 <= ant.x && ant.x < cols && 0 <= ant.y && ant.y < rows
def isBlack=matrix(ant.x)(ant.y)==BLACK
def changeColor(c:Char)={matrix(ant.x)(ant.y)=c; matrix}
def evolve():Langton={
val (newCol, newAnt)=if(isBlack) (WHITE, ant.turnLeft.move) else (BLACK, ant.turnRight.move)
new Langton(changeColor(newCol), newAnt)
}
override def toString()=matrix map (_.mkString("")) mkString "\n"
}

case class Ant(x:Int, y:Int, d:Int=0) {
def turnLeft=Ant(x,y,(d-1)&3)
def turnRight=Ant(x,y,(d+1)&3)
def move=d match {
case 0 => Ant(x,y-1,d) // north
case 1 => Ant(x+1,y,d) // east
case 2 => Ant(x,y+1,d) // south
case 3 => Ant(x-1,y,d) // west
}
}

object Langton {
val BLACK='#'
val WHITE='.'
def apply(x:Int=100, y:Int=100)=new Langton(Array.fill(y, x)(WHITE), Ant(x>>>1, y>>>1, 0))
def main(args: Array[String]): Unit = {
var l=Langton(100,100)
var moves=0
while (l.isValid) {
moves += 1
l=l.evolve
}
println("Out of bounds after "+moves+" moves")
println(l)
}
}</lang>
Output:
<pre style="height: 40ex; overflow: scroll">Out of bounds after 11669 moves
........................................................................#.#.........................
.......................................................................#.#.##.......................
......................................................................##.###.#......................
.....................................................................#.###.####.....................
....................................................................##..#.#####.....................
...................................................................#.##.##...#......................
..................................................................##..#...###.......................
.................................................................#.##.##...#........................
................................................................##..#...###.........................
...............................................................#.##.##...#..........................
..............................................................##..#...###...........................
.............................................................#.##.##...#............................
............................................................##..#...###.............................
...........................................................#.##.##...#..............................
..........................................................##..#...###...............................
.........................................................#.##.##...#................................
........................................................##..#...###.................................
.......................................................#.##.##...#..................................
......................................................##..#...###...................................
.....................................................#.##.##...#....................................
....................................................##..#...###.....................................
...................................................#.##.##...#......................................
..................................................##..#...###.......................................
.................................................#.##.##...#........................................
................................................##..#...###.........................................
...............................................#.##.##...#..........................................
..............................................##..#...###...........................................
.............................................#.##.##...#............................................
............................................##..#...###.............................................
...........................................#.##.##...#..............................................
..........................................##..#...###...............................................
.....................................##..#.##.##...#................................................
....................................##..##..#...###.................................................
.................................#...##..##.##...#..................................................
.................................###..#...#...###...####............................................
.................................#...####.##...#...#....#...........................................
................................#.##.#......#.#...#....###..........................................
................................##.#..##.#.....##.#....###..........................................
...................................##.....#.#.##...#....#...........................................
..................................#...#..#####.#......#.#...........................................
..............................######.##..........#####...#..........................................
.............................##.#.##...#.#.#.##.#..##..###..........................................
............................#.##....###..#...#.#######.#..##........................................
............................#...#...##.#..#...##.######..#..#.......................................
.............................#...#######.######..#.##.#.#....#......................................
.............................#.##.#.##..##....####.#.##.####.#......................................
............................###....##.######.#..#...####....#.......................................
............................###...##..##..#.###.#.##.#...#..........................................
............................#.....#.##.##..#....#######.............................................
............................#.....#..##.##.##.####..##.##..####.....................................
............................#....####.#....###.##.###...#.#....#....................................
............................#......#.#....#####.#.#.###.......###...................................
............................#.....##.###.##...#.##.####.###...#.#...................................
............................#.....#.#.#.####....####..##.##.........................................
............................#......###.....###..###...##..#....#....................................
............................#..##...###......#..####.###.##...##....................................
............................#...##.###.##.#..#...#.....####.#.##....................................
............................#...###..#..#..#.#..####.##...##.####...................................
.............................#.#.....#.#.....#.#.##.#.#..###.##.#...................................
.................................##.###..#.#..##.##....#..#.#.......................................
............................#.#..#....#....#.#####..#....#.##.......................................
............................###......###..#.##.##....#..#.##.#......................................
.............................#....##..##...###..#..#..#..#...#.#....................................
..............................##.#.#######.###.######.#####.#.###...................................
.................................#####.#####..##...#####....#.#.#...................................
................................###.###..##.#..#......#...##..#.....................................
......................................#.#...#########.#####...####..................................
....................................###..###.#...#.#.###.....#..#....##.............................
..................................##.....##.###...##.###...##.####..#..#............................
..................................###.##..#..#....#...#####.#.##.#....###...........................
...................................#..#...#....#.....##..##...#.#.#####.#...........................
....................................##.#.#..##..#...#.##..####.######...............................
.......................................###...#...####..##.###.#......##.............................
........................................#..#..#...##.#...#..#####.#..#..............................
.........................................##.#.....#.....#######.###.##..............................
............................................#....##...#......##.##..#.#.............................
.............................................#..##..###........####.#..#............................
..............................................##..##............###.##.#............................
.....................................................................##.............................
....................................................................##..............................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................</pre>


=={{header|Tcl}}==
=={{header|Tcl}}==