def put(self, board): if board == self.starting_board: self.reset() if len(self.boards) == 0: self.boards.append(board) self._board = board self.white_to_play = True elif board != self.boards[-1]: self.boards.append(board) self._board = board from_square, to_square = Rules.last_move(self.boards[-1], self.boards[-2], self.en_passant) if to_square is not None: if Pieces.is_white(self.boards[-1][to_square]): self.white_to_play = False else: self.white_to_play = True en_passant = Rules.find_en_passant(self.boards[-1], self.boards[-2]) self.en_passant = en_passant wk, wq, bk, bq = Rules.castle_rights(board) self.wk *= wk self.wq *= wq self.bk *= bk self.bq *= bq else: self.en_passant = None self.white_to_play = None self.wk, self.wq, self.bk, self.bq = False, False, False, False
def make_move(self, move: str, white): match = self.pattern.match(move) piece_type = Pieces.get( 'p' if match.group(1) == '' else match.group(1), white=True) move = match.group(2).replace('x', '') promotion = match.group(3) rules = Rules() rules.put(self._board, self.en_passant, self.wk, self.wq, self.bk, self.bq, promote=None if promotion == '' else promotion) exact_row, exact_col = '', '' if move.lower() == 'o-o': if white: move = 'g1' else: move = 'g8' piece_type = 'K' elif move.lower() == 'o-o-o': if white: move = 'c1' else: move = 'c8' piece_type = 'K' elif len(move) == 3: char = move[0] move = move[-2:] if char.isdigit(): exact_col = char else: exact_row = char pieces, possible_moves = rules.pieces, rules.possible_moves for piece, moves in zip(pieces, possible_moves): if piece_type == Pieces.get(piece.type, True) and piece.is_white == white and move in moves \ and exact_row in piece.square and exact_col in piece.square: board_copy = self._board.copy() board_copy.update_squares(*rules.make_move(piece.square, move)) rules_check = Rules() rules_check.put(board_copy, self.en_passant, self.wk, self.wq, self.bk, self.bq) status = rules_check.status(white) if status == Rules.NORMAL: previous_board = self._board.copy() self._board.update_squares( *rules.make_move(piece.square, move)) self.update_castle(*Rules.castle_rights(self._board)) self.en_passant = Rules.find_en_passant( self._board, previous_board) break