假设我有一个这样的列表:
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,]
表示二维板上的空间。
如果玩家一去,它将变为 1。
如果玩家二去,它将变为 2。
获胜条件是:如果一行或一列被完全填满,或者对角线。
以下是我对水平或垂直赢家的功能:
def horizontal_winner(board, boxes):
'''
function will find if the horizontal win conditions apply
given 2 inputs. the board, a list and the number of boxes
- board - the 2D board of the game
- boxes - number of boxes per side
'''
for i in range(boxes):
player_1 = 0
player_2 = 0
for j in range(boxes):
if board[i][j] == 1:# first iteration - [1, 0, 0, 0, 0]
player_1 += 1
print("p1: " + str(player_1))
elif board[i][j] == 2:# first iteration - [2, 0, 0, 0, 0]
player_2 += 1
print("p2: " + str(player_2))
if player_1 == boxes:
return True
elif player_2 == boxes:
return False
def vertical_winner(board, boxes):
'''
function will find if the vertical win conditions apply
given 2 inputs. the board, a list and the number of boxes per side
- board - the 2D board of the game
- boxes - number of boxes per side
'''
for i in range(boxes):
player_1 = 0
player_2 = 0
for j in range(boxes):
if board[j][i] == 1:# first iteration - [1, 0, 0, 0, 0]
player_1 += 1
elif board[j][i] == 2:# first iteration - [2, 0, 0, 0, 0]
player_2 += 1
if player_1 == boxes:
return True
elif player_2 == boxes:
return False
我将如何检查对角线?