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.position.x += xInc[ant.direction]
ant:position:x += xInc[ant:direction]
ant.position.y += yInc[ant.direction]
ant:position:y += yInc[ant:direction]
}
}

func Array.step(ant) {
func Array.step(ant) {
var ptCur = (x: ant.position.x + ant.origin.x, y: ant.position.y + ant.origin.y)
var ptCur = (x = ant:position:x + ant:origin:x, y = ant:position:y + ant:origin:y)
var leftTurn = this[ptCur.x][ptCur.y]
var leftTurn = this[ptCur:x][ptCur:y]
ant.direction =
ant.direction =
if leftTurn {
if leftTurn {
leftTurns[ant.direction]
leftTurns[ant:direction]
} else {
} else {
rightTurns[ant.direction]
rightTurns[ant:direction]
}
}
this[ptCur.x][ptCur.y] = !this[ptCur.x][ptCur.y]
this[ptCur:x][ptCur:y] = !this[ptCur:x][ptCur:y]
move(ant)
move(ant)
ptCur = (x: ant.position.x + ant.origin.x, y: ant.position.y + ant.origin.y)
ptCur = (x = ant:position:x + ant:origin:x, y = ant:position:y + ant:origin:y)
ant.outOfBounds =
ant:outOfBounds =
ptCur.x < 0 ||
ptCur:x < 0 ||
ptCur.x >= ant.width ||
ptCur:x >= ant:width ||
ptCur.y < 0 ||
ptCur:y < 0 ||
ptCur.y >= ant.height
ptCur:y >= ant:height
ant.position
ant:position
}
}

func newAnt(width, height) {
func newAnt(width, height) {
(
(
position: (x: 0, y: 0),
position = (x = 0, y = 0),
origin: (x: width / 2, y: height / 2),
origin = (x = width / 2, y = height / 2),
outOfBounds: false,
outOfBounds = false,
isBlack: [],
isBlack = [],
direction: east,
direction = east,
width: width,
width = width,
height: 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.outOfBounds {
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>