示例#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.getValidMoves()
    moveMade = False  #flag variable for when a move is made, only generaete new valid move list when a user makes a move
    loadImages()
    running = True
    sqSelected = (
    )  #no square selected initially, a tuple to keep track of the square of the user
    playerClicks = []  #keep track of player clicks
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            #mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  #location of mouse
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (
                        row,
                        col):  #check if the user clicked the same square twice
                    sqSelected = ()  #unselecting function
                    playerClicks = []  #clear player clicks
                else:
                    sqSelected = (row, col)
                    playerClicks.append(
                        sqSelected)  #append for both 1st and 2nd clicks
                if len(playerClicks) == 2:  #meaning after the second click
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    for i in range(len(validMoves)):
                        if move == validMoves[i]:
                            gs.makeMove(validMoves[i])
                            moveMade = True
                            sqSelected = ()  #reset the user clicks
                            playerClicks = []
                    if not moveMade:
                        playerClicks = [sqSelected]
            #key handlers
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  #undo when "z" is pressed
                    gs.undoMove()
                    moveMade = True

        if moveMade:
            validMoves = gs.getValidMoves()
            moveMade = False

        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
示例#2
0
def main():
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()

    validMoves = gs.getValidMoves()
    moveMade = False  # flag variable for when a move is made

    loadImages()
    running = True
    sqSelected = (
    )  # no square selected initially. but this simply keep track of the last click of the user
    playerClicks = []  # keep track of user clicks
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  # (x,y) location of the mouse
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (
                        row, col
                ):  # the user clicked the same coordinate/square twice
                    sqSelected = ()  # deselect
                    playerClicks = []  # clear player clicks
                else:
                    sqSelected = (row, col)
                    playerClicks.append(
                        sqSelected)  # append for 1st and 2nd clicks
                if len(playerClicks) == 2:  # after the 2nd click
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    if move in validMoves:
                        gs.makeMove(move)
                        moveMade = True
                        sqSelected = ()  # reset user clicks
                        playerClicks = []
                    else:
                        playerClicks = [sqSelected]

            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:
                    gs.undoMove()
                    moveMade = True

        if moveMade:
            validMoves = gs.getValidMoves()
            moveMade = False
            mixer.music.load("move.wav")
            mixer.music.play()

        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
示例#3
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.getValidMoves()
    moveMade = False  #flag variable for when a move is made

    loadImages()  #only do this once,before the while loop
    running = True
    sqSelected = ()  #no square is selected, keep track of the last click
    #of the user(tuple:(row,colon))
    playerClicks = [
    ]  #keep track of the player clicks (two tuples: [(6,4),(4,4)])
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            #mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  #(x,y) location of mouse
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (row,
                                  col):  #the user click the same square twice
                    sqSelected = ()  #deselect
                    playerClicks = []  #click player clicks
                else:
                    sqSelected = (row, col)
                    playerClicks.append(
                        sqSelected)  #append for both 1st and 2nd click
                if len(playerClicks) == 2:  #after 2nd click
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    if move in validMoves:
                        gs.makeMove(move)
                        moveMade = True
                        sqSelected = ()  #reset user clicks
                        playerClicks = []
                    else:
                        playerClicks = [sqSelected]

            #key handlers
            elif e.type == p.KEYDOWN:
                if e.key == p.k_z:  #undo when 'z' is pressed
                    gs.undoMove()
                    moveMade = True
        if moveMade:
            validMoves = gs.getValidMoves()
            moveMade = False
        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
示例#4
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.getValidMoves()
    moveMade = False
    #Lodes in all of the images.
    loadImages()  #only once, before while loop
    running = True
    sqSelected = (
    )  #no square initially. Keep track of last square the user had selected.
    playerClicks = []  # 0,1,2. Keep track of player clicks.
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            #mouse stuff
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  #(x,y location of the mouse)
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (row,
                                  col):  #user clicked the same square twice
                    sqSelected = ()  #empties the square selected
                    playerClicks = []  #clears player clicks
                else:
                    sqSelected = (row, col)
                    playerClicks.append(
                        sqSelected)  #append for 1st and 2nd click
                if len(playerClicks) == 2:  #after second click
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    if move in validMoves:
                        gs.makeMove(move)
                        moveMade = True
                        sqSelected = ()  #reset
                        playerClicks = []
                    else:
                        playerClicks = [sqSelected]
            #key handler
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:
                    gs.undoMove()
                    moveMade = True
        if moveMade:
            validMoves = gs.getValidMoves()
            moveMade = False

        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
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.getValidMoves()
    moveMade = False
    loadImages()
    runnig = True
    sqSelected = ()
    playerClicks = []
    while runnig:
        for e in p.event.get():
            if e.type == p.QUIT:
                runnig = False
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos(
                )  #la posición será la misma del mouse
                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:
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    for i in range(len(validMoves)):
                        if move == validMoves[i]:
                            gs.makeMove(validMoves[i])
                            moveMade = True
                            sqSelected = ()
                            playerClicks = []
                    if not moveMade:
                        playerClicks = [sqSelected]
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:
                    gs.undoMove()
                    moveMade = True

        if moveMade:
            validMoves = gs.getValidMoves()
            moveMade = False

        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
