【发布时间】:2020-08-13 00:54:50
【问题描述】:
我编写了一个可以运行的井字游戏程序,但我一直在思考如何编写 chekDraw 函数(检查游戏是否平局)。所有其他功能都在工作,我可以玩游戏。我的想法是,for in range 循环将遍历棋盘索引,计算 x 和 o,直到整个棋盘都满了。总的来说,我的条件是,如果它不是胜利(checkWin),那么它就是平局。我已经盯着这个看了这么久,我希望对我的代码有新的看法。任何见解/建议将不胜感激!
编辑:特别是 checkDraw 发生的事情是什么 - 如果棋盘已满且没有赢家,游戏会不断要求移动,但此时任何移动都是非法的,因为所有位置都已被占用(来自 getMove 函数的验证) .
# display instructions
def displayInstructions():
print()
print("This is a game of Tic-Tac-Toe. You will select an empty location")
print("and enter its index to select a move. The first player will be X")
print("and the second player will be O.")
print()
# display current state
# pass in board
# does not return anything
def showBoard(board):
for i in range(len(board)):
print("[" + str(board[i]) + "]", end="")
if i == 2 or i == 5 or i == 8:
print(end="\n")
# pass in board
# return updated board
# must validate move (in range, unoccupied square)
def getMove(board, player):
validMove = False
while not validMove:
move = input("{0}, what is your move: ".format(player))
position = int(move) - 1 # cast input as an integer, and puts player move in correct index
# in range
if position < 0 or position > 8:
print("That is an illegal move.")
# unoccupied square
if board[position] == "X" or board[position] == "O":
print("That is an illegal move.")
else:
# if valid move, put player on board
board[position] = player
return board
def checkWin(board, player):
if (board[0] == player and board[1] == player and board[2] == player) or \
(board[3] == player and board[4] == player and board[5] == player) or \
(board[6] == player and board[7] == player and board[8] == player) or \
(board[0] == player and board[3] == player and board[6] == player) or \
(board[1] == player and board[4] == player and board[7] == player) or \
(board[2] == player and board[5] == player and board[8] == player) or \
(board[0] == player and board[4] == player and board[8] == player) or \
(board[2] == player and board[4] == player and board[6] == player):
return True
else:
return False
def checkDraw(board, player):
count = 0
for i in range(len(board)):
if board[i] == player:
# if board[i] == "X" or board[i) == "O"
count += 1
if count == len(board):
return True
def main():
# Repeat play loop
playGame = True
while playGame:
# output instructions
displayInstructions()
# initialize board
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(len(board))
# initialize first player to X
player = "X"
# play until win or draw
while not checkWin(board, player) or checkDraw(board, player):
showBoard(board)
getMove(board, player)
checkWin(board, player)
checkDraw(board, player)
# if the game is in play (not a win or draw)
if not checkWin(board, player) or checkDraw(board, player):
# swap player
if player == "X":
player = "O"
else:
player = "X"
# if win
if checkWin(board, player):
showBoard(board)
print("{0} wins.".format(player))
playGame = False
# if draw
elif checkDraw(board, player):
showBoard(board)
print("It's a draw")
playGame = False
# Ask if want to play another game
playAgain = input("Would you like to play again? (y/n): ").lower()
if playAgain == "y":
main()
else:
print("Goodbye.")
if __name__ == "__main__":
main()
【问题讨论】:
-
您在尝试实现 chekDraw 功能时遇到了哪些具体问题?
-
我的建议是记录棋盘上有多少个 X/O,如果没有人赢,有 9 个棋子,那就是平局
-
我建议将板子初始化为 ~None~ 的列表。然后,当您检查棋盘时,您可以说~for I in board: if i is None: return False~ 然后当您检查有效动作时,您可以确保该点尚未使用!
-
感谢您的建议!我会试试看。
标签: python tic-tac-toe