1 回答
TA贡献1780条经验 获得超5个赞
这个问题有点宽泛,但我会尽力为您指明正确的方向:
首先,让用户选择他们的板尺寸相当简单。类似于以下内容的操作应该可行,您可以将变量分配给用户的输入,然后运行循环以创建棋盘大小的数组数组:
board_size = int(input("How big do you want your tic-tac-toe board to be?"))
board = []
for row in range(board_size):
board.append([])
for column in range(board_size):
board[row].append([])
给定输入 4,board 变量将等于:
[[[], [], [], []],
[[], [], [], []],
[[], [], [], []],
[[], [], [], []]]
但定义获胜条件有点困难。我们必须考虑您可能获胜的所有模式:横向三排、垂直三排以及对角线三排。由于我们的棋盘很好地保存为数组的数组,因此无论棋盘有多大,我们都可以制定通用规则来查看任意位置是否连续有 3 个“x”或 3 个“o”。
水平行非常简单:只需检查一行中是否有三个“x”或“o”:
for row in range(board_size):
for column in range(board_size-2):
if board[row][column] == board[row][column+1] == board[row][column+2]:
if board[row][column] == "x":
print("x wins!")
else:
print("o wins!")
现在,要添加垂直行,我们必须执行相同的操作,但要遍历列然后行而不是行然后列:
for column in range(board_size):
for row in range(board_size-2):
if board[row][column] == board[row+1][column] == board[row+2][column]:
if board[row][column] == "x":
print("x wins!")
else:
print("o wins!")
最后是对角线。我们通过遍历每一行和每一列来做到这一点,但我们将它们都加 1,或者向行加 1,并从列中取 1。这使我们能够得到这种形状(行和列都增加)或那种形状(行增加和列减少):
"x" "x"
"x" "x"
"x" "x"
要查找增加的行和增加的列:
for row in range(board_size-2):
for column in range(board_size-2):
if board[row][column] == board[row+1][column+1] == board[row+2][column+2]:
if board[row][column] == "x":
print("x wins!")
else:
print("o wins!")
并找到增加的行和减少的列:
for row in range(board_size-2):
for column in range(board_size-2):
if board[row][column+2] == board[row+1][column+1] == board[row+2][column]:
if board[row][column] == "x":
print("x wins!")
else:
print("o wins!")
当我们使用函数将所有这些组合在一起时,它看起来像这样:
#user inputs preferred board size
board_size = int(input("How big do you want your tic-tac-toe board to be?"))
board = []
# we make the board arrays
for row in range(board_size):
board.append([])
for column in range(board_size):
board[row].append([])
# returns who won the game
def game_state(board):
# horizontal wins?
for row in range(board_size):
for column in range(board_size-2):
if board[row][column] == board[row][column+1] == board[row][column+2]:
if board[row][column] == "x":
return "x wins!"
else:
return "o wins!"
# vertical wins?
for column in range(board_size):
for row in range(board_size-2):
if board[row][column] == board[row+1][column] == board[row+2][column]:
if board[row][column] == "x":
return "x wins!"
else:
return "o wins!"
# diagonal wins (aigu)?
for row in range(board_size-2):
for column in range(board_size-2):
if board[row][column+2] == board[row+1][column+1] == board[row+2][column]:
if board[row][column] == "x":
return "x wins!"
else:
return "o wins!"
# diagonal wins (grave)?
for row in range(board_size-2):
for column in range(board_size-2):
if board[row][column] == board[row+1][column+1] == board[row+2][column+2]:
if board[row][column] == "x":
return "x wins!"
else:
return "o wins!"
return "the game is still in progress"
'''
Here's where your game would run.
You would get people to play moves, and after each move, see if someone has won.
Once the board is filled up with moves (when number of moves == board_size**2), \
you will run game_state on the final position to see who won, or if it is a draw.
Good luck!
'''
添加回答
举报