Skip to content

Instantly share code, notes, and snippets.

@CaffeinatedDave
Created September 19, 2014 07:38
Show Gist options
  • Save CaffeinatedDave/38065f02851e6ed47fab to your computer and use it in GitHub Desktop.
Save CaffeinatedDave/38065f02851e6ed47fab to your computer and use it in GitHub Desktop.
Implementation of Langton's Ant - WLHN Sept '14
object Langton extends App {
// TODO - make infinite vectors...
type Grid = Vector[Vector[Boolean]]
case class Ant(pos: (Int, Int), direction: Symbol) {
// If current position is white (false), move right 1 square
// If position is black (true), move left
def move(grid: Grid): Ant = {
(grid(pos._1)(pos._2), direction) match {
case (true, 'N) => Ant((pos._1 - 1, pos._2), 'W)
case (true, 'W) => Ant((pos._1, pos._2 + 1), 'S)
case (true, 'E) => Ant((pos._1, pos._2 - 1), 'N)
case (true, 'S) => Ant((pos._1 + 1, pos._2), 'E)
case (false, 'N) => Ant((pos._1 + 1, pos._2), 'E)
case (false, 'W) => Ant((pos._1, pos._2 - 1), 'N)
case (false, 'E) => Ant((pos._1, pos._2 + 1), 'S)
case (false, 'S) => Ant((pos._1 - 1, pos._2), 'W)
}
}
}
// Flip a single point on the grid
def changeGrid(grid: Grid, pos: (Int, Int)): Grid = {
// Should probably map this but its good enough...
(for (x <- 0 until grid.length) yield {
if (x != pos._1) grid(x)
else {
(for (y <- 0 until grid(x).length) yield {
if (y != pos._2) grid(x)(y)
else !grid(x)(y)
}).toVector
}
}).toVector
}
// move the ant, change the grid based on the old position
def step(ant: Ant, grid: Grid): (Ant, Grid) = (ant.move(grid), changeGrid(grid, ant.pos))
// Vector[Vector[Boolean]] -> pretty
def display(grid: Grid, ant: Ant): Unit = {
val printable = (for (x <- 0 until grid.length) yield {
if (x != ant.pos._1) grid(x).map(c => if (c) "xx" else "__")
else {
(for (y <- 0 until grid(x).length) yield {
if (y != ant.pos._2) {if (grid(x)(y)) "xx" else "__"}
else "**"
}).toVector
}
}).toVector
for (x <- printable) {
for (y <- x) Console.print(y)
Console.println()
}
Console.println(ant)
}
// Take one step, print it, sleep, and do it again
def play (ant: Ant, grid: Grid, steps: Int): (Ant, Grid) = {
if (steps != 0) {
val (a, b) = step(ant, grid)
display(b, a)
Thread.sleep(100) //So we can see movement flickering in the console...
play(a, b, steps - 1)
} else {
(ant, grid)
}
}
// Create a 40x40 grid, and start off with all cells white
val grid: Grid = {
(for (x <- 0 to 39) yield {
(for (y <- 0 to 39) yield false).toVector
}).toVector
}
// Put the ant in the middle, pointing North
val ant = Ant((20, 20), 'N)
// Go 3000 steps
val (a,b) = play(ant, grid, 3000)
display(b, a)
}
Output after 100 iterations:
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
__________________________________________xxxx__________________________________
________________________________________xx____xx________________________________
__________________________________xx__xxxx______xx______________________________
________________________________xx__**__xxxxxx__xx______________________________
________________________________xx____________xx________________________________
__________________________________xx____xx______________________________________
____________________________________xxxx________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
Ant((20,18),'N)
Output after 3000 iterations:
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
____________________________xxxx________________________________________________
__________________________xxxxxxxx____xxxx______________________________________
________________xxxx____xx__xxxx__xxxxxx**______________________________________
______________xxxx______xxxxxxxx__xx__xx__xxxxxxxx______________________________
____________xx__xxxx__xx__xx__xxxx______xx____xx__xx____________________________
____________xxxxxxxx__xxxxxx____xxxxxx______xx__xxxxxx__________________________
__________________xxxxxxxxxxxxxxxxxxxx__xx__xx____xxxx__________________________
__________xx____xx______xxxx______xxxx____xxxxxx__xx____________________________
________xx__xx____xx__xx__xx__xx______xxxxxxxx__xx______________________________
________xx________________xxxxxxxxxx__xxxxxxxx__________________________________
__________xx____xx__xxxxxxxx________xxxxxxxxxx____xx____________________________
__________xx________xx____________xx__xxxxxxxx______xx__________________________
__________xxxxxx______xx______xxxx__xxxx________xx__xx__________________________
__________xxxxxx__xxxxxx__xxxxxx____xxxx__xx____xxxx____________________________
__________xx______xxxxxxxx______xx__xx____xx__xxxx______________________________
__________xx______xx______xx____xxxx__xx________________________________________
__________xx______xx__xx______xxxxxx________xxxx________________________________
__________xx________xx__xx________xxxx________xx________________________________
__________xx____xxxxxx__xxxx____xx____xxxxxx____xxxx____________________________
__________xxxx__xxxx__________xxxx____xx__xx____xx__xx__________________________
__________xxxxxx______________xx__xxxx____________xx__xx________________________
____________xx____xxxx____xx____xxxx__xxxxxx__xx__xx__xx________________________
______________xxxx__xx________xxxxxx____________xx____xx________________________
__________________xx______xx__xx____xxxxxxxx____xxxx__xx________________________
____________xx__xx________xx__________xx__xxxxxxxxxxxx__________________________
____________xxxxxx________________________xx____xxxx____________________________
______________xx____________xxxx________xx______________________________________
________________xxxxxxxxxxxx____xxxxxxxx________________________________________
Ant((14,20),'S)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment