RosettaCodeData/Task/Langtons-ant/Bc/langtons-ant.bc
2023-07-01 13:44:08 -04:00

50 lines
990 B
Text

define o() {
auto i, j
"P1 "
w
h
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
a[j * w + i]
}
}
}
define l(w, h, x, y) {
auto a[], d, i, x[], y[]
/* d represents one of the four possible directions:
* 0
* ⇑
* 3⇐ ⇒1
* ⇓
* 2
* The arrays x[] and y[] contain the changes to the x and y direction for
* each value of d.
*/
x[1] = 1
x[3] = -1
y[0] = -1
y[2] = 1
while (1) {
i = y * w + x
if (a[i] == 0) d += 1 /* turn right if white */
if (a[i] == 1) d -= 1 /* turn left if black */
if (d < 0) d = 3
if (d > 3) d = 0
x += x[d]
y += y[d]
a[i] = 1 - a[i] /* toggle cell colour */
if (x < 0) break
if (x == w) break
if (y < 0) break
if (y == h) break
}
o()
}
l(100, 100, 50, 50)
quit