示例#6
0
def main():
    '''
    handle user input and updating graphics
    '''
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    clock = pygame.time.Clock()
    screen.fill((255, 255, 255))
    gs = ChessEngine.GameState()
    load_images()
    run = True
    sq_selected = (
    )  # (row, col) nothing selected initially, keep track of last click of user
    player_clicks = [
    ]  # keep track of player clicks (two touples) [(6, 4), (8, 3)]
    while run:
        for i in pygame.event.get():
            if i.type == pygame.QUIT:
                run = False
            elif i.type == pygame.MOUSEBUTTONDOWN:
                location = pygame.mouse.get_pos()  #x and y location of mouse
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sq_selected == (row, col):
                    sq_selected = ()
                    player_clicks = []
                else:
                    sq_selected = (row, col)
                    player_clicks.append(sq_selected)

                if len(player_clicks) == 2:
                    # print(player_clicks)
                    move = ChessEngine.Move(player_clicks[0], player_clicks[1],
                                            gs.board)
                    print(move.get_chess_notation())
                    gs.make_move(move)
                    sq_selected = ()
                    # reset the user click
                    player_clicks = []

        draw_game_state(screen, gs.board)
        clock.tick(MAX_FPS)
        pygame.display.flip()
示例#7
0
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()
    loadImages()  # Only do this once, before the while loop
    running = True
    sqSelected = (
    )  # No square is selected, keep track of the last click of the user (tuple: row, col)
    playerClicks = [
    ]  # Keep track of the player clicks (two tuples: [(6, 4), (4, 4))
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  # (x, y) location of the mouse
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sqSelected == (
                        row, col):  # The user clcked the same square twice
                    sqSelected = ()  # deselect
                    playerClicks = []  # clear the player clicks
                else:
                    sqSelected = (row, col)
                    playerClicks.append(
                        sqSelected)  # append for both 1st and 2nd clicks
                if len(playerClicks) == 2:  # after de 2nd click
                    move = ChessEngine.Move(playerClicks[0], playerClicks[1],
                                            gs.board)
                    print(move.getChessNotation())
                    gs.makeMove(move)
                    sqSelected = ()  # reset the user clicks
                    playerClicks = []

        drawGameState(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
示例#8
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.getValidMoves()
    moveMade = False #flag variable for when a valid move is made and gamestate actually changes

    animate = False # flag variable for when we should animate a move
    loadImages() #only do once, before while loop
    running = True
    sqSelected = () #no square is selected, keep track of last click of user (tuple: row, col)
    playerClicks = [] #keep track of player clicks (two tuples: [(6,4), (4,4)])
    gameOver = False

    print(validMoves)


    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            #mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                if not gameOver:
                    location = p.mouse.get_pos() #(x,y location of mouse)
                    col = location[0] // SQ_SIZE
                    row = location[1] // SQ_SIZE
                    if sqSelected == (row, col): #user clicked same square twice
                        sqSelected = ()
                        playerClicks = [] #clear player clicks
                    else:
                        sqSelected = (row, col)
                        playerClicks.append(sqSelected) #append for both 1st and 2nd clicks
                    if len(playerClicks) == 2: #after 2nd click
                        move = ChessEngine.Move(playerClicks[0], playerClicks[1], gs.board)
                        print(move.getChessNotation())
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gs.makeMove(validMoves[i]) #instead of using "move", we make sure to use validMoves[i] bc those have the necessary flags
                                moveMade = True
                                animate = True
                                sqSelected = () #reset user clicks
                                playerClicks = []
                        if not moveMade:
                            playerClicks = [sqSelected]
                    #key handler
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z: #undo when 'z' is pressed
                    gs.undoMove()
                    moveMade = True
                    animate = False
                if e.key == p.K_r: #reset the board when r is pressed
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False
        if moveMade:
            if animate:
                animateMove(gs.moveLog[-1], screen, gs.board, clock)
            validMoves = gs.getValidMoves()
            # print("\nUpdated Valid Moves:")
            # count = 0
            # for i in range(len(validMoves)):
            #     if(validMoves[i].pieceMoved[1] == 'B'):
            #         print(validMoves[i].moveID)
            #         count+=1
            moveMade = False
            animate = False

        drawGameState(screen, gs, validMoves, sqSelected)

        if gs.checkmate:
            gameOver = True
            if gs.whiteToMove:
                drawText(screen, 'Black wins by checkmate')
            else:
                drawText(screen, 'White wins by checkmate')
        elif gs.stalemate:
            gameOver = True
            drawText(screen, 'Stalemate')
        clock.tick(MAX_FPS)
        p.display.flip()
示例#9
0
def main():
    p.init()
    screen = p.display.set_mode((width, height))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gameState = ChessEngine.GameState()
    validMoves = gameState.getValidMoves()
    moveMade = False  #flag varriable for when a move is made

    loadImages()
    running = True
    sqSelected = ()  #last click of the user (row, col)
    playerClicks = []  #player clicks ex: [(1,2), (3,4)]
    gameOver = False
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            # Mouse Handler
            elif e.type == p.MOUSEBUTTONDOWN:
                if not gameOver:
                    location = p.mouse.get_pos()
                    col = location[0] // sqSize
                    row = location[1] // sqSize
                    if sqSelected == (row, col):  #clicked same square
                        sqSelected = ()  #deselect
                        playerClicks = []  #clear clicks
                    else:
                        sqSelected = (row, col)
                        playerClicks.append(sqSelected)  #append both clicks
                    if len(playerClicks) == 2:
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1],
                                                gameState.board)
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gameState.makeMove(validMoves[i])
                                moveMade = True
                                print(move.getChessNotation())
                                sqSelected = ()  #reset square for next
                                playerClicks = []  #reset clicks for next
                        if not moveMade:
                            playerClicks = [sqSelected]
            # key handlers
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  # undo when keyboard 'z' is pressed
                    gameState.undoMove()
                    moveMade = True
                    gameOver = False
                if e.key == p.K_r:  # reset game when 'r' pressed
                    gameState = ChessEngine.GameState()
                    validMoves = gameState.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    gameOver = False
                    #animate = False    #if we decide to animate later :)

        if moveMade:
            validMoves = gameState.getValidMoves()
            moveMade = False

        drawGameState(screen, gameState, validMoves, sqSelected)

        if gameState.checkMate:
            gameOver = True
            if gameState.whiteToMove:
                drawText(screen, 'Black wins')
            else:
                drawText(screen, 'White wins')
        if gameState.staleMate:
            gameOver = True
            drawText(screen, 'Stalemate')

        clock.tick(maxFPS)
        p.display.flip()
