使用python写一个井字棋的必胜算法
时间: 2024-03-10 18:45:07 浏览: 331
python实现井字棋小游戏
5星 · 资源好评率100%
以下是一个简单的井字棋必胜算法的 Python 代码。这个算法基于 Minimax 算法和 Alpha-Beta 剪枝,用于计算计算机在给定井字棋局面下的最优下棋策略。
```python
import math
# 定义棋盘大小
BOARD_SIZE = 3
# 定义棋子枚举类型
class Piece:
EMPTY = 0
X = 1
O = 2
# 构建棋盘
def create_board():
return [[Piece.EMPTY for j in range(BOARD_SIZE)] for i in range(BOARD_SIZE)]
# 判断游戏是否结束
def game_over(board):
# 检查每一行是否有胜者
for i in range(BOARD_SIZE):
if board[i][0] != Piece.EMPTY and board[i][0] == board[i][1] and board[i][1] == board[i][2]:
return True
# 检查每一列是否有胜者
for j in range(BOARD_SIZE):
if board[0][j] != Piece.EMPTY and board[0][j] == board[1][j] and board[1][j] == board[2][j]:
return True
# 检查对角线是否有胜者
if board[0][0] != Piece.EMPTY and board[0][0] == board[1][1] and board[1][1] == board[2][2]:
return True
if board[0][2] != Piece.EMPTY and board[0][2] == board[1][1] and board[1][1] == board[2][0]:
return True
# 检查棋盘是否已经填满
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == Piece.EMPTY:
return False
return True
# 计算当前局势的分值
def evaluate(board):
# 检查每一行
for i in range(BOARD_SIZE):
if board[i][0] != Piece.EMPTY and board[i][0] == board[i][1] and board[i][1] == board[i][2]:
if board[i][0] == Piece.X:
return 1
else:
return -1
# 检查每一列
for j in range(BOARD_SIZE):
if board[0][j] != Piece.EMPTY and board[0][j] == board[1][j] and board[1][j] == board[2][j]:
if board[0][j] == Piece.X:
return 1
else:
return -1
# 检查对角线
if board[0][0] != Piece.EMPTY and board[0][0] == board[1][1] and board[1][1] == board[2][2]:
if board[0][0] == Piece.X:
return 1
else:
return -1
if board[0][2] != Piece.EMPTY and board[0][2] == board[1][1] and board[1][1] == board[2][0]:
if board[0][2] == Piece.X:
return 1
else:
return -1
# 没有胜者,返回0
return 0
# 极小化搜索
def min_search(board, alpha, beta):
if game_over(board):
return evaluate(board)
min_val = math.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == Piece.EMPTY:
board[i][j] = Piece.O
val = max_search(board, alpha, beta)
min_val = min(min_val, val)
board[i][j] = Piece.EMPTY
beta = min(beta, val)
if beta <= alpha:
break
return min_val
# 极大化搜索
def max_search(board, alpha, beta):
if game_over(board):
return evaluate(board)
max_val = -math.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == Piece.EMPTY:
board[i][j] = Piece.X
val = min_search(board, alpha, beta)
max_val = max(max_val, val)
board[i][j] = Piece.EMPTY
alpha = max(alpha, val)
if beta <= alpha:
break
return max_val
# 计算计算机的下一步
def computer_move(board):
best_val = -math.inf
best_move = None
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == Piece.EMPTY:
board[i][j] = Piece.X
val = min_search(board, -math.inf, math.inf)
board[i][j] = Piece.EMPTY
if val > best_val:
best_val = val
best_move = (i, j)
return best_move
# 使用示例
if __name__ == '__main__':
board = create_board()
# 计算机先手
print("计算机先手,玩家执 O,计算机执 X")
while not game_over(board):
print("当前局势:")
for row in board:
print(row)
# 玩家下棋
while True:
row = int(input("请输入行号(0-2):"))
col = int(input("请输入列号(0-2):"))
if board[row][col] == Piece.EMPTY:
board[row][col] = Piece.O
break
if game_over(board):
break
# 计算机下棋
row, col = computer_move(board)
board[row][col] = Piece.X
print("游戏结束!最终局势:")
for row in board:
print(row)
print("胜者是:", evaluate(board))
```
在上面的示例中,我们使用 Minimax 算法和 Alpha-Beta 剪枝来计算计算机的最优下棋策略,并通过与玩家进行交互来进行井字棋游戏。
阅读全文