Python井字棋问题:玩家获胜后AI仍落子、平局无提示
井字棋游戏问题修复方案
问题分析与修复思路
1. 玩家获胜后电脑仍落子
当前代码中,get_move方法会让玩家先落子,随后直接执行电脑的落子逻辑,没有在玩家落子后立即检查胜负。这导致玩家获胜后,电脑依然会继续操作。
修复方式:在玩家落子后,先调用check_winner检查是否获胜,若已产生赢家则终止当前回合,不再执行电脑的落子逻辑。
2. 平局时无提示且游戏不终止
check_winner方法中仅打印平局提示,但未返回特定标识,且play_game的循环仅判断是否有赢家,忽略了平局情况。这导致平局时游戏会继续循环,无法正常结束。
修复方式:让check_winner在平局时返回"TIE"标识,然后在play_game的循环中增加对平局的判断,终止游戏并展示结果。
修改后的完整代码
import random print("Welcome to tic-tac-toe. You will be battling with a dumb AI") class TicTacToe: # create the board def __init__(self): self.board = [] self.player = "" def player_choice(self): while True: choice = input("\nPlease Select if you want O or X \n1. O\n2. X\nEnter your choice: ") if choice == "1": print("You picked \"O\" ") self.player = "O" break elif choice == "2": print("You picked \"X\" ") self.player = "X" break else: print("Invalid Input") def create_board(self): for i in range(3): self.board.append(["-"] * 3) def print_board(self): for row in self.board: print("|".join(row)) # Check winner def check_winner(self): # check row for row in self.board: if row[0] == row[1] == row[2] and row[0] != "-": return row[0] # check column for col in range(3): if self.board[0][col] == self.board[1][col] == self.board[2][col] and self.board[0][col] != "-": return self.board[0][col] # check diagonal if self.board[0][0] == self.board[1][1] == self.board[2][2] and self.board[0][0] != "-": return self.board[0][0] if self.board[0][2] == self.board[1][1] == self.board[2][0] and self.board[0][2] != "-": return self.board[0][2] # check for tie if "-" not in self.board[0] and "-" not in self.board[1] and "-" not in self.board[2]: return "TIE" # no winner no tie return None def get_move(self): print("You are using " + self.player) if self.player == "X": while True: row = int(input("Input Row: ")) col = int(input("Input Column: ")) if row < 0 or row >= len(self.board) or col < 0 or col >= len(self.board[0]): print("out of Bounds") elif self.board[row][col] == "-": self.board[row][col] = self.player break else: print("Someone is already in that spot!") # 玩家落子后立即检查是否获胜 winner = self.check_winner() if winner: return winner print("Player O (computer) is making a move...") row, col = random.randint(0, 2), random.randint(0, 2) while self.board[row][col] != "-": row, col = random.randint(0, 2), random.randint(0, 2) print(f"Player O chooses ({row + 1},{col + 1})") self.board[row][col] = "O" elif self.player == "O": while True: row = int(input("Input Row: ")) col = int(input("Input Column: ")) if row < 0 or row >= len(self.board) or col < 0 or col >= len(self.board[0]): print("out of Bounds") elif self.board[row][col] == "-": self.board[row][col] = self.player break else: print("Someone is already in that spot!") # 玩家落子后立即检查是否获胜 winner = self.check_winner() if winner: return winner print("Player X (computer) is making a move...") row, col = random.randint(0, 2), random.randint(0, 2) while self.board[row][col] != "-": row, col = random.randint(0, 2), random.randint(0, 2) print(f"Player X chooses ({row + 1},{col + 1})") self.board[row][col] = "X" # 电脑落子后检查胜负 return self.check_winner() def play_game(self): self.player_choice() self.create_board() while True: self.print_board() result = self.get_move() if result: self.print_board() if result == "TIE": print("It's a tie!") else: print(f"Player {result} wins!") break game = TicTacToe() game.play_game()
关键修改说明
check_winner方法:将平局判断的print改为返回"TIE",让调用方可以识别平局状态。get_move方法:- 玩家落子后立即调用
check_winner,若已有赢家则返回结果,跳过电脑落子步骤。 - 电脑落子后也调用
check_winner,返回当前的胜负状态。
- 玩家落子后立即调用
play_game方法:接收get_move返回的结果,同时判断赢家和平局情况,终止游戏并打印对应提示。
内容的提问来源于stack exchange,提问作者Hellscythe
相关产品推荐
相关产品推荐