示例#10
0
def main():
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()
    load_images()
    running = True
    sq_selected = (
    )  # no square is selected, keep track of the last click of the user (tuple: (row, column))
    player_clicks = (
        []
    )  # keep track of player clicks (two tuples: [(row, column), (row, column)])
    valid_moves = gs.get_valid_moves()
    move_made = False  # flag variable for when move is made
    animate = False  # flag variable for when we should animate a move
    game_over = False

    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            # mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                if not game_over:
                    location = p.mouse.get_pos()
                    col = location[0] // SQ_SIZE
                    row = location[1] // SQ_SIZE
                    if sq_selected == (
                            row,
                            col,
                    ):  # the user clicked the same square twice
                        sq_selected = ()
                        player_clicks = []
                    else:
                        sq_selected = (row, col)
                        player_clicks.append(sq_selected)

                    if len(player_clicks) == 2:
                        move = ChessEngine.Move(player_clicks[0],
                                                player_clicks[1], gs.board)
                        print(move.get_chess_notation())
                        for i in range(len(valid_moves)):
                            if move == valid_moves[i]:
                                gs.make_move(valid_moves[i])
                                move_made = True
                                animate = True
                                sq_selected = ()  # reset user clocks
                                player_clicks = []
                        if not move_made:
                            player_clicks = [sq_selected]
            # key handler
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  # undo when 'z' is pressed
                    gs.undo_move()
                    move_made = True
                    animate = False
                if e.key == p.K_r:  # reset the board when 'r is pressed
                    gs = ChessEngine.GameState()
                    valid_moves = gs.get_valid_moves()
                    sq_selected = ()
                    move_made = False
                    animate = False

        if move_made:
            if animate:
                animate_move(gs.move_log[-1], screen, gs.board, clock)
            valid_moves = gs.get_valid_moves()
            move_made = False
            animate = False

        draw_game_state(screen, gs, valid_moves, sq_selected)
        if gs.checkmate:
            game_over = True
            if gs.white_to_move:
                draw_text(screen, "Black wins by checkmate")
            else:
                draw_text(screen, "White wins by checkmate")
        elif gs.stalemate:
            game_over = True
            draw_text(screen, "Stalemate")

        clock.tick(MAX_FPS)
        p.display.flip()
示例#11
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.getValidMoves()
    moveMade = False
    animate = False
    gameOver = False
    load_images()
    sqSelected = ()  # last click of the user
    playerClicks = []  # keeps track of player clicks  - two tuples
    running = True
    playerOne = True  # if a human is white this is True
    playerTwo = True  # if a human is black  this is True
    while running:
        humanTurn = (gs.whiteToMove and playerOne) or (not gs.whiteToMove
                                                       and playerTwo)
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
                # mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                if not gameOver and humanTurn:
                    location = p.mouse.get_pos()
                    col = location[0] // SQ_SIZE
                    row = location[1] // SQ_SIZE
                    if sqSelected == (
                            row, col
                    ):  # clicking twice the same sq deselects everything
                        sqSelected = ()
                        playerClicks = []
                    else:
                        sqSelected = (row, col)
                        playerClicks.append(sqSelected)
                    if len(playerClicks) == 2:  # after 2nd click
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], gs.board)
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gs.makeMove(validMoves[i])
                                moveMade = True
                                animate = True
                                gameOver = False
                                sqSelected = ()
                                playerClicks = []
                        if not moveMade:
                            playerClicks = [sqSelected]

            # key handler
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  # undo a move when z is pressed
                    gs.undoMove()
                    moveMade = True
                    animate = False  # don't animate after a undo
                    gameOver = False
                if e.key == p.K_r:
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    gameOver = False
                    animate = False
                    moveMade = False
        # AI move finder
        if not gameOver and not humanTurn:
            AIMove = ChessAI.findBestMoveMinMax(gs, validMoves)
            if AIMove is None:
                ChessAI.findRandomMove(validMoves)
            # AIMove = ChessAI.findRandomMove(validMoves)
            gs.makeMove(AIMove)
            moveMade = True
            animate = True

        if moveMade:
            if animate:
                animateMove(gs.moveLog[-1], screen, gs.board, clock)
            validMoves = gs.getValidMoves()
            moveMade = False
            animate = False

        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')
        elif gs.stalemate:
            gameOver = True
            drawText(screen, 'Draw by stalemate')

        clock.tick(MAX_FPS)
        p.display.flip()
