Python象棋程序

问题描述 投票:0回答:2

我正在尝试为我的编码项目制作一个相当简单的国际象棋游戏,希望在某个时候也能实现一个计算机对手,有点卡在如何添加移动限制和函数来检测此时游戏结束的问题上。身体是由@sloth 设计的,我添加了它。

我已经完成了相当简单的事情,加载所有部件并更改板颜色,但我不太确定此时该做什么。任何帮助,将不胜感激!

import pygame

TILESIZE = 75
BOARD_POS = (10, 10)

def create_board_surf():
    board_surf = pygame.Surface((TILESIZE*8, TILESIZE*8))
    dark = False
    for y in range(8):
        for x in range(8):
            rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
            pygame.draw.rect(board_surf, pygame.Color('darkgreen' if dark else 'beige'), rect)
            dark = not dark
        dark = not dark
    return board_surf

def get_square_under_mouse(board):
    mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
    x, y = [int(v // TILESIZE) for v in mouse_pos]
    try:
        if x >= 0 and y >= 0: return (board[y][x], x, y)
    except IndexError: pass
    return None, None, None

def create_board():
    board = []
    for y in range(8):
        board.append([])
        for x in range(8):
            board[y].append(None)

    ## BLACK PIECES ##

    for x in range(0, 8):
        board[1][x] = ('black', 'pawn')
    for x in range(0, 1):
        board[0][x] = ('black', 'rook')
    for x in range(0, 8):
        board[0][x] = ('black', 'rook')
    for x in range(1, 2):
        board[0][x] = ('black', 'horse')
    for x in range(6, 7):
        board[0][x] = ('black', 'horse')
    for x in range(2, 3):
        board[0][x] = ('black', 'bishop')
    for x in range(5, 6):
        board[0][x] = ('black', 'bishop')
    for x in range(3, 4):
        board[0][x] = ('black', 'queen')
    for x in range(4, 5):
        board[0][x] = ('black', 'king')

    ## WHITE PIECES ##

    for x in range(0, 8):
        board[6][x] = ('white', 'pawn')
    for x in range(0, 1):
        board[7][x] = ('white', 'rook')
    for x in range(7, 8):
        board[7][x] = ('white', 'rook')
    for x in range(1, 2):
        board[7][x] = ('white', 'horse')
    for x in range(6, 7):
        board[7][x] = ('white', 'horse')
    for x in range(2, 3):
        board[7][x] = ('white', 'bishop')
    for x in range(5, 6):
        board[7][x] = ('white', 'bishop')
    for x in range(3, 4):
        board[7][x] = ('white', 'queen')
    for x in range(4, 5):
        board[7][x] = ('white', 'king')



    return board

def draw_pieces(screen, board, font, selected_piece):
    sx, sy = None, None
    if selected_piece:
        piece, sx, sy = selected_piece

    for y in range(8):
        for x in range(8):
            piece = board[y][x]
            if piece:
                selected = x == sx and y == sy
                color, type = piece
                s1 = font.render(type[0], True, pygame.Color('red' if selected else color))
                s2 = font.render(type[0], True, pygame.Color('darkgrey'))
                pos = pygame.Rect(BOARD_POS[0] + x * TILESIZE+1, BOARD_POS[1] + y * TILESIZE + 1, TILESIZE, TILESIZE)
                screen.blit(s2, s2.get_rect(center=pos.center).move(1, 1))
                screen.blit(s1, s1.get_rect(center=pos.center))

def draw_selector(screen, piece, x, y):
    if piece != None:
        rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
        pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)

def draw_drag(screen, board, selected_piece, font):
    if selected_piece:
        piece, x, y = get_square_under_mouse(board)
        if x != None:
            rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
            pygame.draw.rect(screen, (0, 255, 0, 50), rect, 2)

        color, type = selected_piece[0]
        s1 = font.render(type[0], True, pygame.Color(color))
        s2 = font.render(type[0], True, pygame.Color('darkgrey'))
        pos = pygame.Vector2(pygame.mouse.get_pos())
        screen.blit(s2, s2.get_rect(center=pos + (1, 1)))
        screen.blit(s1, s1.get_rect(center=pos))
        selected_rect = pygame.Rect(BOARD_POS[0] + selected_piece[1] * TILESIZE, BOARD_POS[1] + selected_piece[2] * TILESIZE, TILESIZE, TILESIZE)
        pygame.draw.line(screen, pygame.Color('red'), selected_rect.center, pos)
        return (x, y)

def main():
    pygame.init()
    font = pygame.font.SysFont('', 64)
    screen = pygame.display.set_mode((620, 620))
    board = create_board()
    board_surf = create_board_surf()
    clock = pygame.time.Clock()
    selected_piece = None
    drop_pos = None
    while True:
        piece, x, y = get_square_under_mouse(board)
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.MOUSEBUTTONDOWN:
                if piece != None:
                    selected_piece = piece, x, y
            if e.type == pygame.MOUSEBUTTONUP:
                if drop_pos:
                    piece, old_x, old_y = selected_piece
                    board[old_y][old_x] = 0
                    new_x, new_y = drop_pos
                    board[new_y][new_x] = piece
                selected_piece = None
                drop_pos = None

        screen.fill(pygame.Color('grey'))
        screen.blit(board_surf, BOARD_POS)
        draw_pieces(screen, board, font, selected_piece)
        draw_selector(screen, piece, x, y)
        drop_pos = draw_drag(screen, board, selected_piece, font)

        pygame.display.flip()
        clock.tick(60)

if __name__ == '__main__':
    main()
python chess
2个回答
0
投票

首先,如果你只是想把事情做好,你最好使用 python-chess 库。否则,要检查国际象棋比赛的结束,您需要查看是否有少数可能的条件为真:

  • 是将死吗(即国王处于检查状态,不能移动到任何相邻的方格或以其他方式脱离检查)
  • 是否陷入僵局(即没有合法行动)
  • 是三次重复吗(即一个游戏中重复三次的位置)
  • 50 次移动规则(即如果在 50 次移动中没有捕获或棋子移动)
  • 是不是因为材料不够mate才画的

所以仅仅因为游戏规则就相当复杂。

现在,如果您想选择合法的移动,请阅读有关代表董事会的 0x88 方法。它代表两块棋盘的位置,一块包含棋子,另一块是非法领土。这提供了对合法移动的简单检查,因为您只需和具有生成移动的棋盘一起检查合法性。在实施滑动件、king 和 en passant 时要小心。


-1
投票

好吧,国际象棋已经有了一个 python 库。 它被称为蟒蛇国际象棋。 您可以在 here 找到文档 也值得阅读 这篇文章 以快速了解图书馆 这是一个使用这个库构建的简单国际象棋游戏的命令行版本

import chess
import chess.engine
board = chess.Board()
engine = chess.engine.SimpleEngine.popen_uci("Path to a installed chess engine")

while not board.is_game_over():
    legal_move_entered = False
    while legal_move_entered == False:
        move = input("Your move eg:Nf6 , e4 , Qxd2: ")
        try:
            board.push_san(str(move))
            legal_move_entered = True
        except:
            print("Illegal move")
    engine_move = engine.play(board = board , limit = chess.engine.Limit(time = 0.1))
    board.push(engine_move.move)
    print(board)
© www.soinside.com 2019 - 2024. All rights reserved.