为什么将值设为 "None" 而不是 "LIVE"?
Why are the values being made into "None" instead of "LIVE"?
这是生命游戏程序。当我测试它时,即使配置文件指示将制作 8 个正方形,我的 surrounding(row,col) 函数返回 0 "LIVE." 只是 运行 打开配置文件后通过打印板进行测试,事实证明,不是让指示的方块说 'LIVE,',而是 'LIVE' 的方块说 'None',所以没有计算 '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)
您正在为 board
嵌套列表的项目分配两次:
board[row][col] = LIVE
board[row][col] = canvas.itemconfigure(rlist[row][col], fill='red')
第一个将 1
分配给适当的值,第二个将 1
替换为 None
,因为这是 canvas.itemconfigure
的 return 值用这些参数调用。我怀疑(无需测试)您应该简单地从第二个语句中删除赋值:
board[row][col] = LIVE
canvas.itemconfigure(rlist[row][col], fill='red')
这可能仍然存在问题(例如 rlist
需要 rect
,也许?),但是 None
值的问题应该得到解决。
这是生命游戏程序。当我测试它时,即使配置文件指示将制作 8 个正方形,我的 surrounding(row,col) 函数返回 0 "LIVE." 只是 运行 打开配置文件后通过打印板进行测试,事实证明,不是让指示的方块说 'LIVE,',而是 'LIVE' 的方块说 'None',所以没有计算 '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)
您正在为 board
嵌套列表的项目分配两次:
board[row][col] = LIVE
board[row][col] = canvas.itemconfigure(rlist[row][col], fill='red')
第一个将 1
分配给适当的值,第二个将 1
替换为 None
,因为这是 canvas.itemconfigure
的 return 值用这些参数调用。我怀疑(无需测试)您应该简单地从第二个语句中删除赋值:
board[row][col] = LIVE
canvas.itemconfigure(rlist[row][col], fill='red')
这可能仍然存在问题(例如 rlist
需要 rect
,也许?),但是 None
值的问题应该得到解决。