用python写一个五子棋人机博弈系统
时间: 2023-11-14 10:21:37 浏览: 125
好的,下面是一个简单的五子棋人机博弈系统的Python代码,使用了简单的 MiniMax 算法和 Alpha-Beta 剪枝算法:
```python
import numpy as np
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义胜利条件
WIN_COUNT = 5
# 定义深度
DEPTH = 3
# 创建棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=int)
# 定义落子函数
def move(x, y, player):
if board[x][y] == EMPTY:
board[x][y] = player
return True
else:
return False
# 定义判断胜利函数
def is_win(x, y, player):
# 判断横向是否有五子连珠
count = 0
for i in range(max(0, x - WIN_COUNT + 1), min(BOARD_SIZE, x + WIN_COUNT)):
if board[i][y] == player:
count += 1
if count == WIN_COUNT:
return True
else:
count = 0
# 判断纵向是否有五子连珠
count = 0
for j in range(max(0, y - WIN_COUNT + 1), min(BOARD_SIZE, y + WIN_COUNT)):
if board[x][j] == player:
count += 1
if count == WIN_COUNT:
return True
else:
count = 0
# 判断左上到右下是否有五子连珠
count = 0
for i, j in zip(range(max(0, x - WIN_COUNT + 1), min(BOARD_SIZE, x + WIN_COUNT)),
range(max(0, y - WIN_COUNT + 1), min(BOARD_SIZE, y + WIN_COUNT))):
if board[i][j] == player:
count += 1
if count == WIN_COUNT:
return True
else:
count = 0
# 判断右上到左下是否有五子连珠
count = 0
for i, j in zip(range(max(0, x - WIN_COUNT + 1), min(BOARD_SIZE, x + WIN_COUNT)),
range(min(BOARD_SIZE - 1, y + WIN_COUNT - 1), max(-1, y - WIN_COUNT), -1)):
if board[i][j] == player:
count += 1
if count == WIN_COUNT:
return True
else:
count = 0
return False
# 定义评估函数
def evaluate(player):
score = 0
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
continue
elif board[i][j] == player:
score += 10
else:
score -= 10
return score
# 定义 MiniMax 算法
def minimax(depth, player, alpha, beta):
if depth == 0 or is_game_over():
return evaluate(player)
if player == BLACK:
best_score = -np.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
move(i, j, player)
score = minimax(depth - 1, WHITE, alpha, beta)
move(i, j, EMPTY)
best_score = max(best_score, score)
alpha = max(alpha, best_score)
if alpha >= beta:
break
return best_score
else:
best_score = np.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
move(i, j, player)
score = minimax(depth - 1, BLACK, alpha, beta)
move(i, j, EMPTY)
best_score = min(best_score, score)
beta = min(beta, best_score)
if alpha >= beta:
break
return best_score
# 定义 Alpha-Beta 剪枝算法
def alphabeta(depth, player, alpha, beta):
if depth == 0 or is_game_over():
return evaluate(player)
if player == BLACK:
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
move(i, j, player)
alpha = max(alpha, alphabeta(depth - 1, WHITE, alpha, beta))
move(i, j, EMPTY)
if alpha >= beta:
return alpha
return alpha
else:
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
move(i, j, player)
beta = min(beta, alphabeta(depth - 1, BLACK, alpha, beta))
move(i, j, EMPTY)
if alpha >= beta:
return beta
return beta
# 定义判断游戏是否结束函数
def is_game_over():
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
return False
return True
# 定义主函数
def main():
# 初始化玩家和落子位置
player = BLACK
x, y = -1, -1
# 游戏开始
print("游戏开始!")
while True:
# 输出棋盘
print(board)
# 玩家落子
if player == BLACK:
print("轮到玩家落子:")
x = int(input("请输入落子横坐标:"))
y = int(input("请输入落子纵坐标:"))
if not move(x, y, player):
print("此处已有棋子,请重新输入!")
continue
# AI 落子
else:
print("轮到 AI 落子:")
best_score = -np.inf
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
move(i, j, player)
score = alphabeta(DEPTH, BLACK, -np.inf, np.inf)
move(i, j, EMPTY)
if score > best_score:
best_score = score
x, y = i, j
move(x, y, player)
# 判断胜负
if is_win(x, y, player):
print("恭喜玩家 %s 获胜!" % ("黑方" if player == BLACK else "白方"))
print(board)
break
# 切换玩家
player = BLACK if player == WHITE else WHITE
if __name__ == "__main__":
main()
```
这个代码实现了一个简单的五子棋人机博弈系统,玩家可以与 AI 进行对弈,AI 使用 Alpha-Beta 剪枝算法进行决策。你可以根据需要进行修改和扩展,实现更加智能和完整的五子棋人机博弈系统。
阅读全文