示例#12
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.getValidMoves()
    moveMade = False  # flag variable for when a move is made
    animate = False  # flag varible for when we should animate a move
    loadImages()  # only do this once
    running = True
    sqSelected = (
    )  # no square is selected , keep track of the last click of the user
    playerClicks = [
    ]  # keep track of player clicks(two tuples: [(6,4), (4,4)])
    gameOver = False  # flag variable for gameover state

    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            # mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                if not gameOver:
                    location = p.mouse.get_pos()  # (x,y) location of mouse
                    col = location[0] // SQ_SIZE
                    row = location[1] // SQ_SIZE
                    # sqSelected = (row, col)
                    # print(sqSelected)
                    if sqSelected == (
                            row, col
                    ):  # check if the user clicks the same square twice
                        sqSelected = ()  # undo or deselect
                        playerClicks = []  # clear the clicks.
                        # print(sqSelected)
                    else:
                        sqSelected = (row, col)
                        playerClicks.append(
                            sqSelected)  # append for both 1st and 2nd clicks
                    if len(playerClicks) == 2:  # after 2nd click
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], gs.board)
                        print(move.getChessNotation())
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gs.makeMove(validMoves[i])
                                moveMade = True
                                animate = True
                                sqSelected = ()  # reset user clicks
                                playerClicks = []
                        if not moveMade:
                            playerClicks = [sqSelected]
            # key handlers
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  # undo when 'z' is pressed
                    gs.undoMove()
                    moveMade = True
                    animate = False
                    gameOver = False
                if e.key == p.K_r:  # reset the board when 'r' is pressed
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False
                    gameOver = False

        if moveMade:
            if animate:
                animateMove(gs.movelog[-1], screen, gs.board, clock)
            validMoves = gs.getValidMoves()
            moveMade = False
            animate = False

        drawGameState(screen, gs, validMoves, sqSelected)

        if gs.checkMate:
            gameOver = True
            if gs.whiteToMove:
                drawText(screen, 'Black wins by Checkmate')
            else:
                drawText(screen, 'White wins by Checkmate')
        elif gs.staleMate:
            gameOver = True
            drawText(screen, 'Stalemate')

        clock.tick(MAX_FPS)
        p.display.flip()
