python 代码运用α-β剪枝算法实现五子棋小游戏
时间: 2023-07-31 19:11:36 浏览: 424
好的,我可以为您提供一个基于 Python 的五子棋小游戏实现,使用了 α-β剪枝算法来实现电脑的 AI 策略。
首先,为了实现五子棋,我们需要一个棋盘,可以使用二维数组来表示:
```python
board = [[0 for i in range(15)] for j in range(15)]
```
其中,0 表示该位置为空,1 表示该位置为黑子,2 表示该位置为白子。
接着,我们需要实现落子函数,来让玩家和电脑下棋:
```python
def move(board, row, col, player):
if board[row][col] != 0:
return False
board[row][col] = player
return True
```
其中,`player` 参数表示玩家或电脑,1 表示玩家,2 表示电脑。
接下来,我们需要实现电脑的 AI 策略,使用 α-β剪枝算法来优化搜索:
```python
def alphabeta(board, depth, alpha, beta, player):
if depth == 0:
return evaluate(board, player)
if player == 2:
best_score = -999999
for row in range(15):
for col in range(15):
if board[row][col] == 0:
board[row][col] = player
score = alphabeta(board, depth - 1, alpha, beta, 1)
board[row][col] = 0
best_score = max(best_score, score)
alpha = max(alpha, score)
if beta <= alpha:
break
return best_score
else:
best_score = 999999
for row in range(15):
for col in range(15):
if board[row][col] == 0:
board[row][col] = player
score = alphabeta(board, depth - 1, alpha, beta, 2)
board[row][col] = 0
best_score = min(best_score, score)
beta = min(beta, score)
if beta <= alpha:
break
return best_score
```
其中,`depth` 参数表示搜索的深度,`alpha`、`beta` 参数分别表示 α、β 值。
最后,我们需要实现评估函数,来评估当前棋盘的得分:
```python
def evaluate(board, player):
score = 0
for row in range(15):
for col in range(15):
if board[row][col] == player:
score += 1
return score
```
最后,我们可以将上述函数整合到一个完整的程序中,来实现五子棋小游戏:
```python
import random
board = [[0 for i in range(15)] for j in range(15)]
def move(board, row, col, player):
if board[row][col] != 0:
return False
board[row][col] = player
return True
def alphabeta(board, depth, alpha, beta, player):
if depth == 0:
return evaluate(board, player)
if player == 2:
best_score = -999999
for row in range(15):
for col in range(15):
if board[row][col] == 0:
board[row][col] = player
score = alphabeta(board, depth - 1, alpha, beta, 1)
board[row][col] = 0
best_score = max(best_score, score)
alpha = max(alpha, score)
if beta <= alpha:
break
return best_score
else:
best_score = 999999
for row in range(15):
for col in range(15):
if board[row][col] == 0:
board[row][col] = player
score = alphabeta(board, depth - 1, alpha, beta, 2)
board[row][col] = 0
best_score = min(best_score, score)
beta = min(beta, score)
if beta <= alpha:
break
return best_score
def evaluate(board, player):
score = 0
for row in range(15):
for col in range(15):
if board[row][col] == player:
score += 1
return score
def play():
while True:
print("请输入您要下棋的位置,用逗号分隔行列,如 1,1 表示第一行第一列:")
row, col = map(int, input().split(","))
if move(board, row - 1, col - 1, 1):
print("您下了一步棋,当前棋盘:")
for row in board:
print(row)
if evaluate(board, 1) >= 5:
print("您赢了!")
break
print("电脑正在思考,请稍等...")
best_score = -999999
best_row = -1
best_col = -1
for row in range(15):
for col in range(15):
if board[row][col] == 0:
board[row][col] = 2
score = alphabeta(board, 3, -999999, 999999, 1)
board[row][col] = 0
if score > best_score:
best_score = score
best_row = row
best_col = col
move(board, best_row, best_col, 2)
print("电脑下了一步棋,当前棋盘:")
for row in board:
print(row)
if evaluate(board, 2) >= 5:
print("电脑赢了!")
break
if __name__ == "__main__":
play()
```
在上述程序中,我们使用了 `play()` 函数来启动五子棋游戏,玩家先手,电脑后手,电脑使用 α-β剪枝算法来实现 AI 策略。
阅读全文