Langton's ant: Difference between revisions

m
(Updated to compile with Nim 1.4)
Line 1,744:
=={{header|Dyalect}}==
 
<lang dyalect>constlet xInc = [0, 1, -1, 0]
constlet yInc = [-1, 0, 0, 1]
constlet north = 0
constlet east = 1
constlet west = 2
constlet south = 3
constlet leftTurns = [ west, north, south, east ]
constlet 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() {
constlet w = 100
constlet h = 100
constlet blacks = Array.empty(w, () => Array.empty(h, false))
constlet ant = newAnt(w, h)
while !ant::outOfBounds {
blacks.step(ant)
}
Anonymous user