示例#13
0
def main():
    """
    Main driver of code
    Handles user inputs and updating graphics
    :return:
    """
    p.init()  # Initializing pygame object
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    gs = ChessEngine.GameState()

    valid_moves = gs.get_valid_moves()

    # Flag to control the number of times get valid moves is called
    # Only if the user makes a valid move, it is called
    move_made = False

    load_images()
    game_running = True

    sq_selected = tuple()  # (row, col), keeps track of user click
    player_clicks = list()  # 2 tuples in the list, [(row, col), (row, col)]

    while game_running:

        for e in p.event.get():
            if e.type == p.QUIT:
                game_running = False

            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  # undo when 'z' is pressed
                    gs.undo_move()
                    move_made = True  # On undo we need to generate all valid moves again

            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos(
                )  # Gets (col, row) location of mouse click
                row = location[1] // SQ_SIZE
                col = location[0] // SQ_SIZE

                # If user clicks on the same square again, i.e. as source and destination,
                # then we deselect it and reset player clicks
                if sq_selected == (row, col):
                    sq_selected = tuple()
                    player_clicks = list()
                else:
                    if not (len(player_clicks) == 0
                            and gs.board[row][col] == gs.EMPTY_SQ):
                        sq_selected = (row, col)
                        player_clicks.append(
                            sq_selected)  # Append both first and second clicks

                # After second click only
                if len(player_clicks) == 2:
                    move = ChessEngine.Move(start_sq=player_clicks[0],
                                            end_sq=player_clicks[1],
                                            board=gs.board)
                    # move.print_move()
                    for i in range(len(valid_moves)):

                        if move == valid_moves[i]:
                            gs.make_move(valid_moves[i])
                            move_made = True

                            player_clicks = list(
                            )  # Resetting to restart the 2 click move logic
                            sq_selected = tuple()
                    if not move_made:
                        player_clicks = [sq_selected]

        if move_made:
            valid_moves = gs.get_valid_moves()
            move_made = False

        draw_game_state(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
def main():
    p.init()
    screen = p.display.set_mode(
        (BOARD_WIDTH + MOVE_LOG_PANEL_WIDTH, BOARD_HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))
    moveLogFont = p.font.SysFont("Arial", 14, False, False)
    gs = ChessEngine.GameState()
    validMoves = gs.getValidMoves()
    moveMade = False  #flag variable for when a move is made
    animate = False  #flag variable for when when we should animate a move
    loadImages()  #only do this once, before the while loop
    running = True
    sqSelected = (
    )  #makes this a tuple no square is selected initially(tuple: (row,col)
    playerClicks = [
    ]  #keep track of the player clicks (two tuples: [(6,4),(4,4)]
    gameOver = False
    playerOne = True  #if a human is playing white, this is true. If an AI is playing it is false  #we can pit two ai against each other by turning it false
    playerTwo = False  #same as above but for black
    AIThinking = False
    moveFinderProcess = None
    moveUndone = False
    while running:
        humanTurn = (gs.whiteToMove and playerOne) or (not gs.whiteToMove
                                                       and playerTwo)
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            #mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:  #event functionality
                if not gameOver and humanTurn:
                    location = p.mouse.get_pos()  #(x,y) location of the mouse
                    col = location[0] // SQ_SIZE  #source of the bug
                    row = location[1] // SQ_SIZE
                    if sqSelected == (
                            row, col
                    ) or col >= 8:  #the user clicked the same square twice or user click the mouse log
                        sqSelected = ()  #deselcted
                        playerClicks = []  #clear player clicks
                    else:
                        sqSelected = (row, col)
                        playerClicks.append(
                            sqSelected)  #append for both 1sr and 2nf clicks
                    if len(playerClicks) == 2:  #after second click
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], gs.board)
                        print(move.getChessNotation())
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gs.makeMove(validMoves[i])
                                moveMade = True
                                animate = True
                                sqSelected = ()  #reset user clicks
                                playerClicks = []
                        if not moveMade:
                            playerClicks = [sqSelected]
            #key handlers
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  #undo when 'z' is pressed
                    gs.undoMove()
                    moveMade = True
                    animate = False  #here animate is a flag variable
                    gameOver = False
                    if AIThinking:
                        moveFinderProcess.terminate()
                        AIThinking = False
                    moveUndone = True
                if e.key == p.K_r:  #reset the board when 'r' is pressed
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False
                    gameOver = False
                    if AIThinking:
                        moveFinderProcess.terminate()
                        AIThinking = False
                    moveUndone = True

        #AI move finder logic
        if not gameOver and not humanTurn and not moveUndone:
            if not AIThinking:
                AIThinking = True
                print("thinking...")
                returnQueue = Queue()  #used to pass the data between threads
                moveFinderProcess = Process(
                    target=SmartMoveFinder.findBestMove,
                    args=(gs, validMoves, returnQueue))
                moveFinderProcess.start(
                )  #call findBestMove(gs, validMoves, returnQueue)

            if not moveFinderProcess.is_alive():
                print("done thinking")
                AIMove = returnQueue.get()
                if AIMove is None:
                    AIMove = SmartMoveFinder.findRandomMove(validMoves)
                gs.makeMove(AIMove)
                moveMade = True
                animate = True
                AIThinking = False

        if moveMade:
            if animate:
                animateMove(
                    gs.moveLog[-1], screen, gs.board, clock
                )  #we are animating the last move in the movelog, on the screen with the board using pygame clock
            validMoves = gs.getValidMoves()
            moveMade = False
            animate = False
            moveUndone = False

        drawGameState(screen, gs, validMoves, sqSelected, moveLogFont)
        if gs.checkMate or gs.staleMate:
            gameOver = True
            text = 'Stalemate' if gs.staleMate else 'Black wins by Checkmate' if gs.whiteToMove else 'White wins by Checkmate'
            drawEndGameText(screen, text)

        clock.tick(MAX_FPS)
        p.display.flip()
