コード例 #1
0
def main():
    p.init()
    window = p.display.set_mode((W_WIDTH, W_HEIGHT))
    time = p.time.Clock()
    window.fill(p.Color("black"))
    gamestate = ChessEngine.Game()
    loadIMG()
    loop = True
    squareSelected = (
    )  # this tuple holds the last click that was made (col,row)
    clicks = [
    ]  # two tuples meaning click 1 -> piece selected and click 2-> piece moved
    highlight = False
    row = -1
    col = -1
    while (loop):
        for e in p.event.get():
            if e.type == p.QUIT:  # quit the game
                loop = False
            elif e.type == p.MOUSEBUTTONDOWN:  # player selected a square
                cursorposition = p.mouse.get_pos()
                # map the cursor position to dimensions needed for the board
                row = cursorposition[1] // SQ_SIZE
                col = cursorposition[0] // SQ_SIZE
                if squareSelected == (col,
                                      row):  # player clicked the same sq twice
                    # clears the clicks
                    highlight = False
                    squareSelected = ()
                    clicks = []
                else:
                    squareSelected = (col, row)
                    clicks.append(squareSelected)  # adds the last click
                if len(clicks
                       ) == 1:  # player selected a piece, so highlight it
                    if row == 0 or row == 9:
                        highlight = False
                        squareSelected = ()
                        clicks = []
                    elif gamestate.board[row - 1][col] != "**":
                        highlight = True
                    else:
                        highlight = False
                        squareSelected = ()
                        clicks = []
                if len(clicks) == 2:
                    highlight = False
                    clicks = []
                    squareSelected = []

        drawBoard(window, gamestate, highlight, col, row)
        time.tick(FPS)
        p.display.flip()
コード例 #2
0
def playChess():
    pygame.init()
    window = pygame.display.set_mode((WIDTH, HEIGHT))
    window.fill(pygame.Color("white"))
    pygame.display.set_caption("Chess game?")
    pygame.display.update()
    clock = pygame.time.Clock()
    game = ChessEngine.Game()
    validMoves = game.getValidMove()
    moveLog = []
    moveMade = False
    animate = True
    LoadImages()
    # drawMenu(window) ### does not work for now
    run = True
    gameOver = False
    computerTurn = False
    stockfish.set_skill_level(1)

    sqSelected = ()  # no square
    playerClicks = []  # track 1st and 2nd player [(0, 0), (1, 1)]
    filesToCols = {
        "h": 7,
        "g": 6,
        "f": 5,
        "e": 4,
        "d": 3,
        "c": 2,
        "b": 1,
        "a": 0
    }
    ranksToRows = {
        "1": 7,
        "2": 6,
        "3": 5,
        "4": 4,
        "5": 3,
        "6": 2,
        "7": 1,
        "8": 0
    }
    while run:
        if not gameOver:

            for e in pygame.event.get():
                if e.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                elif vsComputer and computerTurn:
                    if game.checkMate:  # if there is checkmate
                        print("Checkmate")
                    else:
                        computerMove = computer(moveLog, game)
                        col = filesToCols[computerMove[0:1]]
                        row = ranksToRows[computerMove[1:2]]
                        sqSelected = (row, col)
                        print(sqSelected[0], sqSelected[1], "from")
                        playerClicks.append(sqSelected)
                        col = filesToCols[computerMove[2:3]]
                        row = ranksToRows[computerMove[3:4]]
                        sqSelected = (row, col)
                        print(sqSelected[0], sqSelected[1], "to")
                        playerClicks.append(sqSelected)
                        # stockfish.set_position(moveLog)
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], game.board)

                        printNotation(game, move, playerClicks)
                        game.makeMove(move)
                        moveMade = True
                        animate = True
                        moveLog.append(move.getChessNotation())

                        sqSelected = ()  # clear move
                        playerClicks = []
                        computerTurn = False

                elif e.type == pygame.MOUSEBUTTONDOWN:  # mouse down checker ######
                    if game.checkMate:  # if there is checkmate
                        print("Checkmate")
                    else:
                        location = pygame.mouse.get_pos()  # location[x, y]
                        col = location[0] // SQ_SIZE
                        row = location[1] // SQ_SIZE
                        if sqSelected == (row, col):  # same
                            sqSelected = ()
                            playerClicks = []
                        else:
                            sqSelected = (row, col)
                            playerClicks.append(sqSelected)
                        if len(playerClicks) == 1 and game.board[
                                sqSelected[0]][sqSelected[1]] == "--":
                            sqSelected = ()
                            playerClicks = []
                        if len(playerClicks) == 2:
                            move = ChessEngine.Move(playerClicks[0],
                                                    playerClicks[1],
                                                    game.board)
                            if move in validMoves:
                                printNotation(game, move, playerClicks)
                                game.makeMove(move)
                                moveMade = True
                                animate = True
                                moveLog.append(move.getChessNotation())
                                print("moveLog = " + str(moveLog))
                            sqSelected = ()  # clear move
                            playerClicks = []

                elif e.type == pygame.KEYDOWN:  # keys down checker #####
                    if e.key == pygame.K_z:
                        game.undoMove()
                        moveMade = True
                        animate = False
                        moveLog.pop()
                        if vsComputer:
                            game.undoMove()
                    if e.key == pygame.K_r:
                        game = ChessEngine.Game()
                        validMoves = game.getValidMove()
                        sqSelected = ()
                        playerClicks = []
                        animate = False
                        moveMade = False
                        moveLog = []
                    if e.key == pygame.K_ESCAPE:
                        run = False

        if moveMade:
            if animate:
                animation(game.moveLog[-1], window, game.board, clock)
            validMoves = game.getValidMove()
            moveMade = False
            animate = False

            if vsComputer:
                computerTurn = True if not game.whiteMove else False
                if computerTurn:
                    computerMove = computer(moveLog, game)
                    print(computerMove, computerTurn)  # int(computerMove[1:2])

        drawGame(window, game, sqSelected, validMoves)
        clock.tick(FPS)
        pygame.display.flip()
