用python设计一个字符界面人机对战的五子棋游戏,游戏难度中等,人机策略多元化
时间: 2024-05-09 18:15:18 浏览: 188
基于python实现五子棋人机对战
5星 · 资源好评率100%
好的,我可以为您提供一个五子棋游戏的代码示例。在这个游戏中,人类玩家将扮演黑棋,电脑玩家将扮演白棋。电脑玩家使用 Minimax 算法和 Alpha-Beta 剪枝来进行决策,并且可以选择不同的深度来调整难度级别。此外,您可以通过修改评估函数来调整电脑玩家的策略。
代码如下:
```python
import random
import copy
import os
# 棋盘的大小
BOARD_SIZE = 15
# 玩家执黑棋
BLACK = 1
# 电脑执白棋
WHITE = -1
# 空位置
EMPTY = 0
# 方向数组
DIRECTIONS = [(0, 1), (1, 0), (1, 1), (1, -1)]
# 评估函数
def evaluate(board, player):
# 检查每个位置的得分
score = [[0 for x in range(BOARD_SIZE)] for y in range(BOARD_SIZE)]
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
# 检查每个方向
for d in DIRECTIONS:
# 检查连续的五个位置
count = 0
for k in range(5):
x, y = i + k * d[0], j + k * d[1]
if x >= 0 and x < BOARD_SIZE and y >= 0 and y < BOARD_SIZE and board[x][y] == player:
count += 1
else:
break
if count == 5:
score[i][j] += 1000000
elif count > 0:
# 连续的棋子越多,得分越高
score[i][j] += count * count
# 计算总分
total_score = sum(map(sum, score))
# 如果是黑棋,则返回总分,否则返回相反数
return total_score if player == BLACK else -total_score
# 判断游戏是否结束
def game_over(board):
# 检查每个位置
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] != EMPTY:
# 检查每个方向
for d in DIRECTIONS:
# 检查连续的五个位置
count = 0
for k in range(5):
x, y = i + k * d[0], j + k * d[1]
if x >= 0 and x < BOARD_SIZE and y >= 0 and y < BOARD_SIZE and board[x][y] == board[i][j]:
count += 1
else:
break
if count == 5:
return True
return False
# 电脑玩家使用 Minimax 算法和 Alpha-Beta 剪枝来进行决策
def minimax(board, player, depth, alpha, beta):
# 如果达到最大深度或者游戏结束,则返回评估函数的值
if depth == 0 or game_over(board):
return evaluate(board, player)
# 如果是电脑玩家,则最大化评估函数的值
if player == WHITE:
best_score = float('-inf')
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
board[i][j] = WHITE
score = minimax(board, BLACK, depth - 1, alpha, beta)
board[i][j] = EMPTY
best_score = max(best_score, score)
alpha = max(alpha, best_score)
if beta <= alpha:
break
return best_score
# 如果是人类玩家,则最小化评估函数的值
else:
best_score = float('inf')
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
board[i][j] = BLACK
score = minimax(board, WHITE, depth - 1, alpha, beta)
board[i][j] = EMPTY
best_score = min(best_score, score)
beta = min(beta, best_score)
if beta <= alpha:
break
return best_score
# 电脑玩家决定下一步棋的位置
def computer_move(board, depth):
best_score = float('-inf')
best_move = None
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == EMPTY:
board[i][j] = WHITE
score = minimax(board, BLACK, depth, float('-inf'), float('inf'))
board[i][j] = EMPTY
if score > best_score:
best_score = score
best_move = (i, j)
return best_move
# 打印棋盘
def print_board(board):
print(" ", end="")
for i in range(BOARD_SIZE):
print(chr(ord('a') + i), end="")
print()
for i in range(BOARD_SIZE):
print(i + 1, end="")
for j in range(BOARD_SIZE):
if board[i][j] == BLACK:
print("●", end="")
elif board[i][j] == WHITE:
print("○", end="")
else:
print("┼", end="")
print()
# 主函数
def main():
# 初始化棋盘
board = [[EMPTY for x in range(BOARD_SIZE)] for y in range(BOARD_SIZE)]
# 游戏开始
while True:
print_board(board)
# 人类玩家下棋
while True:
move = input("请输入您下棋的位置(例如:a1):")
if len(move) == 2 and move[0] >= 'a' and move[0] < chr(ord('a') + BOARD_SIZE) and move[1] >= '1' and move[1] < chr(ord('1') + BOARD_SIZE):
i, j = ord(move[0]) - ord('a'), ord(move[1]) - ord('1')
if board[i][j] == EMPTY:
board[i][j] = BLACK
break
# 判断游戏是否结束
if game_over(board):
print_board(board)
print("您赢了!")
break
# 电脑玩家下棋
print("电脑玩家正在思考中...")
computer_i, computer_j = computer_move(board, depth=3)
board[computer_i][computer_j] = WHITE
# 判断游戏是否结束
if game_over(board):
print_board(board)
print("电脑玩家赢了!")
break
if __name__ == '__main__':
main()
```
该程序将输出一个字符界面,您可以通过输入坐标来下棋。电脑玩家将使用 Minimax 算法和 Alpha-Beta 剪枝来进行决策,并且可以选择不同的深度来调整难度级别。
阅读全文