作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这不是我的全部代码。这只是显示了我想要完成的事情。我想要的是列表“板”保持不变,列表"new"改变。我的程序存在的问题是"new"列表和主板都发生了变化。
DEADCELL = "."
LIVECELL = "A"
rows = 5
columns = 5
def startingBoard():
cellRow = ""
cellCol = 1
board = []
for i in range(rows):
board.append([DEADCELL]* columns)
while cellRow != "q":
cellRow = input("Please enter the row of a cell to turn on (or q to exit): ")
if cellRow != "q":
cellCol = int(input("Please enter a column for that cell: "))
board[int(cellRow)][cellCol] = LIVECELL
return board
def printBoard(board):
for i in range(rows):
printRow = ""
for j in range(columns):
if board[i][j] == LIVECELL:
printRow = printRow + LIVECELL
elif board[i][j] == DEADCELL:
printRow = printRow + DEADCELL
print(printRow)
print("\n")
def neighbors(board):
new = list(board)
for r in range(rows):
for c in range(columns):
neighbors = 0
# ALL THIS SECTION DOES IS CHECK THE 'NEIGHBORS' AROUND A CELL
if r - 1 >= 0 and c - 1 >= 0:
if board[r - 1][c - 1] == LIVECELL:
neighbors += 1
if c - 1 >= 0:
if board[r][c-1] == LIVECELL:
neighbors += 1
if r + 1 < rows and c + 1 < rows:
if board[r + 1][c + 1] == LIVECELL:
neighbors += 1
if r - 1 >= 0:
if board[r - 1][c] == LIVECELL:
neighbors += 1
if r + 1 < rows:
if board[r + 1][c] == LIVECELL:
neighbors += 1
if r - 1 >=0 and c + 1 < rows:
if board[r-1][c+1] == LIVECELL:
neighbors += 1
if c + 1 < rows:
if board[r][c+1] == LIVECELL:
neighbors += 1
if r + 1 < rows and c - 1 >= 0:
if board[r+1][c-1] == LIVECELL:
neighbors += 1
# RULES FOR CELLS:
# IF A LIVE CELL HAS TWO OR THREE NEIGHBORS, IT STAYS ALIVE
# IF A LIVE CELL HAS LESS THAN TWO NEIGHBORS, IT DIES
# IF A LIVE CELL HAS MORE THAN THREE NEIGHBORS, IT DIES
# IF A DEAD CELL HAS THREE NEIGHBORS, IT BECOMES ALIVE
if board[r][c] == DEADCELL and neighbors == 3:
new[r][c] = LIVECELL
elif board[r][c] == LIVECELL and (neighbors < 2 or neighbors > 3):
new[r][c] = DEADCELL
#This prints out the new list and the board list to show you guys how they are the same.
print(new)
print(board)
def main():
board = startingBoard()
printBoard(board)
neighbors(board)
printBoard(board)
main()
这是我的输出:
Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 1
Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 2
Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 3
Please enter the row of a cell to turn on (or q to exit): q
.....
.....
.AAA.
.....
.....
[['.', '.', '.', '.', '.'], ['.', '.', 'A', 'A', '.'], ['.', 'A', '.', 'A', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
[['.', '.', '.', '.', '.'], ['.', '.', 'A', 'A', '.'], ['.', 'A', '.', 'A', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
.....
..AA.
.A.A.
.....
.....
看看 new 列表和 board 列表有何相同。这是为什么?
最佳答案
new = list(board)
是浅复制:它不会克隆子元素,只会复制引用。
a = [[True]]
b = list(a)
b[0][0] = False
a
# => [[False]]
您需要深层复制:
a = [[True]]
from copy import deepcopy
b = deepcopy(a)
b[0][0] = False
a
# => [[True]]
关于python - 复制列表错误Python(康威生命游戏的一部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727066/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!