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