python黑白棋双人对战代码
时间: 2023-11-20 08:56:55 浏览: 156
黑白棋的代码
4星 · 用户满意度95%
以下是基于pygame实现的黑白棋游戏AI代码,使用了Min-max算法和Alpha-Beta剪枝实现人工智能对手。代码中包含了游戏界面的绘制、游戏规则的判断、AI对手的实现等。
```python
import pygame
import sys
import time
import random
import copy
# 初始化pygame
pygame.init()
# 设置游戏界面大小
size = width, height = 640, 640
# 设置棋盘格子大小
grid_size = 80
# 设置棋盘大小
board_size = 8
# 设置棋子半径
piece_radius = int(grid_size / 2 - 5)
# 设置棋盘边缘留白大小
margin = int(grid_size / 2)
# 设置棋盘颜色
board_color = (0, 128, 0)
# 设置棋子颜色
black_color = (0, 0, 0)
white_color = (255, 255, 255)
# 设置字体
font = pygame.font.Font(None, 32)
# 设置AI搜索深度
search_depth = 3
# 定义棋盘类
class Board:
def __init__(self):
self.board = [[0 for i in range(board_size)] for j in range(board_size)]
self.board[3][3] = self.board[4][4] = 1
self.board[3][4] = self.board[4][3] = -1
# 获取当前棋盘状态
def get_board(self):
return self.board
# 获取当前棋盘上黑白棋子的数量
def get_piece_count(self):
black_count = 0
white_count = 0
for i in range(board_size):
for j in range(board_size):
if self.board[i][j] == 1:
black_count += 1
elif self.board[i][j] == -1:
white_count += 1
return black_count, white_count
# 判断当前位置是否可以落子
def is_valid_move(self, x, y, color):
if self.board[x][y] != 0:
return False
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)]
for d in directions:
if self.is_valid_direction(x, y, color, d):
return True
return False
# 判断当前位置在某个方向上是否可以落子
def is_valid_direction(self, x, y, color, d):
dx, dy = d
x += dx
y += dy
count = 0
while x >= 0 and x < board_size and y >= 0 and y < board_size:
if self.board[x][y] == -color:
count += 1
x += dx
y += dy
elif self.board[x][y] == color:
if count > 0:
return True
else:
return False
else:
return False
return False
# 落子
def make_move(self, x, y, color):
self.board[x][y] = color
directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)]
for d in directions:
self.make_flips(x, y, color, d)
# 在某个方向上翻转棋子
def make_flips(self, x, y, color, d):
dx, dy = d
x += dx
y += dy
count = 0
while x >= 0 and x < board_size and y >= 0 and y < board_size:
if self.board[x][y] == -color:
count += 1
x += dx
y += dy
elif self.board[x][y] == color:
for i in range(count):
x -= dx
y -= dy
self.board[x][y] = color
break
else:
break
# 获取当前可以落子的位置列表
def get_valid_moves(self, color):
valid_moves = []
for i in range(board_size):
for j in range(board_size):
if self.is_valid_move(i, j, color):
valid_moves.append((i, j))
return valid_moves
# 判断游戏是否结束
def is_game_over(self):
black_count, white_count = self.get_piece_count()
if black_count == 0 or white_count == 0 or black_count + white_count == board_size * board_size:
return True
if len(self.get_valid_moves(1)) == 0 and len(self.get_valid_moves(-1)) == 0:
return True
return False
# 获取当前棋盘上黑白双方的得分
def get_score(self):
black_score = 0
white_score = 0
for i in range(board_size):
for j in range(board_size):
if self.board[i][j] == 1:
black_score += 1
elif self.board[i][j] == -1:
white_score += 1
return black_score, white_score
# 定义游戏类
class Game:
def __init__(self):
self.board = Board()
self.current_player = 1
self.game_over = False
# 切换当前玩家
def switch_player(self):
self.current_player = -self.current_player
# 处理鼠标点击事件
def handle_click(self, x, y):
if self.current_player == 1:
if self.board.is_valid_move(x, y, self.current_player):
self.board.make_move(x, y, self.current_player)
self.switch_player()
self.check_game_over()
# 处理AI落子
def ai_move(self):
valid_moves = self.board.get_valid_moves(self.current_player)
if len(valid_moves) > 0:
best_move = self.get_best_move()
self.board.make_move(best_move[0], best_move[1], self.current_player)
self.switch_player()
self.check_game_over()
# 获取最佳落子位置
def get_best_move(self):
return self.minimax(self.board, search_depth, self.current_player)
# Min-max算法
def minimax(self, board, depth, color):
if depth == 0 or board.is_game_over():
return None, self.evaluate(board, color)
if color == 1:
best_score = -float('inf')
best_move = None
for move in board.get_valid_moves(color):
new_board = copy.deepcopy(board)
new_board.make_move(move[0], move[1], color)
score = self.minimax(new_board, depth - 1, -color)[1]
if score > best_score:
best_score = score
best_move = move
else:
best_score = float('inf')
best_move = None
for move in board.get_valid_moves(color):
new_board = copy.deepcopy(board)
new_board.make_move(move[0], move[1], color)
score = self.minimax(new_board, depth - 1, -color)[1]
if score < best_score:
best_score = score
best_move = move
return best_move, best_score
# Alpha-Beta剪枝
def alphabeta(self, board, depth, color, alpha, beta):
if depth == 0 or board.is_game_over():
return None, self.evaluate(board, color)
if color == 1:
best_score = -float('inf')
best_move = None
for move in board.get_valid_moves(color):
new_board = copy.deepcopy(board)
new_board.make_move(move[0], move[1], color)
score = self.alphabeta(new_board, depth - 1, -color, alpha, beta)[1]
if score > best_score:
best_score = score
best_move = move
alpha = max(alpha, best_score)
if alpha >= beta:
break
else:
best_score = float('inf')
best_move = None
for move in board.get_valid_moves(color):
new_board = copy.deepcopy(board)
new_board.make_move(move[0], move[1], color)
score = self.alphabeta(new_board, depth - 1, -color, alpha, beta)[1]
if score < best_score:
best_score = score
best_move = move
beta = min(beta, best_score)
if alpha >= beta:
break
return best_move, best_score
# 评估当前棋盘状态
def evaluate(self, board, color):
black_score, white_score = board.get_score()
if color == 1:
return black_score - white_score
else:
return white_score - black_score
# 检查游戏是否结束
def check_game_over(self):
if self.board.is_game_over():
self.game_over = True
# 绘制游戏界面
def draw(self, screen):
screen.fill(board_color
阅读全文