这是生命游戏计划。当我测试它时,我的周围(row,col)函数返回0,即使配置文件指示将制作8个方格" LIVE。"刚刚打开配置文件后打印电路板就进行了测试,但事实证明并没有使指示的方块说“现场”,“真实”。那些“活着”的人说'没有'所以没有' LIVE'正在计算价值。
[[None,None,None,0,0,0,0],[None,0,None,0,0,0,0],[None,None,None,0,0,0,0] ],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0], {0,0,0,0,0,0,0]是我print board
时得到的。无法看到我在这里失踪的东西?
LIVE = 1
DEAD = 0
def board(canvas, width, height, n):
for row in range(n+1):
for col in range(n+1):
canvas.create_rectangle(row*height/n,col*width/n,(row+1)*height/n,(col+1)*width/n,width=1,fill='black',outline='green')
n = int(raw_input("Enter the dimensions of the board: "))
width = n*25
height = n*25
from Tkinter import *
import math
window=Tk()
window.title('Game of Life')
canvas=Canvas(window,width=width,height=height,highlightthickness=0)
canvas.grid(row=0,column=0,columnspan=5)
board = [[DEAD for row in range(n)] for col in range(n)]
rect = [[None for row in range(n)] for col in range(n)]
for row in range(n):
for col in range(n):
rect[row][col] = canvas.create_rectangle(row*height/n,col*width/n,(row+1)*height/n,(col+1)*width/n,width=1,fill='black',outline='green')
#canvas.itemconfigure(rect[2][3], fill='red') #rect[2][3] is rectangle ID
#print rect
f = open('filename','r') #filename is whatever configuration file is chosen that gives the step() function to work off of for the first time
for line in f:
parsed = line.split()
print parsed
if len(parsed)>1:
row = int(parsed[0].strip())
col = int(parsed[1].strip())
board[row][col] = LIVE
board[row][col] = canvas.itemconfigure(rlist[row][col], fill='red')
def surrounding(row,col):
count = 0
if board[(row-1) % n][(col-1) % n] == LIVE:
count += 1
if board[(row-1) % n][col % n] == LIVE:
count += 1
if board[(row-1) % n][(col+1) % n] == LIVE:
count += 1
if board[row % n][(col-1) % n] == LIVE:
count += 1
if board[row % n][(col+1) % n] == LIVE:
count += 1
if board[(row+1) % n][(col-1) % n] == LIVE:
count +=1
if board[(row+1) % n ][col % n] == LIVE:
count += 1
if board[(row+1) % n][(col+1) % n] == LIVE:
count += 1
print count
return count
surrounding(1,1)
答案 0 :(得分:5)
您要两次分配board
嵌套列表中的项目:
board[row][col] = LIVE
board[row][col] = canvas.itemconfigure(rlist[row][col], fill='red')
第一个将1
分配给适当的值,第二个将1
替换为None
,因为这是使用这些参数调用时canvas.itemconfigure
的返回值。我怀疑(没有测试它)你应该简单地从第二个语句中删除作业:
board[row][col] = LIVE
canvas.itemconfigure(rlist[row][col], fill='red')
这可能仍有问题(例如,rlist
需要rect
,或许?),但应解决None
值的问题。