我有一个玩奥赛罗的棋盘(由一个列表列表代表)。游戏的重点是翻转对方玩家的棋子。
在这里,我试图记录板上可以翻转的位置。每个位置都由表示行和列的坐标表示。通常,输入是玩家棋子所在的棋盘位置,并且该功能正在检查有效的移动(定位相邻的相对棋子),并且输出应该是可以翻转相对棋子的位置列表。
但是,我的问题是我似乎无法将这些位置附加到列表中并返回它们。每次我尝试返回时,都会得到一个“ []”,这表示它什么也不做,或者无,这取决于我将缩进位置放在哪里。
奇怪为什么不存储坐标值。该功能的相关部分就在我要记录所有这些位置的“如果有效:”部分之后。
def findOppose(board, colorPlaying, colorAgainst, piece, row, col):
## find opposing pieces and turn them into colorPlaying
valid = False
opp = []
pieces = [piece]
try:
if board[piece[0]+row][piece[1] + col] == colorAgainst:
if (piece[1] + col == 0) or (piece[1] + col == 7) or (piece[0] + row == 0) or (piece[0] + row == 7):
next
else:
piece[0], piece[1] = piece[0]+row, piece[1] + col
## check if there is colorPlaying surrounding these pieces otherwise would flip everything
while board[piece[0]][piece[1]] != colorPlaying:
## no surrounding colorPlaying piece
## if black, example would be BWW. but need BWWB
if board[piece[0]+row][piece[1]+col] == " .":
break
## reached boundaries of board
elif (piece[1] + col < 0) or (piece[1] + col > 7) or (piece[0] + row < 0) or (piece[0] + row > 7):
break
elif board[piece[0]][piece[1]] == colorPlaying:
valid = True
break
else:
board[piece[0]][piece[1]] = board[piece[0]+row][piece[1]+col]
## flip
if valid:
while board[piece[0]][piece[1]] == colorAgainst:
## there is an issue of directly changing here as the next findOppose() will
## continue where the board is left off which means other pieces will change incorrectly
# board[piece[0]][piece[1]] = colorPlaying
if board[piece[0]][piece[1]] == colorAgainst:
opp_piece = piece[0],piece[1]
opp.append(opp_piece) ## append to list of all opposing pieces
if board[piece[0]+row][piece[1] + col] == colorAgainst: ## keep progressing through
board[piece[0]][piece[1]] = board[piece[0]+row][piece[1]+col]
else:
break
except IndexError:
pass
return opp