对于家庭作业,我必须在python中制作基于文本的Conway生活游戏。它的规则是这样的。
活动邻居少于两个的活细胞死亡
任何有两三个活邻居的活细胞都会留在下一代。
任何有三个以上邻居的活细胞都会死亡。
任何有三个活邻居的死细胞都会成为活细胞。
我将棋盘放在2d列表中,我无法找到如何循环列表中的每个元素(游戏中的每个单元格,无论是死的还是活着的,' 0'或者' X')并根据周围的单元格检查条件。
起始板的一个例子是
00000
00000
000X0
000X0
000X0
首先,我现在尝试遍历每个元素,并检查当前单元格右侧的单元格是否为X.
这是我尝试执行此操作的代码。下一次迭代的功能应该是让康威的生命游戏的下一次迭代回归董事会,但是现在要弄清楚我只是想让它检查它右边的单元格和如果当前单元格右边的那个单元格,则将当前单元格更改为X.
def nextIteration(board):
newBoardTemp = board[:]
newBoard = [board[:] for board in newBoardTemp]
column = 0
for row in newBoard:
column = 0
for item in range(len(row)):
column += 1
item2 = row[column + 1]
if item == item2:
board[item][column] = 'X'
def printBoard(board):
for row in board:
for item in row:
print(item, end="")
print()
def main():
rows = input("Please enter number of rows: ")
columns = input("Please enter number of columns: ")
print()
cellRow = 0
cellRows = []
cellColumns = []
total = 0
#the cellRow and cellColumn will contain all of the inputted rows
#and columns connected by the index value
while cellRow != "q":
cellRow = input("Please enter the row of a cell to turn on (or q to exi\
t): ")
if cellRow != "q":
cellColumn = input("Please enter a column for that cell: ")
cellRows.append(cellRow)
cellColumns.append(cellColumn)
total = total + 1
print()
else:
print()
board = []
#boardTemp will hold a list that contains one row of the entire board
boardTemp = []
for i in range(int(rows)):
boardTemp.append("0")
for i in range(int(columns)):
board.append(boardTemp[:])
for i in range(total):
temp = i
board[int(cellRows[temp - 1])][int(cellColumns[temp - 1])] = "X"
iterations = input("How many iterations should I run? ")
print()
print("Starting Board:")
print()
printBoard(board)
for i in range(int(iterations)):
iterationNumber = 1
nextIteration(board)
print("Iteration", iterationNumber,":")
printBoard(board)
main()
这是我之前展示的起跑板上的错误。
File "hw7.py", line 9, in nextIteration
item2 = row[column + 1]
IndexError: list index out of range
答案 0 :(得分:0)
for item in range(len(row)):
column += 1
item2 = row[column + 1]
if item == item2:
board[item][column] = 'X'
在此循环的第四次迭代中,将item2设置为row [5],这超出了范围。 如果要比较两个相邻的单元格,则应稍后增加列(在第一个迭代项== row [0]和item2 == row [2]中)。 您还应该提前停止循环。 以下内容可以帮助您解决错误:
for item in range(len(row) - 1):
item2 = row[column + 1]
if item == item2:
board[item][column] = 'X'
column += 1
答案 1 :(得分:0)
你想用边做什么?一种可能性是将表面视为圆环,又称圆环...如果可以,请关注我
nr = 5 ; nc = 4
# here I define a List Of Lists with content unrelated to Conway's Life
# just to have an easy to follow example
lol = [["%d"%(r*10+c+11,) for c in range(nc)] for r in range(nr)]
print(lol)
# [['11', '12', '13', '14'],
# ['21', '22', '23', '24'],
# ['31', '32', '33', '34'],
# ['41', '42', '43', '44'],
# ['51', '52', '53', '54']]
# now, we build an AUGmented List Of Lists, prepending each row with
# its last element and appending the last one, prepending the first
# augmented row with the last row and appending the first to the last
aug_lol = [[lol[r%nr][c%nc] for c in range(-1,nc+1)] for r in range(-1,nr+1)]
print(aug_lol)
# [['54', '51', '52', '53', '54', '51'],
# ['14', '11', '12', '13', '14', '11'],
# ['24', '21', '22', '23', '24', '21'],
# ['34', '31', '32', '33', '34', '31'],
# ['44', '41', '42', '43', '44', '41'],
# ['54', '51', '52', '53', '54', '51'],
# ['14', '11', '12', '13', '14', '11']]
使用aug_lol
列表列表,您可以以最自然的方式查询元素11
(或其他)的邻居,
def neighborhood(alol, r, c):
return [alol[nr][nc] for nc in range(c,c+3)
for nr in range(r,r+3)
if (nr!=(r+1) or nc!=(c+1))]
算住邻居,
ln = sum(c=="X" for c in neighborhood(...))
要打印电路板,以下效率更高
print('\n'.join(''.join(ch for ch in r) for r in board))