def main(window, clock, versusPlayer): if versusPlayer: pyg.display.set_caption('vs Player') else: pyg.display.set_caption('vs AI') game = ChessBoard.Board() # creating the 2D list representation of the chess board validMoves = game.getValidMoves() moveMade = False # Boolean when a move is made so validMoves does not prematurely makes the appropriate moves create_chess_pieces() running = True squareSelected = () # Will keep track of the last click of the user playerClicks = [] # Keep track of player clicks in (row, col) format i.e. [(1, 2), (1, 4)] gameOver = False while running: for event in pyg.event.get(): if event.type == pyg.QUIT: running = False elif event.type == pyg.MOUSEBUTTONDOWN: if not gameOver: location = pyg.mouse.get_pos() col = location[0] // SQUARE_SIZE row = location[1] // SQUARE_SIZE if squareSelected == (row, col): # If user clicked same square twice, reset squareSelected = () playerClicks = [] else: # Add it to player clicks if a different square was clicked squareSelected = (row, col) playerClicks.append(squareSelected) if len(playerClicks) == 2: # Player clicked two different squares move = ChessBoard.Move(playerClicks[0], playerClicks[1], game.board) # print(move.getChessNotation()) for i in range(len(validMoves)): if move == validMoves[i]: game.makeMove(validMoves[i]) # game.computeScore() moveMade = True squareSelected = () playerClicks = [] if not moveMade: # Invalid second click/Move i.e (second click on friendly piece) playerClicks = [squareSelected] elif event.type == pyg.KEYDOWN: if event.key == pyg.K_u: # Undo when u is pressed game.undoMove() moveMade = True if event.key == pyg.K_r: # Reset game when r is pressed game = ChessBoard.Board() validMoves = game.getValidMoves() squareSelected = () playerClicks = [] moveMade = False if event.key == pyg.K_ESCAPE: running = False if moveMade: validMoves = game.getValidMoves() moveMade = False if not gameOver: draw_game(window, game, validMoves, squareSelected, game.movesLog) if game.checkMate: gameOver = True if game.whitesMove: draw_text(window, 'Black Wins By Checkmate') else: draw_text(window, 'White Wins By Checkmate') elif game.staleMate: gameOver = True draw_text(window, 'Stalemate') if not versusPlayer and not game.whitesMove: game.aiMove() moveMade = True clock.tick(MAX_FPS) pyg.display.flip()