Langton's ant: Difference between revisions

Content added Content deleted
(Updated to compile with Nim 1.4)
Line 1,744: Line 1,744:
=={{header|Dyalect}}==
=={{header|Dyalect}}==


<lang dyalect>const xInc = [0, 1, -1, 0]
<lang dyalect>let xInc = [0, 1, -1, 0]
const yInc = [-1, 0, 0, 1]
let yInc = [-1, 0, 0, 1]
const north = 0
let north = 0
const east = 1
let east = 1
const west = 2
let west = 2
const south = 3
let south = 3
const leftTurns = [ west, north, south, east ]
let leftTurns = [ west, north, south, east ]
const rightTurns = [ east, south, north, west ]
let 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
let w = 100
const h = 100
let h = 100
const blacks = Array.empty(w, () => Array.empty(h, false))
let blacks = Array.empty(w, () => Array.empty(h, false))
const ant = newAnt(w, h)
let ant = newAnt(w, h)
while !ant:outOfBounds {
while !ant::outOfBounds {
blacks.step(ant)
blacks.step(ant)
}
}