def check_computer_placement_move(self, piece, respawn): if self.pieces[piece].availability: #temp_inter = [i for i in self.pieces[piece].inital_position if i not in self.occupied_position_on_board] anirudh_index = [ (Helping_Class.convert_my_coordinate_to_anirudh(i[0], i[1]), i) for i in self.pieces[piece].inital_position ] print(piece, anirudh_index) temp_inter = [ index[1] for index in anirudh_index if not self.game_state.board[index[0]] ] if temp_inter: index = randint(0, len(temp_inter) - 1) where = temp_inter[index] self.selected_position = Helping_Class.selection_bar_reverse_mapping[ piece] #self.occupied_position_on_board.append(where) if temp_inter: temp_game_state = CP.game_data() temp_game_state = copy.deepcopy(self.game_state) data_convert = CP.Conversion_of_postion_name( piece, Helping_Class.selection_bar_reverse_mapping[piece], (where[0], where[1])) temp_game_state.update(data_convert.piece, int(data_convert.i_pos_ani()), int(data_convert.f_pos_ani())) temp_game_state.active_color = not temp_game_state.active_color fen = temp_game_state.generate_fen() board2 = chess.Board(fen=fen) print(board2) print(fen) print('board2.is_check()', board2.is_check()) if not board2.is_check(): self.selected_piece = piece data_convert = CP.Conversion_of_postion_name( self.selected_piece, self.selected_position, (where[0], where[1])) self.game_state.update(data_convert.piece, int(data_convert.i_pos_ani()), int(data_convert.f_pos_ani())) self.selected_piece = None self.selected_position = None self.pieces[piece].availability -= 1 self.computer_turn = False respawn = False self.black_pieces_on_board += 1 self.whose_move == 'white' self.computer_where, self.computer_to = None, None return respawn else: return respawn return respawn
def _check_valid_move_(self, x_adjusted, y_adjusted): fen_prev = self.game_state.generate_fen() #print(fen_prev) data_convert = CP.Conversion_of_postion_name(self.selected_piece, self.selected_position, (x_adjusted, y_adjusted)) board = chess.Board(fen=fen_prev) legal_moves_ = str(board.legal_moves) move = legal_moves_.split('(') move = move[1].split(')') move = move[0] move = move.split(',') for i in range(len(move)): move[i] = move[i].strip() #print(move) _move_ = CP.pgn(move, data_convert) #CP.Conversion_of_postion_name(self.selected_piece, self.selected_position ,(x_adjusted,y_adjusted)) if _move_: #place the piece self.game_state.update(data_convert.piece, int(data_convert.i_pos_ani()), int(data_convert.f_pos_ani())) return _move_ else: #maa choudau -> do nothing return None
def get_board_state(self): fen = self.game_state.generate_fen() board = chess.Board(fen=fen) #when check is their if board.is_check(): #print('1') if board.is_checkmate(): #print('2') checkmate = 'checkmate' if self.whose_move == 'white': piece_list = [ 'WRook', 'WQueen', 'WKnight', 'WBishop', 'WPawn' ] else: piece_list = [ 'BRook', 'BQueen', 'BKnight', 'BBishop', 'BPawn' ] for piece in piece_list: if self.pieces[piece].availability: for position in self.pieces[piece].inital_position: if not self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( position[0], position[1])]: print('piece', piece, ' at position :', position) temp_game_state = CP.game_data() temp_game_state = copy.deepcopy( self.game_state) data_convert = CP.Conversion_of_postion_name( piece, Helping_Class. selection_bar_reverse_mapping[piece], (position[0], position[1])) temp_game_state.update( data_convert.piece, int(data_convert.i_pos_ani()), int(data_convert.f_pos_ani())) temp_game_state.active_color = not temp_game_state.active_color fen = temp_game_state.generate_fen() board2 = chess.Board(fen=fen) #print(board2) #print(board2.is_check()) if not board2.is_check(): checkmate = 'pseudo_check' return checkmate return checkmate else: #normal check return 'check' else: #normal game return 'normal'
def main_board_maintenance(self, x_cor, y_cor, situation): """ ` clicked on board | --------------------------------- | | yes No | | selected from where nothing to do | -------------------------------------------------------------------------------------- | | | selection_bar Board no selection | | | is clicked on valid position is clicked on valid position(Rajan) clicked on empty slot | | | (it returns pgn) | | | | | | | yes no yes no ----------------------------- | | | | | | place it (if his piece) does move contain 'x' (capture) select piece no yes (if not check) change selection | | (Ask & Discuss) | | yes no is it his piece pass | | | capture and update both pieces state just place piece ----------------------- | | yes no | | select the piece pass """ for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() pygame.quit() quit() if event.type == pygame.MOUSEBUTTONDOWN: #'check','normal','checkmate' x_adjusted, y_adjusted = Helping_Class.convert_coordinate( x_cor, y_cor, from_where='board') #print(x_adjusted/80,y_adjusted/80) print('situation', situation) if self.selected_from_selection_bar: x_adjusted, y_adjusted = Helping_Class.convert_coordinate( x_cor, y_cor, from_where='board') temp_game_state = CP.game_data() temp_game_state = copy.deepcopy(self.game_state) data_convert = CP.Conversion_of_postion_name( self.selected_piece, Helping_Class.selection_bar_reverse_mapping[ self.selected_piece], (x_adjusted, y_adjusted)) temp_game_state.update(data_convert.piece, int(data_convert.i_pos_ani()), int(data_convert.f_pos_ani())) temp_game_state.active_color = not temp_game_state.active_color fen = temp_game_state.generate_fen() board2 = chess.Board(fen=fen) print(board2) print(fen) print('board2.is_check()', board2.is_check()) #now we need to place the piece on board if self.game_state.board[ Helping_Class.convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)] == None: #print(self.selected_position) if not board2.is_check(): if self._check_valid_position_( x_adjusted, y_adjusted): self.place_piece_on_board_from_selection_bar( x_adjusted, y_adjusted) #rajan's #print(self.selected_piece) #print(self.selected_position) data_convert = CP.Conversion_of_postion_name( self.selected_piece, self.selected_position, (x_adjusted, y_adjusted)) self.game_state.update( data_convert.piece, int(data_convert.i_pos_ani()), int(data_convert.f_pos_ani())) #self._where,self._to = (x_adjusted,y_adjusted),self.selected_position self.selected_piece = None self.selected_position = None else: pass #board position is filled then nothing to do else: #if his piece change selection self.selected_from_selection_bar = False self.selected_from_board = True self.selected_piece = self.anirudh_2_pritish[ self.game_state.board[ Helping_Class.convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]] self.selected_position = (x_adjusted, y_adjusted) elif self.selected_from_board: #print('inside selection bar board option') x_adjusted, y_adjusted = Helping_Class.convert_coordinate( x_cor, y_cor, from_where='board') omega = True if self.selected_position: if self.selected_position == (x_adjusted, y_adjusted): omega = False #print(self.selected_position,(x_adjusted,y_adjusted)) if omega: move = self._check_valid_move_(x_adjusted, y_adjusted) #print(move) if omega: if move: #if move contains x then we have update state of captured piece #else just update selected piece #print("correct move") self.capture_piece_update_board_or_place_piece( move, x_adjusted, y_adjusted) else: #select the piece if self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]: self.selected_piece = self.anirudh_2_pritish[ self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]] self.selected_position = (x_adjusted, y_adjusted) self.selected_from_board = True else: x_adjusted, y_adjusted = Helping_Class.convert_coordinate( x_cor, y_cor, from_where='board') if self.game_state.board[ Helping_Class.convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]: #select the piece if self.whose_move == 'white': if 'W' in self.anirudh_2_pritish[ self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]]: self.selected_piece = self.anirudh_2_pritish[ self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]] self.selected_from_board = True self.selected_position = (x_adjusted, y_adjusted) else: #nothing to do pass elif self.whose_move == 'black': if 'B' in self.anirudh_2_pritish[ self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]]: self.selected_piece = self.anirudh_2_pritish[ self.game_state.board[ Helping_Class. convert_my_coordinate_to_anirudh( x_adjusted, y_adjusted)]] self.selected_from_board = True self.selected_position = (x_adjusted, y_adjusted) else: #nothing to do pass else: #it is none means nothing is their so nothing to do pass else: #print("not_pressed") pass