我做了一个游戏,其中生成一块随机点亮的方块(0关闭,1打开),目标是点亮整个板。目前我一直在使用用户输入来赢得游戏,当点击一个正方形时,它反转以及上面,下面,左,右的邻居。现在我试图允许计算机在没有用户输入的情况下解决游戏,但choice
中的evolve2d
函数无法正常工作。目前它正在反复制作原始列表。任何指导都将不胜感激。
import time # provides time.sleep(0.5)
from csplot import choice
from random import * # provides choice( [0,1] ), etc.
import sys # larger recursive stack
sys.setrecursionlimit(100000) # 100,000 deep
def runGenerations2d(L , x = 0,y=0):
show(L)
print( L ) # display the list, L
time.sleep(.1) # pause a bit
newL = evolve2d( L ) # evolve L into newL
print(newL)
if min(L) == 1:
#I like read outs to be explained so I added an extra print command.
if x<=1: # Takes into account the possibility of a 1 click completition.
print ('BaseCase Reached!... it took %i click to complete' % (x))
print (x)
done()#removes the need to input done() into the shell
else:
print ('BaseCase Reached!... it took %i clicks to complete' % (x))
print (x)
done()#removes the need to input done() into the shell
return
x = x+1 # add 1 to x before every recusion
runGenerations2d( newL , x,y ) # recurse
def setNewElement2d( L, i, j, x=0, y=0 ):
if y==j and (i == x-1 or i == x+1 or i ==x):
return 1-L[j][i]
elif x==i and (j == y-1 or j == y+1):
return 1-L[j][i]
else:
return L[j][i]
def evolve2d( L ):
N = len(L) # N now holds the size of the list
x = choice (L)
y = choice (L)
return [[ setNewElement2d( L, i, j,x,y ) for i in range(N)]for j in range(N) ]