五子棋博弈系统python
时间: 2023-10-08 08:12:00 浏览: 174
五子棋AI python实现
5星 · 资源好评率100%
实现五子棋博弈系统,可以按照以下步骤进行:
1. 安装 Pygame 模块,可以使用 pip install pygame 命令进行安装。
2. 创建一个棋盘,可以使用 Pygame 的绘图功能进行绘制。
3. 实现棋子的下落,可以使用鼠标点击棋盘的位置进行下落。
4. 实现胜负判断,可以在每次下落后判断是否有五个连续的棋子。
5. 实现人机对战,可以使用 Minimax 算法进行实现。
以下是一个简单的五子棋博弈系统的 Python 代码示例:
```python
import pygame
# 棋盘大小
BOARD_SIZE = 15
# 棋子大小
PIECE_SIZE = 30
# 棋盘边缘留白
MARGIN = 50
# 棋盘和棋子颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PIECE_COLOR = (0, 0, 0)
class ChessBoard:
def __init__(self):
self.board = [[0] * BOARD_SIZE for _ in range(BOARD_SIZE)]
self.turn = 1
self.game_over = False
def drop_piece(self, row, col):
if self.board[row][col] == 0:
self.board[row][col] = self.turn
self.turn = 3 - self.turn
return True
else:
return False
def check_winner(self, row, col):
if self.check_horizontal(row, col) or self.check_vertical(row, col) or \
self.check_diagonal(row, col) or self.check_antidiagonal(row, col):
self.game_over = True
return self.board[row][col]
else:
return 0
def check_horizontal(self, row, col):
count = 0
for c in range(col - 4, col + 5):
if c < 0 or c >= BOARD_SIZE:
continue
if self.board[row][c] == self.board[row][col]:
count += 1
if count == 5:
return True
else:
count = 0
return False
def check_vertical(self, row, col):
count = 0
for r in range(row - 4, row + 5):
if r < 0 or r >= BOARD_SIZE:
continue
if self.board[r][col] == self.board[row][col]:
count += 1
if count == 5:
return True
else:
count = 0
return False
def check_diagonal(self, row, col):
count = 0
for i in range(-4, 5):
r = row + i
c = col + i
if r < 0 or r >= BOARD_SIZE or c < 0 or c >= BOARD_SIZE:
continue
if self.board[r][c] == self.board[row][col]:
count += 1
if count == 5:
return True
else:
count = 0
return False
def check_antidiagonal(self, row, col):
count = 0
for i in range(-4, 5):
r = row + i
c = col - i
if r < 0 or r >= BOARD_SIZE or c < 0 or c >= BOARD_SIZE:
continue
if self.board[r][c] == self.board[row][col]:
count += 1
if count == 5:
return True
else:
count = 0
return False
class Game:
def __init__(self):
self.board = ChessBoard()
self.player = 1
self.computer = 2
def run(self):
pygame.init()
screen = pygame.display.set_mode((BOARD_SIZE * PIECE_SIZE + 2 * MARGIN, BOARD_SIZE * PIECE_SIZE + 2 * MARGIN))
pygame.display.set_caption("五子棋")
font = pygame.font.SysFont(None, 48)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.MOUSEBUTTONDOWN and not self.board.game_over:
if self.player == self.board.turn:
pos = pygame.mouse.get_pos()
col = (pos[0] - MARGIN) // PIECE_SIZE
row = (pos[1] - MARGIN) // PIECE_SIZE
if self.board.drop_piece(row, col):
winner = self.board.check_winner(row, col)
if winner == self.player:
text = font.render("你赢了!", True, BLACK)
screen.blit(text, (MARGIN, MARGIN // 2))
elif winner == self.computer:
text = font.render("你输了!", True, BLACK)
screen.blit(text, (MARGIN, MARGIN // 2))
if self.board.turn == self.computer and not self.board.game_over:
row, col = self.minimax(3, -float("inf"), float("inf"), True)
self.board.drop_piece(row, col)
winner = self.board.check_winner(row, col)
if winner == self.player:
text = font.render("你赢了!", True, BLACK)
screen.blit(text, (MARGIN, MARGIN // 2))
elif winner == self.computer:
text = font.render("你输了!", True, BLACK)
screen.blit(text, (MARGIN, MARGIN // 2))
screen.fill(WHITE)
for r in range(BOARD_SIZE):
for c in range(BOARD_SIZE):
color = PIECE_COLOR if self.board.board[r][c] != 0 else WHITE
pygame.draw.circle(screen, color, (c * PIECE_SIZE + MARGIN, r * PIECE_SIZE + MARGIN), PIECE_SIZE // 2)
pygame.display.flip()
def minimax(self, depth, alpha, beta, max_player):
if self.board.game_over or depth == 0:
return (-1, -1), self.evaluate()
if max_player:
max_pos = (-1, -1)
max_eval = -float("inf")
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
if self.board.board[row][col] == 0:
self.board.board[row][col] = self.computer
eval = self.minimax(depth - 1, alpha, beta, False)[1]
self.board.board[row][col] = 0
if eval > max_eval:
max_pos = (row, col)
max_eval = eval
alpha = max(alpha, eval)
if beta <= alpha:
break
return max_pos, max_eval
else:
min_pos = (-1, -1)
min_eval = float("inf")
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
if self.board.board[row][col] == 0:
self.board.board[row][col] = self.player
eval = self.minimax(depth - 1, alpha, beta, True)[1]
self.board.board[row][col] = 0
if eval < min_eval:
min_pos = (row, col)
min_eval = eval
beta = min(beta, eval)
if beta <= alpha:
break
return min_pos, min_eval
def evaluate(self):
score = 0
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
if self.board.board[row][col] == self.computer:
score += self.evaluate_position(row, col)
elif self.board.board[row][col] == self.player:
score -= self.evaluate_position(row, col)
return score
def evaluate_position(self, row, col):
score = 0
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
if r >= 0 and r < BOARD_SIZE and c >= 0 and c < BOARD_SIZE and (r != row or c != col):
if self.board.board[r][c] == self.computer:
score += 1
elif self.board.board[r][c] == self.player:
score -= 1
return score
if __name__ == "__main__":
game = Game()
game.run()
```
运行以上代码,即可开始五子棋博弈,可以使用鼠标进行下棋,也可以与 AI 进行对战。
阅读全文