示例#15
0
def main():
    global menu, multiplayer, single

    # standard pygame initialization
    p.init()
    screen = p.display.set_mode((WIDTH, HEIGHT))
    clock = p.time.Clock()
    screen.fill(p.Color("white"))

    g = ChessEngine.Game()  # create the game
    AI = ChessAI.AI(g)  # start the AI
    valid_moves = g.all_moves(
    )  # generate all the initial moves that can be made
    find_moves = False

    load_images()

    running = True
    curr_sq = ()
    clicks = []

    while running:

        for e in p.event.get():

            # allows the user to quit the program
            if e.type == p.QUIT:
                running = False

            # if the event is the user clicking the mouse
            elif e.type == p.MOUSEBUTTONDOWN:

                location = p.mouse.get_pos()  # location of the mouse click

                # buttons on the menu that allow players to choose game mode
                if menu:
                    if WIDTH // 4 <= location[0] <= (WIDTH // 4) + (SQ_SIZE *
                                                                    4):
                        if 2 * (HEIGHT // 4) <= location[1] <= 2 * (
                                HEIGHT // 4) + SQ_SIZE:
                            menu = False
                            single = True

                    if WIDTH // 4 <= location[0] <= (WIDTH // 4) + (SQ_SIZE *
                                                                    4):
                        if 3 * (HEIGHT // 4) <= location[1] <= 3 * (
                                HEIGHT // 4) + SQ_SIZE:
                            menu = False
                            multiplayer = True
                    continue  # make sure you don't move pieces while on menu screen

                # finding the square coordinates of the mouseclick
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE

                # if they double clicked on the same square then reset
                if curr_sq == (row, col):
                    curr_sq = ()
                    clicks = []

                else:
                    curr_sq = (row, col)
                    clicks.append(curr_sq)

                if len(
                        clicks
                ) == 2:  # if there are two loaded clicks(piece they want to move, where to move)

                    if multiplayer:
                        move = ChessEngine.Move(clicks[0], clicks[1],
                                                g.board)  # store the move

                        for i in range(len(valid_moves)):
                            # if the move stored is valid given the rules of chess
                            if valid_moves[i].print_move() == move.print_move(
                            ):
                                g.make_move(valid_moves[i])  # make the move
                                find_moves = True  # tell the computer to begin calculating the opponent's valid moves
                                curr_sq = ()
                                clicks = []

                        if curr_sq != ():
                            clicks = [curr_sq]

                    # for single player games against the computer
                    if single:
                        move = ChessEngine.Move(clicks[0], clicks[1],
                                                g.board)  # store the move

                        for i in range(len(valid_moves)):
                            # if the move stored is valid given the rules of chess
                            if move is not None:
                                if valid_moves[i].print_move(
                                ) == move.print_move():
                                    g.make_move(
                                        valid_moves[i])  # make the move
                                    find_moves = True  # tell the computer to begin calculating the player's valid moves

                                    move = AI.find_move(
                                        2, True)  # finds the best move
                                    if move is not None:  # makes sure there is a move (not checkmate or stalemate)
                                        g.make_move(move)

                                    curr_sq = ()
                                    clicks = []

                        # adds the selected square to clicks
                        if curr_sq != ():
                            clicks = [curr_sq]

                # if the game is over go back to menu and reset the game
                if g.checkmate or g.stalemate:
                    menu = True
                    single = False
                    multiplayer = False
                    g = ChessEngine.Game()
                    AI = ChessAI.AI(g)
                    find_moves = True

            elif e.type == p.KEYDOWN:

                # if the user clicks u then we want to undo the last made move
                if e.key == p.K_u:
                    g.undo_move()
                    find_moves = True

        if find_moves:  # this calculates all the possible next moves

            valid_moves = g.all_moves()
            find_moves = False

        # if a game is ongoing then draw the board
        if single or multiplayer:
            draw_board(screen, g)

        # draw the menu
        if menu:
            draw_menu(screen)

        clock.tick(MAX_FPS)
        p.display.flip()
示例#16
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.getValidMoves()
    moveMade = False
    animate = False
    loadImages()
    running = True
    sqSelected = ()  # no squares selected
    playerClicks = []  # players clicks tracking
    gameOver = False
    playerOne = True #False to enable AI vs AI
    playerTwo = False

    while running:
        humanTurn = (gs.whiteToMove and playerOne) or (not gs.whiteToMove and playerTwo)
        for e in p.event.get():
            if not gameOver and humanTurn:
                if e.type == p.QUIT:
                    running = False
                elif e.type == p.MOUSEBUTTONDOWN:
                    location = p.mouse.get_pos()
                    col = location[0] // SQ_SIZE
                    row = location[1] // SQ_SIZE

                    if sqSelected == (row, col):
                        sqSelected = ()  # Un-select
                        playerClicks = []  # Reset player clicks
                    else:
                        sqSelected = (row, col)
                        playerClicks.append(sqSelected)

                    if len(playerClicks) == 2:  # Move piece after second click
                        move = ChessEngine.Move(playerClicks[0], playerClicks[1], gs.board)
                        print(move.getChessNotations())
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gs.makeMove(validMoves[i])
                                moveMade = True
                                animate = True
                                sqSelected = ()
                                playerClicks = []
                        if not moveMade:
                            playerClicks = [sqSelected]
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:
                    gs.undoMove()
                    moveMade = True
                    animate = False
                if e.key == p.K_r:
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False

        # -- A.I --
        if not gameOver and not humanTurn:
            AIMove = SmartMoveFinder.findRandomMove(validMoves)
            gs.makeMove(AIMove)
            moveMade = True
            animate = True


        if moveMade:
            if animate:
                animateMove(gs.moveLog[-1], screen, gs.board, clock)
            validMoves = gs.getValidMoves()
            moveMade = False
            animate = False

        drawGameState(screen, gs, validMoves, sqSelected)

        if gs.checkMate:
            gameOver = True
            if gs.whiteToMove:
                drawText(screen, 'Black Wins by Checkmate')
            else:
                drawText(screen, 'White Wins by Checkmate')

        elif gs.staleMate:
            gameOver = True
            drawText(screen, 'Stalemate')

        clock.tick(MAX_FPS)
        p.display.flip()
示例#17
0
def main():
    """Main function which handles user input and updates graphics"""
    screen = p.display.set_mode((board_width + move_log_panel_width, board_height))
    clock = p.time.Clock()
    screen.fill(p.Color('white'))
    move_log_font = p.font.SysFont('Arial', 14, False, False)
    game_state = ChessEngine.GameState()
    valid_moves = game_state.get_valid_moves()
    move_made = False  # Flag variable for when a move is made
    animate = False  # Flag variable for when a move should be animated
    load_images()
    running = True
    square_selected = ()  # Keeps track of the last click by user (tuple: (row, column))
    player_clicks = []  # Keeps track of player clicks (two tuples: ex. [(6, 4), (4, 4)])
    game_over = False

    while running:
        human_turn = (game_state.white_to_move and player_one) or (not game_state.white_to_move and player_two)

        for event in p.event.get():
            if event.type == p.QUIT:
                running = False

            # Mouse handler
            elif event.type == p.MOUSEBUTTONDOWN:
                if not game_over and human_turn:
                    location = p.mouse.get_pos()  # (x, y) location of mouse
                    column = location[0] // sq_size
                    row = location[1] // sq_size
                    if square_selected == (row, column) or column >= dimension:  # User clicks same square or move log
                        square_selected = ()  # Deselects
                        player_clicks = []  # Clears player clicks
                    else:
                        square_selected = (row, column)
                        player_clicks.append(square_selected)  # Appends both 1st and 2nd clicks
                    if len(player_clicks) == 2:
                        move = ChessEngine.Move(player_clicks[0], player_clicks[1], game_state.board)
                        for i in range(len(valid_moves)):
                            if move == valid_moves[i]:
                                game_state.make_move(valid_moves[i])
                                move_made = True
                                animate = True
                                square_selected = ()  # Resets user clicks
                                player_clicks = []
                        if not move_made:
                            player_clicks = [square_selected]

            # Key handlers
            elif event.type == p.KEYDOWN:
                if event.key == p.K_z:  # Undo move when 'z' is pressed
                    game_state.undo_move()
                    move_made = True
                    animate = False
                    game_over = False
                if event.key == p.K_r:  # Reset board when 'r is pressed
                    game_state = ChessEngine.GameState()
                    valid_moves = game_state.get_valid_moves()
                    square_selected = ()
                    player_clicks = []
                    move_made = False
                    animate = False
                    game_over = False

        # AI move finder
        if not game_over and not human_turn:
            AI_move = ChessAI.find_best_move(game_state, valid_moves)
            if AI_move is None:
                AI_move = ChessAI.find_random_move(valid_moves)
            game_state.make_move(AI_move)
            move_made = True
            animate = True

        if move_made:
            if animate:
                animate_move(game_state.move_log[-1], screen, game_state.board, clock)
            valid_moves = game_state.get_valid_moves()
            move_made = False
            animate = False

        draw_game_state(screen, game_state, square_selected, move_log_font)

        if game_state.checkmate or game_state.stalemate:
            game_over = True
            if game_state.stalemate:
                text = 'Stalemate'
            else:
                text = 'Black wins by checkmate' if game_state.white_to_move else 'White wins by checkmate'
            draw_endgame_text(screen, text)

        clock.tick(max_fps)
        p.display.flip()
示例#18
0
def main(usersID):
    global firstUserID, secondUserID
    firstUserID = usersID[0]
    secondUserID = usersID[1]

    pg.init()
    screen = pg.display.set_mode((WIDTH, HEIGHT))
    clock = pg.time.Clock()
    screen.fill(pg.Color('white'))

    gs = ChessEngine.GameState(firstUserID, secondUserID)
    validMoves = gs.getValidMoves()
    moveMade = False
    animate = True

    loadImages()  # only do this once, before the will loop

    running = True
    sqSelected = ()
    playerClicks = []

    gameOver = False

    while running:
        for e in pg.event.get():
            if e.type == pg.QUIT:
                running = False
            # mouse handlers
            elif e.type == pg.MOUSEBUTTONDOWN:
                if not gameOver:
                    location = pg.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:  # after 2nd click
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], gs.board)
                        if move in validMoves:
                            gs.makeMove(move)
                            moveMade = True
                            animate = True
                            sqSelected = ()  # reset users click
                            playerClicks = []
                        else:
                            playerClicks = [sqSelected]

            # key handlers
            elif e.type == pg.KEYDOWN:
                if e.key == pg.K_z:  # undo when 'z' is pressed
                    gs.undoMove()
                    moveMade = True
                if e.type == pg.K_r:  # reset the board when 'r' is pressed
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False

        if moveMade:
            if animate:
                animateMove(gs.moveLog[-1], screen, gs.board, clock)
            validMoves = gs.getValidMoves()
            moveMade = False
            animate = False

        drawGameState(screen, gs, validMoves, sqSelected)

        if gs.checkmate:
            gameOver = True
            if gs.whiteToMove:
                drawText(screen, 'Black wins to checkmate')
                addWins('black')
                running = False
            else:
                drawText(screen, 'White wins to checkmate')
                addWins('white')
                running = False
        elif gs.stalemate:
            gameOver = True
            drawText(screen, 'Stalemate')

        clock.tick(MAX_FPS)
        pg.display.flip()
