Langton's Ant is an automata which creates a complex pattern by following a couple of simple rules:
- If the ant is on an empty pixel, turn 90° right, set the pixel then move forward
- If the ant is on a set pixel, turn 90° left, reset the pixel then move forward
The ant's path appears chaotic at first before falling into a repetitive “highway” pattern, moving 2 pixels diagonally every 104 cycles.
Here's the code to display Langton's Ant on the ZX Spectrum in 61 bytes. It runs in just over a second so you might want to add a halt
to slow things down:
org 65472
ld de,128*256+96
ANT:
; halt
ld a,c ; check direction
and 3
rrca
add a,a
dec a
jr nc,XMOVE
add a,e ; adjust y position +/-1
ld e,a
cp 192
ret nc
xor a
XMOVE:
add a,d ; adjust x position +/-1
ld d,a
; ----------
and 7 ; calculate screen address
ld b,a
inc b
ld a,e
rra
scf
rra
or a
rra
ld l,a
xor e
and 248
xor e
ld h,a
ld a,d
xor l
and 7
xor d
rrca
rrca
rrca
ld l,a
ld a,1
PLOTBIT:
rrca
djnz PLOTBIT
; ----------
ld b,a ; test pixel
and (hl)
jr nz,LEFT ; turn left/right
inc c
inc c
LEFT:
dec c
ld a,b ; flip pixel
xor (hl)
ld (hl),a
jr ANT
No comments:
Post a Comment
Note: only a member of this blog may post a comment.