基于α-β剪枝算法的五子棋游戏代码
时间: 2023-09-11 18:06:59 浏览: 192
以下是一个基于α-β剪枝算法的五子棋游戏代码示例(Python实现):
```python
import random
# 定义棋盘的大小
BOARD_SIZE = 15
# 定义棋子的类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义棋子在棋盘上的表示方式
PIECE_STR = ['.', 'X', 'O']
# 定义棋盘
board = [[EMPTY for x in range(BOARD_SIZE)] for y in range(BOARD_SIZE)]
# 定义α-β剪枝算法中的最大深度
MAX_DEPTH = 3
# 定义评估函数中的权重
SCORE_WEIGHTS = [0, 1, 10, 100, 1000, 10000]
def print_board():
"""打印棋盘"""
print(" ", end="")
for i in range(BOARD_SIZE):
print("{:2d}".format(i), end="")
print()
for i in range(BOARD_SIZE):
print("{:2d}".format(i), end="")
for j in range(BOARD_SIZE):
print(" " + PIECE_STR[board[i][j]], end="")
print()
def get_winner():
"""获取胜者"""
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if board[x][y] == EMPTY:
continue
if y + 4 < BOARD_SIZE and \
board[x][y] == board[x][y + 1] == board[x][y + 2] == board[x][y + 3] == board[x][y + 4]:
return board[x][y]
if x + 4 < BOARD_SIZE and \
board[x][y] == board[x + 1][y] == board[x + 2][y] == board[x + 3][y] == board[x + 4][y]:
return board[x][y]
if x + 4 < BOARD_SIZE and y + 4 < BOARD_SIZE and \
board[x][y] == board[x + 1][y + 1] == board[x + 2][y + 2] == board[x + 3][y + 3] == board[x + 4][y + 4]:
return board[x][y]
if x + 4 < BOARD_SIZE and y - 4 >= 0 and \
board[x][y] == board[x + 1][y - 1] == board[x + 2][y - 2] == board[x + 3][y - 3] == board[x + 4][y - 4]:
return board[x][y]
return EMPTY
def get_score(piece_type):
"""计算当前棋盘的得分"""
score = 0
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if board[x][y] != piece_type:
continue
for direction_x, direction_y in [(1, 0), (0, 1), (1, 1), (1, -1)]:
cnt = 1
for i in range(1, 5):
new_x, new_y = x + i * direction_x, y + i * direction_y
if new_x >= BOARD_SIZE or new_y >= BOARD_SIZE or board[new_x][new_y] != piece_type:
break
cnt += 1
score += SCORE_WEIGHTS[cnt]
return score
def alpha_beta_pruning(piece_type, depth, alpha, beta):
"""α-β剪枝算法"""
winner = get_winner()
if winner == piece_type:
return 1000000
if winner != EMPTY:
return -1000000
if depth == MAX_DEPTH:
return get_score(piece_type) - get_score(3 - piece_type)
best_score = -float("inf") if piece_type == BLACK else float("inf")
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if board[x][y] != EMPTY:
continue
board[x][y] = piece_type
score = alpha_beta_pruning(3 - piece_type, depth + 1, alpha, beta)
board[x][y] = EMPTY
if piece_type == BLACK:
best_score = max(best_score, score)
alpha = max(alpha, score)
else:
best_score = min(best_score, score)
beta = min(beta, score)
if alpha >= beta:
return best_score
return best_score
def ai_make_move():
"""AI下棋"""
best_moves = []
best_score = -float("inf")
for x in range(BOARD_SIZE):
for y in range(BOARD_SIZE):
if board[x][y] != EMPTY:
continue
board[x][y] = BLACK
score = alpha_beta_pruning(WHITE, 1, -float("inf"), float("inf"))
board[x][y] = EMPTY
if score > best_score:
best_score = score
best_moves = [(x, y)]
elif score == best_score:
best_moves.append((x, y))
return random.choice(best_moves)
if __name__ == "__main__":
while True:
print_board()
winner = get_winner()
if winner != EMPTY:
print("游戏结束,胜者是:", PIECE_STR[winner])
break
x, y = map(int, input("请下棋(x y):").split())
if board[x][y] != EMPTY:
print("非法落子,请重新输入")
continue
board[x][y] = WHITE
x, y = ai_make_move()
print("AI下棋:", x, y)
board[x][y] = BLACK
```
这个代码实现了一个简单的五子棋游戏,使用了α-β剪枝算法来实现AI下棋。其中,`get_winner`函数用来判断胜负,`get_score`函数用来评估当前棋盘的得分,`alpha_beta_pruning`函数用来执行α-β剪枝算法,`ai_make_move`函数用来让AI下棋。
阅读全文