示例#19
0
def main():
    # initialize pygame
    p.init()
    # screen variable
    screen = p.display.set_mode((WIDTH, HEIGHT))
    # clock variable
    clock = p.time.Clock()
    # fill screen with color white, not necessary later on
    screen.fill(p.Color("white"))
    # create game state, gives access to variables in ChessEngine like board for example
    gs = ChessEngine.GameState()
    valid_moves = gs.get_valid_moves()
    move_made = False  # flag variable for when a move is made

    # example to print chess board
    # print(gs.board)

    # load in images. do this only once, before the while loop
    load_images()
    running = True
    sq_selected = (
    )  # no square is selected, keep track of last click of the user (tuple: (row, col))
    player_clicks = [
    ]  # keeps track of player clicks (two tuples: [(6, 4), (4, 4)])
    while running:
        # clear event queue
        for e in p.event.get():
            # to quit game
            if e.type == p.QUIT:
                running = False
            # if event type is mouse click, later I will add dragging pieces
            # mouse handler
            elif e.type == p.MOUSEBUTTONDOWN:
                location = p.mouse.get_pos()  # (x,y) location of mouse
                col = location[0] // SQ_SIZE
                row = location[1] // SQ_SIZE
                if sq_selected == (
                        row,
                        col):  # the user clicked the same square twice, undo
                    sq_selected = ()  # deselect/undo
                    player_clicks = []  # clear player clicks
                else:
                    sq_selected = (row, col)
                    player_clicks.append(
                        sq_selected)  # append for both first and second clicks
                if len(player_clicks) == 2:  # after 2nd click
                    move = ChessEngine.Move(player_clicks[0], player_clicks[1],
                                            gs.board)
                    print(move.get_chess_notation())
                    if move in valid_moves:
                        gs.make_move(move)
                        move_made = True
                        # reset user clicks and square
                        sq_selected = ()
                        player_clicks = []
                    else:
                        player_clicks = [sq_selected]

            # key handlers
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:  # undo when 'z' is pressed
                    gs.undo_move()
                    move_made = True

        if move_made:
            valid_moves = gs.get_valid_moves()
            move_made = False

        draw_game_state(screen, gs)
        clock.tick(MAX_FPS)
        p.display.flip()
