这是Game of Life计划.当我测试它时,我的周围(行,col)函数返回0,即使配置文件指示8个方格将被设置为"LIVE".只需在打开配置文件后通过打印电路板进行测试,结果表明,而不是让指示的方块显示为"LIVE",那些"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)
Run Code Online (Sandbox Code Playgroud)
您将board
两次分配嵌套列表的项目:
board[row][col] = LIVE
board[row][col] = canvas.itemconfigure(rlist[row][col], fill='red')
Run Code Online (Sandbox Code Playgroud)
第一个赋值1
给适当的值,第二个替换1
with None
,因为那是canvas.itemconfigure
用这些参数调用时的返回值.我怀疑(不测试它)你应该简单地从第二个语句中删除赋值:
board[row][col] = LIVE
canvas.itemconfigure(rlist[row][col], fill='red')
Run Code Online (Sandbox Code Playgroud)
这可能仍有问题(例如rlist
需要rect
,或许?),但None
应解决值的问题.
归档时间: |
|
查看次数: |
34 次 |
最近记录: |