【发布时间】:2020-05-23 03:34:30
【问题描述】:
我正在为 connect4 制作一个更新板,并尝试在板上的前一个上堆叠一个。但是遍历董事会让我感到困惑。我知道 for 循环不正确,因为它只是一个数字。但是我将如何做到这一点(playcol 是恒定的,它将是 3)。除了可能使用切片之外,在 google 上找不到很多东西吗?
'''
board = [[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,0,0,0],
[0,0,0,1,0,0,0]]
def updateboard(playcol,board):
x=0
for x in board[x][playcol]:
if board[x][playcol] == 0:
try:
continue
#if it goes too far past it will throw index error and return the previous
except IndexError:
board[x-1][playcol] = 1
#else there is a number already there and make the one before it a number
elif board[x][playcol] != 0:
board[x-1][playcol] = 1
print(board)
'''
【问题讨论】:
标签: python-3.x loops iteration