Langton's ant: Difference between revisions

→‎{{header|Processing}}: adding Processing Python mode
m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
(→‎{{header|Processing}}: adding Processing Python mode)
Line 5,457:
set(x,y,white?#ffffff:#000000);
}</lang>
 
==={{header|Processing Python mode}}===
<lang python>"""
we use the following conventions:
directions 0: up, 1: left, 2: down: 3: right
 
pixel white: True, black: False
 
turn right: True, left: False
"""
 
# number of iteration steps per frame
# set this to 1 to see a slow animation of each
# step or to 10 or 100 for a faster animation
 
STEP = 100
count = 0
 
def setup():
global x, y, direction
 
# 100x100 is large enough to show the
# corridor after about 10000 cycles
size(100, 100, P2D)
 
background(255)
x = width / 2
y = height / 2
direction = 0
 
def draw():
global count
for i in range(STEP):
count += 1
pix = get(x, y) != -1 # white =-1
setBool(x, y, pix)
 
turn(pix)
move()
 
if (x < 0 or y < 0 or x >= width or y >= height):
println("finished")
noLoop()
break
 
if count % 1000 == 0:
println("iteration {}".format(count))
 
def move():
global x, y
if direction == 0:
y -= 1
elif direction == 1:
x -= 1
elif direction == 2:
y += 1
elif direction == 3:
x += 1
 
def turn(rightleft):
global direction
direction += 1 if rightleft else -1
if direction == -1:
direction = 3
if direction == 4:
direction = 0
 
def setBool(x, y, white):
set(x, y, -1 if white else 0)</lang>
 
=={{header|Prolog}}==
Anonymous user