コード例 #3
0
def main():
    window = p.display.set_mode((W_WIDTH, W_HEIGHT))
    p.display.set_caption('Python Chess')
    time = p.time.Clock()
    window.fill(p.Color("black"))
    gamestate = ChessEngine.Game()
    validMoves = gamestate.getValidMoves()
    move_made = False
    loadIMG()
    loop = True
    squareSelected = (
    )  # this tuple holds the last click that was made (col,row)
    clicks = [
    ]  # two tuples meaning click 1 -> piece selected and click 2-> piece moved
    highlight = False
    row = -1
    col = -1
    while (loop):
        for e in p.event.get():
            if e.type == p.QUIT:  # quit the game
                loop = False
            elif e.type == p.MOUSEBUTTONDOWN:  # player selected a square
                cursorposition = p.mouse.get_pos()
                # map the cursor position to dimensions needed for the board
                row = cursorposition[1] // SQ_SIZE
                col = cursorposition[0] // SQ_SIZE
                if row == 9:  # out of bounds for now
                    highlight = False
                    squareSelected = ()
                    clicks = []
                elif squareSelected == (
                        row - 1, col):  # player clicked the same sq twice
                    # clears the clicks
                    highlight = False
                    squareSelected = ()
                    clicks = []
                else:
                    squareSelected = (row - 1, col)
                    clicks.append(squareSelected)  # adds the last click
                if len(clicks
                       ) == 1:  # player selected a piece, so highlight it
                    if row == 0 or row == 9:
                        highlight = False
                        squareSelected = ()
                        clicks = []
                    elif gamestate.board[row - 1][col] != "**":
                        highlight = True
                    else:
                        highlight = False
                        squareSelected = ()
                        clicks = []
                if len(clicks) == 2:
                    move = ChessEngine.Move(clicks[0], clicks[1],
                                            gamestate.board)
                    if move in validMoves:
                        gamestate.makeMove(move)
                        move_made = True
                    highlight = False
                    clicks = []
                    squareSelected = []
            elif e.type == p.KEYDOWN and e.key == p.K_z:
                gamestate.moveBack()
                highlight = False
                clicks = []
                squareSelected = []
        if gamestate.check_mate or gamestate.stale_mate:
            drawWinner(window, gamestate)
        if move_made:
            validMoves = gamestate.getValidMoves()
        drawBoard(window, gamestate, highlight, col, row - 1)
        time.tick(FPS)
        p.display.flip()