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