Python实现简单的井字棋小游戏
井字棋是一种非常经典的小游戏,下面将详细讲解怎样使用 Python 实现一个简单的井字棋小游戏。
游戏规则
井字棋是由两个玩家轮流在一个3x3网格上放置棋子(通常是X和O),先将三个棋子排列成横线、竖线或斜线的玩家获胜。
实现步骤
- 定义游戏棋盘
- 定义棋手 (player)
- 输出棋盘并让玩家输入下棋位置
- 检查胜利和平局
定义游戏棋盘
我们可以使用一个二维列表来表示棋盘,每个元素代表一个棋子。
board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
]
定义棋手
在定义棋手时,我们可以使用一个简单的类来表示。棋手具有下棋行为并且可以设置棋子的形状。
class Player:
def __init__(self, name, symbol):
self.name = name
self.symbol = symbol
def move(self, board):
print(f"{self.name}, it's your turn!")
x = int(input("Enter the row(1-3): "))
y = int(input("Enter the column(1-3): "))
if x < 1 or x > 3 or y < 1 or y > 3:
print("Invalid move! Please enter the row and column between 1 and 3.")
return self.move(board)
if board[x-1][y-1] != " ":
print("Invalid move! The spot is already taken.")
return self.move(board)
board[x-1][y-1] = self.symbol
输出棋盘并让玩家输入下棋位置
在每个轮次下棋前,我们需要输出当前棋盘以便玩家选择下棋位置。
def print_board(board):
for i in range(3):
for j in range(3):
if j != 2:
print(f" {board[i][j]} ", end="|")
else:
print(f" {board[i][j]} ")
if i != 2:
print("-----------")
检查胜利和平局
我们需要在每个轮次下棋后检查是否有一个玩家获得胜利或者所有的位置已经被占据。我们可以使用一个函数来判断游戏是否结束。
def is_win(board, symbol):
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] == symbol: # 横向匹配
return True
if board[0][i] == board[1][i] == board[2][i] == symbol: # 纵向匹配
return True
if board[0][0] == board[1][1] == board[2][2] == symbol: # / 对角线匹配
return True
if board[0][2] == board[1][1] == board[2][0] == symbol: # \ 对角线匹配
return True
return False
def game_over(board):
if is_win(board, "X"):
print_board(board)
print("Congratulations! The winner is X")
return True
elif is_win(board, "O"):
print_board(board)
print("Congratulations! The winner is O")
return True
elif all(board[i][j] != " " for i in range(3) for j in range(3)):
print_board(board)
print("It's a tie!")
return True
return False
游戏流程
将以上功能结合起来,就能够实现一个简单的井字棋小游戏了。
def tic_tac_toe():
board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
]
players = [Player("Player X", "X"), Player("Player O", "O")]
current_player = players[0]
while True:
print_board(board)
current_player.move(board)
if game_over(board):
break
# 切换玩家
current_player = players[1] if current_player == players[0] else players[0]
if __name__ == "__main__":
tic_tac_toe()
示例说明
示例一
下面是一局井字棋游戏的示例输出:
| |
-----------
| |
-----------
| |
Player X, it's your turn! # X玩家的输入
Enter the row(1-3): 2
Enter the column(1-3): 2
| |
-----------
| X |
-----------
| |
Player O, it's your turn! # O玩家的输入
Enter the row(1-3): 1
Enter the column(1-3): 2
| O |
-----------
| X |
-----------
| |
Player X, it's your turn! # X玩家的输入
Enter the row(1-3): 3
Enter the column(1-3): 3
| O |
-----------
| X |
-----------
| | X
Player O, it's your turn! # O玩家的输入
Enter the row(1-3): 1
Enter the column(1-3): 1
O | O |
-----------
| X |
-----------
| | X
Player X, it's your turn! # X玩家的输入
Enter the row(1-3): 1
Enter the column(1-3): 3
O | O | X
-----------
| X |
-----------
| | X
Player O, it's your turn! # O玩家的输入
Enter the row(1-3): 3
Enter the column(1-3): 1
O | O | X
-----------
| X |
-----------
O | | X
Player X, it's your turn! # X玩家的输入
Enter the row(1-3): 2
Enter the column(1-3): 1
O | O | X
-----------
X | X |
-----------
O | | X
Congratulations! The winner is X # X玩家胜利
示例二
下面的示例说明了当一个玩家尝试占据已经被另一个玩家占据的位置时,程序会提示玩家重新输入:
| |
-----------
| |
-----------
| |
Player X, it's your turn!
Enter the row(1-3): 2 # X玩家选择(2, 2)
Enter the column(1-3): 2
| |
-----------
| X |
-----------
| |
Player O, it's your turn!
Enter the row(1-3): 2 # O玩家选择(2, 2),但是该位置已被占据
Enter the column(1-3): 2
Invalid move! The spot is already taken.
Enter the row(1-3): 1 # O玩家选择另外一个位置(1, 3)
Enter the column(1-3): 3
| |
-----------
O | X |
-----------
| |
Player X, it's your turn!
Enter the row(1-3): 1
Enter the column(1-3): 3
X | | X
-----------
O | X |
-----------
| |
Player O, it's your turn!
Enter the row(1-3): 4
Enter the column(1-3): 4 # 输入超出范围(1-3)
Invalid move! Please enter the row and column between 1 and 3.
Enter the row(1-3): 1
Enter the column(1-3): 1
O | | X
-----------
O | X |
-----------
| |
Player X, it's your turn!
Enter the row(1-3): 2
Enter the column(1-3): 1
O | | X
-----------
O | X |
-----------
X | |
Player O, it's your turn!
Enter the row(1-3): 2
Enter the column(1-3): 3
O | | X
-----------
O | X | O
-----------
X | |
Player X, it's your turn!
Enter the row(1-3): 3
Enter the column(1-3): 3
O | | X
-----------
O | X | O
-----------
X | | X
It's a tie!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python实现简单的井字棋小游戏 - Python技术站