Langton's ant: Difference between revisions

Line 1,750:
const west = 2
const south = 3
 
const leftTurns = [ west, north, south, east ]
const rightTurns = [ east, south, north, west ]
 
func move(ant) {
ant.:position.:x += xInc[ant.:direction]
ant.:position.:y += yInc[ant.:direction]
}
 
func Array.step(ant) {
var ptCur = (x: = ant.:position.:x + ant.:origin.:x, y: = ant.:position.:y + ant.:origin.:y)
var leftTurn = this[ptCur.:x][ptCur.:y]
ant.direction =
if leftTurn {
leftTurns[ant.:direction]
} else {
rightTurns[ant.:direction]
}
this[ptCur.:x][ptCur.:y] = !this[ptCur.:x][ptCur.:y]
move(ant)
ptCur = (x: = ant.:position.:x + ant.:origin.:x, y: = ant.:position.:y + ant.:origin.:y)
ant.:outOfBounds =
ptCur.:x < 0 ||
ptCur.:x >= ant.:width ||
ptCur.:y < 0 ||
ptCur.:y >= ant.:height
ant.:position
}
 
func newAnt(width, height) {
(
position: = (x: = 0, y: = 0),
origin: = (x: = width / 2, y: = height / 2),
outOfBounds: = false,
isBlack: = [],
direction: = east,
width: = width,
height: = height
)
}
 
func run() {
const w = 100
Line 1,796:
const blacks = Array.empty(w, () => Array.empty(h, false))
const ant = newAnt(w, h)
 
while !ant.:outOfBounds {
blacks.step(ant)
}
 
var iRow = 0;
 
while iRow < w {
var iCol = 0;
Line 1,818:
}
}
 
run()</lang>
 
Anonymous user