1
我對編程非常陌生,但我認爲嘗試編程Langton的螞蟻會很有趣。對於那些你不知道的人來說,這是一個在董事會中散步的螞蟻。當它遇到紅色方塊時,它會右轉並將紅色方塊變成白色,當它碰到白色方塊時,它會左轉,將之前的白色方塊變成紅色。隨着我的編程非常原始的知識,我想出了這個計劃:如何檢查一組座標是否在列表中
def Move(direction):
global x
global y
if direction % 4 == 0:
y += 1
elif direction % 4 == 1:
x += 1
elif direction % 4 == 2:
y -= 1
else:
x -= 1
Lred = []
Lwhite = []
# To make sure the program recognizes the squares as being white initially.
for i in range(-4, 5):
for i2 in range(-4, 5):
Lwhite.append([i2,i])
x = 0
y = 0
direct = 0
# Here I try to make the first 10 steps.
for i in range(10):
print [x,y]
if [x,y] in Lred:
Lred.remove([x,y])
Lwhite.append([x,y])
direct-=1
if [x,y] in Lwhite:
print "white square"
Lwhite.remove([x,y])
Lred.append([x,y])
direct+=1
Move(direct)
print Lwhite
print Lred
當你運行這個程序,你可以看到,它說,廣場是白色它擊中了第二次[0,0],但當它開始時,它應該將它塗成紅色。 [0,0]在最後打印的紅色方塊列表中,並且不在白色方塊列表中。這是我想要的。
但是爲什麼它仍然認爲「Lwhite中的if [x,y]」語句是正確的?