Python 创建一个简单的 Tic-Tac-Toe 游戏类
Tic-Tac-Toe(井字棋)是一个经典的双人游戏,玩家轮流在 3x3 的网格上放置自己的标记(通常是 "X" 和 "O")。游戏的目标是在水平、垂直或对角线上先形成一条连续的三个相同标记的线。下面我们将创建一个简单的 Tic-Tac-Toe 游戏类,包含基本的游戏逻辑和显示功能。
实例
class TicTacToe:
def __init__(self):
self.board = [[' ' for _ in range(3)] for _ in range(3)]
self.current_player = 'X'
def print_board(self):
for row in self.board:
print('|'.join(row))
print('-' * 5)
def make_move(self, row, col):
if self.board[row][col] == ' ':
self.board[row][col] = self.current_player
self.current_player = 'O' if self.current_player == 'X' else 'X'
else:
print("Invalid move! The cell is already occupied.")
def check_winner(self):
# Check rows
for row in self.board:
if row[0] == row[1] == row[2] != ' ':
return row[0]
# Check columns
for col in range(3):
if self.board[0][col] == self.board[1][col] == self.board[2][col] != ' ':
return self.board[0][col]
# Check diagonals
if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ':
return self.board[0][0]
if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ':
return self.board[0][2]
# Check for a tie
if all(cell != ' ' for row in self.board for cell in row):
return 'Tie'
return None
def play_game(self):
while True:
self.print_board()
print(f"Player {self.current_player}'s turn")
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
self.make_move(row, col)
winner = self.check_winner()
if winner:
self.print_board()
if winner == 'Tie':
print("It's a tie!")
else:
print(f"Player {winner} wins!")
break
def __init__(self):
self.board = [[' ' for _ in range(3)] for _ in range(3)]
self.current_player = 'X'
def print_board(self):
for row in self.board:
print('|'.join(row))
print('-' * 5)
def make_move(self, row, col):
if self.board[row][col] == ' ':
self.board[row][col] = self.current_player
self.current_player = 'O' if self.current_player == 'X' else 'X'
else:
print("Invalid move! The cell is already occupied.")
def check_winner(self):
# Check rows
for row in self.board:
if row[0] == row[1] == row[2] != ' ':
return row[0]
# Check columns
for col in range(3):
if self.board[0][col] == self.board[1][col] == self.board[2][col] != ' ':
return self.board[0][col]
# Check diagonals
if self.board[0][0] == self.board[1][1] == self.board[2][2] != ' ':
return self.board[0][0]
if self.board[0][2] == self.board[1][1] == self.board[2][0] != ' ':
return self.board[0][2]
# Check for a tie
if all(cell != ' ' for row in self.board for cell in row):
return 'Tie'
return None
def play_game(self):
while True:
self.print_board()
print(f"Player {self.current_player}'s turn")
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
self.make_move(row, col)
winner = self.check_winner()
if winner:
self.print_board()
if winner == 'Tie':
print("It's a tie!")
else:
print(f"Player {winner} wins!")
break
代码解析:
__init__
方法初始化了一个 3x3 的空棋盘,并将当前玩家设置为 "X"。print_board
方法用于打印当前的棋盘状态。make_move
方法允许玩家在指定的行和列上放置标记,并切换当前玩家。check_winner
方法检查是否有玩家获胜或是否平局。它检查所有行、列和对角线是否有三个相同的标记,并返回获胜的玩家或 "Tie" 表示平局。play_game
方法是游戏的主循环,允许玩家轮流输入行和列来下棋,直到有玩家获胜或平局。
输出结果:
运行 play_game
方法后,游戏会开始,玩家可以轮流输入行和列来下棋。游戏会持续进行,直到有玩家获胜或平局。例如:
| | ----- | | ----- | | ----- Player X's turn Enter row (0, 1, 2): 0 Enter column (0, 1, 2): 0 X| | ----- | | ----- | | ----- Player O's turn Enter row (0, 1, 2): 1 Enter column (0, 1, 2): 1 X| | ----- |O| ----- | | ----- ...
游戏会持续进行,直到有玩家获胜或平局。
点我分享笔记