def main(): p.init() p.display.set_caption('Chess -B Nayak') 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 move is made animate = False # Flag variable for when we should animate a move loadImages() running = True sqSelected = ( ) # no squareis selected , keep track of the last click of the user (tuple(row,column)) playerClicks = [] # keep track of player clicks [two tuple: [(6,4),(6,8)]] gameOver = False while running: for event in p.event.get(): if event.type == p.QUIT: running = False # Mouse Button Down elif event.type == p.MOUSEBUTTONDOWN: if not gameOver: location = p.mouse.get_pos( ) #get the mouse position as a tuple (x,y) col = location[0] // SQ_SIZE row = location[1] // SQ_SIZE if sqSelected == (row, col): # The user clicked twice sqSelected = () # deselected the square playerClicks = [] # clear player clicks else: sqSelected = (row, col) playerClicks.append( sqSelected ) # append for both first and second click if len(playerClicks) == 2: move = ChessEngine.Move(playerClicks[0], playerClicks[1], gs.board) print(move.getChessNotaion()) for select_move in validMoves: if move == select_move: gs.makeMove(select_move) moveMade = True animate = True sqSelected = () # Reset user clicks playerClicks = [] if not moveMade: playerClicks = [sqSelected] # Key Button Down elif event.type == p.KEYDOWN: if event.key == p.K_z: #Undo when 'z' is pressed gs.undoMove() moveMade = True animate = False if event.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() moveMade = False animate = False drawGameState(screen, gs, validMoves, sqSelected) if gs.checkMate: gameOver = True if gs.whiteToMove: drawText(screen, ' Black Wins CHECKMATE') else: drawText(screen, ' White Wins CHECKMATE') elif gs.staleMate: gameOver = True drawText(screen, ' Stalemate') clock.tick(MAX_FPS) p.display.flip() # Update the full display Surface to the screen
def main(): p.init() clock = p.time.Clock() start_screen = True end_screen = False running = True is_fischer = False is_random = False acknowledge_screen(clock) screen = p.display.set_mode((WIDTH+50, 50+HEIGHT)) screen.fill(p.Color(0x000F0F)) gs = ChessEngine.GameState() prev_board = gs.board # print(chess.Board()) validMoves = gs.getValidMoves() moveMade = False # flag variable when a move is made animate = False # flag variable for when we should animate a move loadImages() initial = () final = () sqSelected = () # keeps track of last call of user playerClicks = [] # keeps track of players click pvcn = False pvcf = False full_move = 1 half_move = 0 prev_half = 0 preference = 0 flag_undo = False prev_bstring = -1 #if_fill_or_fischer = False msg = '' while running: print_board = board_to_fen( gs.board, gs.whiteToMove, gs.enpassantPossible, half_move, full_move) print(print_board) # print(gs.enpassantPossible) # print(convert_c2p(gs.enpassantPossible)) if start_screen: return_val = show_startscreen(clock) screen = p.display.set_mode((WIDTH+50, 50+HEIGHT)) screen.fill(p.Color(0x000F0F)) running = return_val[0] is_fischer = return_val[1] is_random = return_val[2] pvcn = return_val[3] pvcf = return_val[4] full_move = 1 half_move = 0 prev_half = 0 prev_bstring = -1 if pvcf or pvcn: temp_pref = input("Enter Your Preference (W/B) :") if(temp_pref == 'W' or temp_pref == 'w'): preference = 1 elif(temp_pref == 'B' or temp_pref == 'b'): preference = 2 else: print(" Wrong Preference ") preference = 0 if is_random: prev_board = is_fill_function() gs.board = prev_board is_random = False # print("2") elif is_fischer or pvcf: prev_board = is_fischer_function() gs.board = prev_board if is_fischer: is_fischer = False flag_temp = 0 for k1 in range(0,8): if gs.board[0][k1][1]=='R' and not flag_temp: flag_temp+=1 gs.blackleft = k1 elif flag_temp: gs.blackright = k1 flag_temp = 0 for k1 in range(0,8): if gs.board[7][k1][1]=='R' and not flag_temp: flag_temp+=1 gs.whiteleft = k1 elif flag_temp: gs.whiteright = k1 print(gs.blackright) print(gs.blackleft) print(gs.whiteright) print(gs.whiteleft) elif not is_fischer or not is_random or not pvcf: gs.board = [ ["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"], ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"], ["--", "--", "--", "--", "--", "--", "--", "--"], ["--", "--", "--", "--", "--", "--", "--", "--"], ["--", "--", "--", "--", "--", "--", "--", "--"], ["--", "--", "--", "--", "--", "--", "--", "--"], ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"], ["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]] gs.gameOver = False start_screen = False continue # print(prev_board) # inside_c=False for e in p.event.get(): if e.type == p.QUIT: running = False # mouse handler elif e.type == p.MOUSEBUTTONDOWN : # print("Inside 2nd") if not gs.gameOver: # check here for bug if ((preference == 0) or (preference == 1 and gs.whiteToMove) or (preference == 2 and gs.whiteToMove == 0)): # inside_c=False location = p.mouse.get_pos() # get mouse coordinates print(location) if location[0]>=800: location=(799,location[1]) if location[1]>=800: location=(location[0],799) col = location[0]//SQ_SIZE row = location[1]//SQ_SIZE if sqSelected == (row, col): # reset if clicked on same block sqSelected = () playerClicks = [] else: sqSelected = (row, col) # append for both first and second clicks playerClicks.append(sqSelected) 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] elif e.type == p.MOUSEBUTTONUP: # print("Inside 1st") if ((preference == 1 and not gs.whiteToMove) or (preference == 2 and gs.whiteToMove)): # inside_c=True move_stime = time() move = next_move(gs) print(f"move time {time()-move_stime}\n") print(move.getChessNotation()) for i in range(len(validMoves)): if move == validMoves[i]: gs.makeMove(validMoves[i], by_AI=True) moveMade = True animate = True # key handler elif e.type == p.KEYDOWN: if e.key == p.K_z: # undo a move by pressing z # need to implement this using undo button also %% if(gs.whiteToMove and full_move > 1): #print("hello 1") full_move -= 1 if(half_move > 0): half_move -= 1 flag_undo = True 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() final = () initial = () sqSelected = () final = () initial = () playerClicks = [] moveMade = False animate = False gs.staleMate = False gs.checkMate = False full_move = 1 half_move = 0 prev_half = 0 prev_bstring = -1 gs.checks = False gs.board = prev_board if e.key == p.K_m: gs = ChessEngine.GameState() validMoves = gs.getValidMoves() final = () initial = () sqSelected = () final = () initial = () playerClicks = [] moveMade = False animate = False gs.staleMate = False gs.checkMate = False gs.checks = False start_screen = True continue if moveMade: if(gs.whiteToMove and not flag_undo): #print("hello 3") full_move += 1 if(not flag_undo): half_move += 1 flag_undo = False if(len(gs.moveLog) >= 1): r, c = (gs.moveLog[-1].endRow, gs.moveLog[-1].endCol) if(gs.board[r][c][1] == 'p'): half_move = 0 if animate: animateMove(gs.moveLog[-1], screen, gs.board, clock) validMoves = gs.getValidMoves() #board_to_fen(gs.board,gs.whiteToMove,gs.enpassantPossible,half_move,full_move ) var_board = count_pieces(print_board) if(var_board != prev_bstring): half_move = 0 prev_bstring = var_board moveMade = False animate = False # if(gs.moveLog[-1]): if(len(gs.moveLog) >= 1): final = (gs.moveLog[-1].endRow, gs.moveLog[-1].endCol) initial = (gs.moveLog[-1].startRow, gs.moveLog[-1].startCol) else: initial = () final = () drawGameState(screen, gs, validMoves, sqSelected, initial, final) if gs.checkMate: gs.gameOver = True if gs.whiteToMove: drawText(screen, 'Black wins by checkmate') msg = 'Black wins by checkmate' else: drawText(screen, 'White wins by checkmate') msg = 'White wins by checkmate' sleep(1) end_screen = True elif gs.staleMate: gs.gameOver = True drawText(screen, 'Stalemate') msg = 'Draw' end_screen = True if end_screen: return_val = show_endscreen(clock, msg) if not return_val: running = False gs = ChessEngine.GameState() validMoves = gs.getValidMoves() gs.gameOver = False final = () initial = () sqSelected = () final = () initial = () playerClicks = [] moveMade = False animate = False end_screen = False gs.staleMate = False gs.checkMate = False gs.checks = False start_screen = True clock.tick(MAX_FPS) p.display.flip()