我从哪里开始用python(带有GUI)制作的国际象棋游戏



我不是在问具体的代码,而是一般的结构,以及如何将python国际象棋库与pygame库与允许玩家和AI移动的代码链接起来。

关于从哪里开始有什么建议吗?

我对python象棋引擎很感兴趣。现在我正在使用python国际象棋库编写自己的程序
目前,它的水平很差,但我希望我能做一个更好的人工智能。我正在改变评估功能
我使用pygame和维基百科图像创建了一个GUI([在此处输入链接描述][1]。这个吟游诗人很容易在维基百科上找到。
我制作了3个不同的代码:一个用于引擎,一个用于玩电脑,还有一个用于观看两台电脑。我将向您展示我的引擎(正在构建中(,您应该想象一下如何使用它。
engine.py:

import pygame
import chess
import random
screen = pygame.display.set_mode((480, 480))
pygame.display.set_caption("Chess Engine")
screen.fill("WHITE")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
chessboard = pygame.image.load('graph/chessboard.png').convert_ alpha()
King = pygame.image.load('graph/WKing.png').convert_alpha()
king = pygame.image.load('graph/BKing.png').convert_alpha()
Knight = pygame.image.load('graph/WKnight.png').convert_alpha()
knight = pygame.image.load('graph/BKnight.png').convert_alpha()
Rook = pygame.image.load('graph/WRook.png').convert_alpha()
rook = pygame.image.load('graph/BRook.png').convert_alpha()
Queen = pygame.image.load('graph/WQueen.png').convert_alpha()
queen = pygame.image.load('graph/BQueen.png').convert_alpha()
Bishop = pygame.image.load('graph/WBishop.png').convert_alpha()
bishop = pygame.image.load('graph/BBishop.png').convert_alpha()
Pawn = pygame.image.load('graph/WPawn.png').convert_alpha()
pawn = pygame.image.load('graph/BPawn.png').convert_alpha()

def draw(fen, col, rank, board) :
if fen == 'K' :
if board.turn == chess.WHITE :
if board.is_check() :
pygame.draw.circle(screen, RED, (rank + 30, col + 30), 30)
screen.blit(King, (rank, col))
elif fen == 'k' :
if board.turn == chess.BLACK :
if board.is_check() :
pygame.draw.circle(screen, RED, (rank + 30, col + 30), 30)
screen.blit(king, (rank, col))
elif fen == 'Q' :
screen.blit(Queen, (rank, col))
elif fen == 'q' :
screen.blit(queen, (rank, col))
elif fen == 'R' :
screen.blit(Rook, (rank, col))
elif fen == 'r' :
screen.blit(rook, (rank, col))
elif fen == 'N' :
screen.blit(Knight, (rank, col))
elif fen == 'n' :
screen.blit(knight, (rank, col))
elif fen == 'B' :
screen.blit(Bishop, (rank, col))
elif fen == 'b' :
screen.blit(bishop, (rank, col))
elif fen == 'P' :
screen.blit(Pawn, (rank, col))
elif fen == 'p' :
screen.blit(pawn, (rank, col))
def show(FEN, board) :
screen.blit(chessboard, (0, 0))
col = 0
rank = 0
for fen in FEN :
if fen == '/' :
col = col + 1
rank = 0
elif fen in ('1', '2', '3', '4', '5', '6', '7', '8') :
rank = rank + int(fen)
elif fen in ('K', 'k', 'Q', 'q', 'R', 'r', 'N', 'n', 'B', 'b', 'P', 'p') :
draw(fen, col*60, rank*60, board)
rank = rank + 1
pygame.display.update() 
infinity = 1000000000
def piece_list(pos) :
'''Renvoie la liste des pièces présentes sur l'échiquier.'''
piece_liste = []
for place in chess.SQUARES :
if pos.piece_at(place) != None :
piece_liste.append(pos.piece_at(place).symbol())
return piece_liste
def game_is_finished(pos) :
'''Détermine si la partie est finie.'''
if pos.is_checkmate() or pos.is_stalemate() or pos.is_insufficient_material() or pos.can_claim_threefold_repetition():
return True
else :
return False
def evaluate(pos) :
'''Évaluation d'une position en fonction d'une position et de la mobillité des pièces.'''
if pos.is_checkmate() :
return -infinity
elif game_is_finished(pos) :
return 0
piece_liste = piece_list(pos)
evaluation = 0
opponent_legal_moves = []
current_player_legal_moves = [move for move in pos.legal_moves]
for i in piece_liste :
if i == 'Q' :
evaluation = evaluation + 9
elif i == 'q' :
evaluation = evaluation - 9
elif i == 'R' :
evaluation = evaluation + 5
elif i == 'r' :
evaluation = evaluation - 5
elif i == 'N' :
evaluation = evaluation + 3
elif i == 'n' :
evaluation = evaluation - 3
elif i == 'B' :
evaluation = evaluation + 3
elif i == 'b' :
evaluation = evaluation - 3
elif i == 'P' :
evaluation = evaluation + 1
elif i == 'p' :
evaluation = evaluation - 1
for i in range(len(current_player_legal_moves)-1) :
pos.push_san(str(current_player_legal_moves[i]))
opponent_legal_moves.append(pos.legal_moves.count())
if pos.is_checkmate() :
pos.pop()
return infinity
elif game_is_finished(pos) :
pos.pop()
return 0
pos.pop()
moyenne = 0
for i in opponent_legal_moves :
moyenne = moyenne + i
if len(opponent_legal_moves) != 0 :
moyenne = moyenne / len(opponent_legal_moves)
#else :   a servi pour le déboggage
#print(moyenne)
if pos.turn == chess.WHITE :
evaluation = evaluation + 0.1*(pos.legal_moves.count() - moyenne)
else :
evaluation = evaluation - 0.1*(pos.legal_moves.count() - moyenne)
return evaluation
def evaluation_turn(pos) :
'''Évaluation d'une position selon le côté qui doit jouer.'''
if pos.turn == chess.WHITE :
return evaluate(pos)
else :
return - evaluate(pos)
def one_legal(pos) :
current_player_legal_moves = [str(move) for move in pos.legal_moves]
#print(current_player_legal_moves)
return random.choice(current_player_legal_moves)
def can_t_move(pos) :
if pos.legal_moves.count() == 0 :
return True
else :
return False
def child_of_position(pos) :
'''Détermine toutes les possitions filles d'une position (avec leur coup associé pour aller plus vite).'''
current_player_legal_moves = [move for move in pos.legal_moves]
result = []
for i in range(len(current_player_legal_moves)-1) :
new_pos = chess.Board(pos.fen())
new_pos.push_san(str(current_player_legal_moves[i]))
result.append((new_pos, str(current_player_legal_moves[i])))
return result  # (board, move)
def minimax(pos, depth, alpha, beta, maximizingPlayer) :
'''Algorithme MiniMax avec Alpha-Bêta.'''
# maximizingPlayer = Trus si White2Play et False si Black2Play
if depth == 0 or game_is_finished(pos[0]) or can_t_move(pos[0]) :
return (evaluate(pos[0]), None)
one_legal_move = one_legal(pos[0])
if maximizingPlayer :
maxEval = (-infinity, one_legal_move) # None = move
for child in child_of_position(pos[0]) :
evalu = minimax(child, depth - 1, alpha, beta, False)
maxEval = (max(maxEval[0], evalu[0]), maxEval[1])
if max(maxEval[0], evalu[0]) == evalu[0] :
maxEval = (max(maxEval[0], evalu[0]), child[1])
alpha = max(alpha, evalu[0])
if beta <= alpha :
break
return maxEval
else :
maxEval = (infinity, one_legal_move)
for child in child_of_position(pos[0]) :
evalu = minimax(child, depth - 1, alpha, beta, True)
maxEval = (min(maxEval[0], evalu[0]), maxEval[1])
if max(maxEval[0], evalu[0]) == evalu[0] :
maxEval = (min(maxEval[0], evalu[0]), child[1])
alpha = min(alpha, evalu[0])
if beta <= alpha :
break
return maxEval

抱歉我英语不好,我是法国人。我希望我的回答能对你有所帮助。[1] :https://commons.wikimedia.org/wiki/Category:PNG_chess_pieces/Standard_transparent

您可以看看本教程。这个解释很彻底,我相信它会给你足够的提示,告诉你如何开始。快乐编码:(以下是如何使用python 制作国际象棋游戏的链接

最新更新