示例#20
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.getValidMoves()
    moveMade = False  #flag vaieable for when a move is made
    animate = False
    loadImages()
    running = True
    sqSelected = ()  #last click from user(row, col)
    playerClicks = [
    ]  #keep track  of player clicks (two tuples: [(6,4), (4,4)]
    gameOver = False
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:
                    gs.undoMove()
                    moveMade = True
                    animate = False
                    gameOver = False
                if e.key == p.K_r:
                    gs = ChessEngine.GameState()
                    validMoves = gs.getValidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False
                    gameOver = False
            elif not gs.whiteToMove:
                best_move = minimax(gs)
                if best_move in gs.getValidMoves():
                    gs.makeMove(best_move)
                    moveMade = True
                    animate = True
            elif e.type == p.MOUSEBUTTONDOWN:
                if not gameOver:
                    location = p.mouse.get_pos()  #(x,y) location
                    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:
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], gs.board)
                        for i in range(len(validMoves)):
                            if move == validMoves[i]:
                                gs.makeMove(validMoves[i])
                                moveMade = True
                                animate = True
                                sqSelected = ()
                                playerClicks = []
                            if not moveMade:
                                playerClicks = [sqSelected]
        if moveMade:
            if animate:
                animateMove(gs.moveLog[-1], screen, gs.board, clock)
            validMoves = gs.getValidMoves()
            moveMade = False
            animate = False
        drawGameState(screen, gs, validMoves, sqSelected)

        if gs.checkMate:
            gameOver = True
            if gs.whiteToMove:
                drawText(screen, 'Black wins by checkmate')
            else:
                drawText(screen, 'White wins by checkmate')
        elif gs.staleMate:
            gameOver = True
            drawText(screen, 'Stalemate')

        clock.tick(MAX_FPS)
        p.display.flip()
示例#21
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.getvalidMoves()
    animate = False
    moveMade = False
    loadImages()
    running = True
    sqSelected = ()  #tuple of selected squares
    playerClicks = []  #keep track of player clicks
    gameOver = False  #for checkmate
    while running:
        for e in p.event.get():
            if e.type == p.QUIT:
                running = False
            elif e.type == p.MOUSEBUTTONDOWN:
                if not gameOver:
                    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:
                        move = ChessEngine.Move(playerClicks[0],
                                                playerClicks[1], gs.board)
                        print(move.getChessNotation())

                        if move in validMoves:
                            gs.makeMove(move)
                            moveMade = True
                            animate = True
                            sqSelected = ()
                            playerClicks = []
                        else:
                            playerClicks = [sqSelected]
            elif e.type == p.KEYDOWN:
                if e.key == p.K_z:
                    gs.undoMove()
                    moveMade = True
                    animate = False
                elif e.key == p.K_r:
                    gs = ChessEngine.GameState()
                    validMoves = gs.getvalidMoves()
                    sqSelected = ()
                    playerClicks = []
                    moveMade = False
                    animate = False

        if moveMade:
            if animate:
                animateMove(gs.movelog[-1], screen, gs.board, clock)
            validMoves = gs.getvalidMoves()
            moveMade = False
            animate = False

        drawGameState(screen, gs, validMoves, sqSelected)
        if gs.checkMate:
            gameOver = True
            if gs.WhiteToMove:
                drawText(screen, "Black wins by Checkmate!!")
            else:
                drawText(screen, "White wins by Checkmate!!")
        elif gs.staleMate:
            drawText(screen, "Stalemate!!")

        clock.tick(MAX_FPS)
        p.display.flip()