用python生成一段五子棋人机对战代码
时间: 2023-06-19 07:03:49 浏览: 115
基于python实现五子棋人机对战
5星 · 资源好评率100%
以下是一个简单的五子棋人机对战代码,使用了minimax算法和alpha-beta剪枝实现:
```python
import numpy as np
# 定义棋盘大小
SIZE = 15
# 定义棋子类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义搜索深度
SEARCH_DEPTH = 3
# 定义评估函数参数
SCORES = {
(0, 0): 7, (0, 1): 35, (0, 2): 800, (0, 3): 15000, (0, 4): 800000,
(1, 0): 7, (1, 1): 15, (1, 2): 400, (1, 3): 1800, (1, 4): 100000,
(2, 0): 7, (2, 1): 15, (2, 2): 200, (2, 3): 1000, (2, 4): 50000,
(3, 0): 7, (3, 1): 15, (3, 2): 100, (3, 3): 500, (3, 4): 25000,
(4, 0): 7, (4, 1): 15, (4, 2): 50, (4, 3): 200, (4, 4): 10000
}
# 初始化棋盘
board = np.zeros((SIZE, SIZE), dtype=int)
# 判断是否有五子连珠
def is_win(board, player):
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == player:
# 水平方向
if j + 4 < SIZE and board[i][j+1:j+5].tolist() == [player] * 4:
return True
# 竖直方向
elif i + 4 < SIZE and board[i+1:i+5, j].tolist() == [player] * 4:
return True
# 左上到右下方向
elif i + 4 < SIZE and j + 4 < SIZE and np.diag(board[i+1:i+5, j+1:j+5]).tolist() == [player] * 4:
return True
# 右上到左下方向
elif i - 4 >= 0 and j + 4 < SIZE and np.diag(np.fliplr(board[i-3:i+1, j+1:j+5])).tolist() == [player] * 4:
return True
return False
# 评估函数
def evaluate(board, player):
score = 0
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == player:
for dx, dy in [(0, 1), (1, 0), (1, 1), (1, -1)]:
cnt = 0
for k in range(1, 5):
x, y = i + k * dx, j + k * dy
if x < 0 or x >= SIZE or y < 0 or y >= SIZE or board[x][y] != player:
break
cnt += 1
score += SCORES.get((cnt, player), 0)
return score
# 极大极小值搜索
def search(board, player, depth, alpha, beta):
if depth == 0:
return None, evaluate(board, player)
best_move = None
if player == BLACK:
best_score = -np.inf
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == EMPTY:
board[i][j] = BLACK
_, score = search(board, WHITE, depth-1, alpha, beta)
board[i][j] = EMPTY
if score > best_score:
best_score = score
best_move = (i, j)
alpha = max(alpha, best_score)
if alpha >= beta:
return best_move, best_score
else:
best_score = np.inf
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == EMPTY:
board[i][j] = WHITE
_, score = search(board, BLACK, depth-1, alpha, beta)
board[i][j] = EMPTY
if score < best_score:
best_score = score
best_move = (i, j)
beta = min(beta, best_score)
if alpha >= beta:
return best_move, best_score
return best_move, best_score
# 人机对战
def play():
player = BLACK
while True:
if player == BLACK:
row, col = map(int, input('Your move (row, col): ').split())
if board[row][col] != EMPTY:
print('Invalid move!')
continue
board[row][col] = BLACK
else:
print('AI is thinking...')
move, _ = search(board, WHITE, SEARCH_DEPTH, -np.inf, np.inf)
row, col = move
board[row][col] = WHITE
print(f'AI moves ({row}, {col})')
print(board)
if is_win(board, player):
print('You win!' if player == BLACK else 'AI wins!')
break
if np.count_nonzero(board) == SIZE * SIZE:
print('Draw!')
break
player = WHITE if player == BLACK else BLACK
# 开始游戏
play()
```
使用该代码进行人机对战时,可以通过输入行列坐标来下棋子,例如:
```
Your move (row, col): 7 7
```
AI会自动思考并下出最优的落子位置,并输出到控制台上,例如:
```
AI is thinking...
AI moves (8, 8)
```
可以通过修改`SEARCH_DEPTH`来调整搜索深度,从而影响AI的棋力。但要注意,搜索深度过大会导致计算时间过长,影响游戏体验。
阅读全文