Exemple #1
0
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()
    validMoves = gs.getValidMove()
    moveMade = False
    ChessEngine.print_text_board(gs.board)
    loadImages()
    running = True
    sqSelected = ()  # box clicked by the user (row,col)
    playerClicks = []  # all clicks by user [(row,col),(row,col)]
    gameOver = False
    p.display.set_caption(turnText(gs.whiteToMove))
    flag = 1
    choice = 1
    while running:
        if flag:
            flag = 0
            choice = OptionWindow(screen, gs)
            if choice == 0:
                break
        for event in p.event.get():
            if not gs.whiteToMove and choice == 2:
                move = aimove(gs, 3)
                gs.makeMove(move)
                moveMade = True
                p.display.set_caption(turnText(gs.whiteToMove))
                print(len(gs.moveLog))
            if event.type == p.QUIT:
                running = False
            # mouse handel
            # mouse press
            elif event.type == p.MOUSEBUTTONDOWN and (gs.whiteToMove
                                                      or choice == 1):
                if gameOver == True:
                    break
                location = p.mouse.get_pos()
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (row, col):
                    sqSelected = ()
                    playerClicks = []
                else:
                    sqSelected = (row, col)
                    playerClicks.append(sqSelected)
                if len(playerClicks) == 2:  # two box selected move the piece
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    print(playerClicks)
                    for i in range(len(validMoves)):
                        if move == validMoves[i]:
                            print("moved here:", move.endRow, move.endCol)
                            if validMoves[i].isPawnPromotion:
                                print("Promote to:")
                                new_piece = input().upper()
                                validMoves[
                                    i].promotionChoice = new_piece if new_piece in (
                                        "Q", "R", "N", "B") else "Q"
                            gs.makeMove(validMoves[i])
                            moveMade = True
                            p.display.set_caption(turnText(gs.whiteToMove))
                            sqSelected = ()
                            playerClicks = []
                            break
                    if not moveMade:
                        playerClicks = [sqSelected]
            elif event.type == p.KEYDOWN:
                if event.key == p.K_z:  # z to undo move
                    print(gs.moveLog)
                    gs.undoMove()
                    if not gs.whiteToMove and choice == 2:
                        gs.undoMove()
                    moveMade = True
                    p.display.set_caption(turnText(gs.whiteToMove))

                if event.key == p.K_c:  # c to clear variables
                    sqSelected = ()
                    playerClicks = []
                if event.key == p.K_f:  # f to print valid moves
                    for i in validMoves:
                        print(i.startRow, i.startCol, i.endRow, i.endCol)
                if event.key == p.K_r:
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMove()
                    playerClicks = []
                    sqSelected = ()
                    moveMade = False
                    p.display.set_caption(turnText(gs.whiteToMove))

        if moveMade:
            validMoves = gs.getValidMove()
            moveMade = False
            p.display.set_caption(turnText(gs.whiteToMove))

        drawGameState(screen, gs, validMoves, sqSelected)
        if gs.checkMate:
            gameOver = True
            if gs.whiteToMove:
                drawText(screen, "Black WON by checkmate")
            else:
                drawText(screen, "White WON by checkmate")
        clock.tick(MAX_FPS)
        p.display.flip()
Exemple #2
0
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()
    validMoves = gs.getValidMove()
    moveMade = False
    ChessEngine.print_text_board(gs.board)
    loadImages()
    running = True
    sqSelected = ()  # box clicked by the user (row,col)
    playerClicks = []  # all clicks by user [(row,col),(row,col)]

    # pass
    while running:
        for event in p.event.get():
            if event.type == p.QUIT:
                running = False
            # mouse handel
            elif event.type == p.MOUSEBUTTONDOWN:  # mouse press
                location = p.mouse.get_pos()
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (row, col):
                    sqSelected = ()
                    playerClicks = []
                else:
                    sqSelected = (row, col)
                    playerClicks.append(sqSelected)
                if len(playerClicks) == 2:  # two box selected move the piece
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    print(playerClicks)
                    if move in validMoves:
                        print("moved here:", move.endRow, move.endCol)
                        gs.makeMove(move)
                        moveMade = True
                    else:
                        playerClicks = [sqSelected]
                    sqSelected = ()
                    playerClicks = []
            # key handel
            if event.type == p.KEYDOWN:
                if event.key == p.K_z:
                    gs.undoMove()
                    moveMade = True
                if event.key == p.K_c:
                    sqSelected = ()
                    playerClicks = []
                if event.key == p.K_f:
                    for i in validMoves:
                        print(i.startRow, i.startCol, i.endRow, i.endCol)

        if moveMade == True:
            validMoves = gs.getValidMove()
            moveMade = False
        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()