def init(data): data.mode = "beginning" data.y = data.height / 4 * 3.3 data.x1 = data.width * .098 data.x2 = data.width * .3629 data.x3 = data.width / 20 * 12.6 data.x4 = data.width / 19 * 17 data.r = data.width / 17 data.margin = int(data.width / 31) data.mainBoard = CB.Board(data.width, data.height) data.mainBoard.makeBoard() data.player = "White" data.timerFiredCount = 0 data.origRow = None data.origCol = None data.newRow = None data.newCol = None data.moved = False data.hover = None data.checkMate = None data.instructImage = "./MoveImages/MovePawn.png" data.music = "On" try: return data.other.ID except: data.me = Person(data.mainBoard) data.other = Person(data.mainBoard)
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()