pgn_read/core/game.py
def append_pawn_promote_move(self, match):
        """Append pawn promotion move token to game score and update board
        state.

        Put game in error state if the token represents an illegal move.

        """
        if self._movetext_offset is None:
            if not self.set_initial_position():
                self.append_token_and_set_error(match)
                return
            self._ravstack.append([0])
            self._movetext_offset = len(self._text)
        group = match.group
        if group(IFG_PAWN_PROMOTE_TO_RANK) is None:
            self.append_token_and_set_error(match)
            return
        piece_placement_data = self._piece_placement_data

        # Promotion move and capture.

        if group(IFG_PAWN_CAPTURE_TO_FILE):
            destination = (group(IFG_PAWN_CAPTURE_TO_FILE) +
                           group(IFG_PAWN_PROMOTE_TO_RANK))
            pawn_capture_from_file = group(IFG_PAWN_FROM_FILE)
            if self._active_color == FEN_WHITE_ACTIVE:
                fullmove_number_for_next_halfmove = self._fullmove_number
                source_squares_index = FEN_WHITE_PAWN + PGN_CAPTURE_MOVE
            else:
                fullmove_number_for_next_halfmove = self._fullmove_number + 1
                source_squares_index = FEN_BLACK_PAWN + PGN_CAPTURE_MOVE
            try:
                source_squares = SOURCE_SQUARES[source_squares_index
                                                ][destination]
            except KeyError:
                self.append_token_and_set_error(match)
                return
            for source in source_squares:
                if source.startswith(pawn_capture_from_file):
                    try:
                        if FEN_PAWNS.get(piece_placement_data[source].name
                                         ) == self._active_color:
                            piece = piece_placement_data[source]
                            break
                    except KeyError:
                        self.append_token_and_set_error(match)
                        return
            else:
                self.append_token_and_set_error(match)
                return
            if destination not in piece_placement_data:
                if self._en_passant_target_square == FEN_NULL:
                    self.append_token_and_set_error(match)
                    return
                capture_square = EN_PASSANT_TARGET_SQUARES.get(group()[:4])
                if not capture_square:
                    self.append_token_and_set_error(match)
                    return
                if capture_square not in piece_placement_data:
                    self.append_token_and_set_error(match)
                    return
                if FEN_PAWNS.get(piece_placement_data[capture_square].name
                                 ) == self._active_color:
                    self.append_token_and_set_error(match)
                    return
            elif piece_placement_data[source
                                      ].color == piece_placement_data[
                                          destination].color:
                self.append_token_and_set_error(match)
                return
            else:
                capture_square = destination
            remove = ((capture_square,
                       piece_placement_data[capture_square]),
                      (piece.square.name, piece))
            self.remove_piece_from_board(remove[0])
            self.remove_piece_from_board(remove[1])
            promoted_pawn = piece.promoted_pawn(
                PROMOTED_PIECE_NAME[self._active_color
                                    ][group(IFG_PAWN_PROMOTE_PIECE)],
                destination)
            place = destination, promoted_pawn
            self.place_piece_on_board(place)
            if self.is_square_attacked_by_other_side(
                self._pieces_on_board[PIECE_TO_KING[promoted_pawn.name]
                                      ][0].square.name):
                self.remove_piece_from_board(place)
                self.place_piece_on_board(remove[0])
                self.place_piece_on_board(remove[1])
                self.append_token_and_set_error(match)
                return
            self._text.append(group())
            self.modify_board_state(
                ((remove,
                  self._active_color,
                  self._castling_availability,
                  self._en_passant_target_square,
                  self._halfmove_clock,
                  self._fullmove_number),
                 ((place,),
                  OTHER_SIDE[self._active_color],
                  self.get_castling_options_after_move_applied(remove),
                  FEN_NULL,
                  0,
                  fullmove_number_for_next_halfmove),
                 ))
            (self._active_color,
             self._castling_availability,
             self._en_passant_target_square,
             self._halfmove_clock,
             self._fullmove_number,
             ) = self._position_deltas[-1][1][1:]
            self.append_check_indicator()
            return

        # Promotion move without capture.

        destination = (group(IFG_PAWN_FROM_FILE) +
                       group(IFG_PAWN_PROMOTE_TO_RANK))
        if destination in piece_placement_data:
            self.append_token_and_set_error(match)
            return
        if self._active_color == FEN_WHITE_ACTIVE:
            fullmove_number_for_next_halfmove = self._fullmove_number
            source_squares_index = FEN_WHITE_PAWN
        else:
            fullmove_number_for_next_halfmove = self._fullmove_number + 1
            source_squares_index = FEN_BLACK_PAWN
        try:
            source_squares = SOURCE_SQUARES[source_squares_index][destination]
        except KeyError:
            self.append_token_and_set_error(match)
            return
        for source in source_squares:
            if source in piece_placement_data:
                piece = piece_placement_data[source]
                if FEN_PAWNS.get(piece.name) == self._active_color:
                    if self.line_empty(destination, source):
                        break
        else:
            self.append_token_and_set_error(match)
            return
        remove = piece.square.name, piece
        self.remove_piece_from_board(remove)
        new_en_passant_target_square = FEN_NULL
        promoted_pawn = piece.promoted_pawn(
            PROMOTED_PIECE_NAME[self._active_color
                                ][group(IFG_PAWN_PROMOTE_PIECE)],
            destination)
        place = destination, promoted_pawn
        self.place_piece_on_board(place)
        if self.is_square_attacked_by_other_side(
            self._pieces_on_board[PIECE_TO_KING[promoted_pawn.name
                                                ]][0].square.name):
            self.remove_piece_from_board(place)
            self.place_piece_on_board(remove)
            self.append_token_and_set_error(match)
            return
        self._text.append(group())
        self.modify_board_state(
            (((remove,),
              self._active_color,
              self._castling_availability,
              self._en_passant_target_square,
              self._halfmove_clock,
              self._fullmove_number),
             ((place,),
              OTHER_SIDE[self._active_color],
              self.get_castling_options_after_move_applied((remove,)),
              new_en_passant_target_square,
              0,
              fullmove_number_for_next_halfmove),
             ))
        (self._active_color,
         self._castling_availability,
         self._en_passant_target_square,
         self._halfmove_clock,
         self._fullmove_number,
         ) = self._position_deltas[-1][1][1:]
        self.append_check_indicator()
Append pawn promotion move token to game score and update board
        state.

        Put game in error state if the token represents an illegal move.

        
Append pawn promotion move token to game score and update board
state.

Put game in error state if the token represents an illegal move.
Append pawn promotion move token to game score and update board state . Put game in error state if the token represents an illegal move .
def append_pawn_promote_move(self, match):
        if self._movetext_offset is None:
            if not self.set_initial_position():
                self.append_token_and_set_error(match)
                return
            self._ravstack.append([0])
            self._movetext_offset = len(self._text)
        group = match.group
        if group(IFG_PAWN_PROMOTE_TO_RANK) is None:
            self.append_token_and_set_error(match)
            return
        piece_placement_data = self._piece_placement_data
        if group(IFG_PAWN_CAPTURE_TO_FILE):
            destination = (group(IFG_PAWN_CAPTURE_TO_FILE) +
                           group(IFG_PAWN_PROMOTE_TO_RANK))
            pawn_capture_from_file = group(IFG_PAWN_FROM_FILE)
            if self._active_color == FEN_WHITE_ACTIVE:
                fullmove_number_for_next_halfmove = self._fullmove_number
                source_squares_index = FEN_WHITE_PAWN + PGN_CAPTURE_MOVE
            else:
                fullmove_number_for_next_halfmove = self._fullmove_number + 1
                source_squares_index = FEN_BLACK_PAWN + PGN_CAPTURE_MOVE
            try:
                source_squares = SOURCE_SQUARES[source_squares_index
                                                ][destination]
            except KeyError:
                self.append_token_and_set_error(match)
                return
            for source in source_squares:
                if source.startswith(pawn_capture_from_file):
                    try:
                        if FEN_PAWNS.get(piece_placement_data[source].name
                                         ) == self._active_color:
                            piece = piece_placement_data[source]
                            break
                    except KeyError:
                        self.append_token_and_set_error(match)
                        return
            else:
                self.append_token_and_set_error(match)
                return
            if destination not in piece_placement_data:
                if self._en_passant_target_square == FEN_NULL:
                    self.append_token_and_set_error(match)
                    return
                capture_square = EN_PASSANT_TARGET_SQUARES.get(group()[:4])
                if not capture_square:
                    self.append_token_and_set_error(match)
                    return
                if capture_square not in piece_placement_data:
                    self.append_token_and_set_error(match)
                    return
                if FEN_PAWNS.get(piece_placement_data[capture_square].name
                                 ) == self._active_color:
                    self.append_token_and_set_error(match)
                    return
            elif piece_placement_data[source
                                      ].color == piece_placement_data[
                                          destination].color:
                self.append_token_and_set_error(match)
                return
            else:
                capture_square = destination
            remove = ((capture_square,
                       piece_placement_data[capture_square]),
                      (piece.square.name, piece))
            self.remove_piece_from_board(remove[0])
            self.remove_piece_from_board(remove[1])
            promoted_pawn = piece.promoted_pawn(
                PROMOTED_PIECE_NAME[self._active_color
                                    ][group(IFG_PAWN_PROMOTE_PIECE)],
                destination)
            place = destination, promoted_pawn
            self.place_piece_on_board(place)
            if self.is_square_attacked_by_other_side(
                self._pieces_on_board[PIECE_TO_KING[promoted_pawn.name]
                                      ][0].square.name):
                self.remove_piece_from_board(place)
                self.place_piece_on_board(remove[0])
                self.place_piece_on_board(remove[1])
                self.append_token_and_set_error(match)
                return
            self._text.append(group())
            self.modify_board_state(
                ((remove,
                  self._active_color,
                  self._castling_availability,
                  self._en_passant_target_square,
                  self._halfmove_clock,
                  self._fullmove_number),
                 ((place,),
                  OTHER_SIDE[self._active_color],
                  self.get_castling_options_after_move_applied(remove),
                  FEN_NULL,
                  0,
                  fullmove_number_for_next_halfmove),
                 ))
            (self._active_color,
             self._castling_availability,
             self._en_passant_target_square,
             self._halfmove_clock,
             self._fullmove_number,
             ) = self._position_deltas[-1][1][1:]
            self.append_check_indicator()
            return
        destination = (group(IFG_PAWN_FROM_FILE) +
                       group(IFG_PAWN_PROMOTE_TO_RANK))
        if destination in piece_placement_data:
            self.append_token_and_set_error(match)
            return
        if self._active_color == FEN_WHITE_ACTIVE:
            fullmove_number_for_next_halfmove = self._fullmove_number
            source_squares_index = FEN_WHITE_PAWN
        else:
            fullmove_number_for_next_halfmove = self._fullmove_number + 1
            source_squares_index = FEN_BLACK_PAWN
        try:
            source_squares = SOURCE_SQUARES[source_squares_index][destination]
        except KeyError:
            self.append_token_and_set_error(match)
            return
        for source in source_squares:
            if source in piece_placement_data:
                piece = piece_placement_data[source]
                if FEN_PAWNS.get(piece.name) == self._active_color:
                    if self.line_empty(destination, source):
                        break
        else:
            self.append_token_and_set_error(match)
            return
        remove = piece.square.name, piece
        self.remove_piece_from_board(remove)
        new_en_passant_target_square = FEN_NULL
        promoted_pawn = piece.promoted_pawn(
            PROMOTED_PIECE_NAME[self._active_color
                                ][group(IFG_PAWN_PROMOTE_PIECE)],
            destination)
        place = destination, promoted_pawn
        self.place_piece_on_board(place)
        if self.is_square_attacked_by_other_side(
            self._pieces_on_board[PIECE_TO_KING[promoted_pawn.name
                                                ]][0].square.name):
            self.remove_piece_from_board(place)
            self.place_piece_on_board(remove)
            self.append_token_and_set_error(match)
            return
        self._text.append(group())
        self.modify_board_state(
            (((remove,),
              self._active_color,
              self._castling_availability,
              self._en_passant_target_square,
              self._halfmove_clock,
              self._fullmove_number),
             ((place,),
              OTHER_SIDE[self._active_color],
              self.get_castling_options_after_move_applied((remove,)),
              new_en_passant_target_square,
              0,
              fullmove_number_for_next_halfmove),
             ))
        (self._active_color,
         self._castling_availability,
         self._en_passant_target_square,
         self._halfmove_clock,
         self._fullmove_number,
         ) = self._position_deltas[-1][1][1:]
        self.append_check_indicator()
def append_pawn_promote_move ( self , match ) : if self . _movetext_offset is None : if not self . set_initial_position ( ) : self . append_token_and_set_error ( match ) return self . _ravstack . append ( [ 0 ] ) self . _movetext_offset = len ( self . _text ) group = match . group if group ( IFG_PAWN_PROMOTE_TO_RANK ) is None : self . append_token_and_set_error ( match ) return piece_placement_data = self . _piece_placement_data if group ( IFG_PAWN_CAPTURE_TO_FILE ) : destination = ( group ( IFG_PAWN_CAPTURE_TO_FILE ) + group ( IFG_PAWN_PROMOTE_TO_RANK ) ) pawn_capture_from_file = group ( IFG_PAWN_FROM_FILE ) if self . _active_color == FEN_WHITE_ACTIVE : fullmove_number_for_next_halfmove = self . _fullmove_number source_squares_index = FEN_WHITE_PAWN + PGN_CAPTURE_MOVE else : fullmove_number_for_next_halfmove = self . _fullmove_number + 1 source_squares_index = FEN_BLACK_PAWN + PGN_CAPTURE_MOVE try : source_squares = SOURCE_SQUARES [ source_squares_index ] [ destination ] except KeyError : self . append_token_and_set_error ( match ) return for source in source_squares : if source . startswith ( pawn_capture_from_file ) : try : if FEN_PAWNS . get ( piece_placement_data [ source ] . name ) == self . _active_color : piece = piece_placement_data [ source ] break except KeyError : self . append_token_and_set_error ( match ) return else : self . append_token_and_set_error ( match ) return if destination not in piece_placement_data : if self . _en_passant_target_square == FEN_NULL : self . append_token_and_set_error ( match ) return capture_square = EN_PASSANT_TARGET_SQUARES . get ( group ( ) [ : 4 ] ) if not capture_square : self . append_token_and_set_error ( match ) return if capture_square not in piece_placement_data : self . append_token_and_set_error ( match ) return if FEN_PAWNS . get ( piece_placement_data [ capture_square ] . name ) == self . _active_color : self . append_token_and_set_error ( match ) return elif piece_placement_data [ source ] . color == piece_placement_data [ destination ] . color : self . append_token_and_set_error ( match ) return else : capture_square = destination remove = ( ( capture_square , piece_placement_data [ capture_square ] ) , ( piece . square . name , piece ) ) self . remove_piece_from_board ( remove [ 0 ] ) self . remove_piece_from_board ( remove [ 1 ] ) promoted_pawn = piece . promoted_pawn ( PROMOTED_PIECE_NAME [ self . _active_color ] [ group ( IFG_PAWN_PROMOTE_PIECE ) ] , destination ) place = destination , promoted_pawn self . place_piece_on_board ( place ) if self . is_square_attacked_by_other_side ( self . _pieces_on_board [ PIECE_TO_KING [ promoted_pawn . name ] ] [ 0 ] . square . name ) : self . remove_piece_from_board ( place ) self . place_piece_on_board ( remove [ 0 ] ) self . place_piece_on_board ( remove [ 1 ] ) self . append_token_and_set_error ( match ) return self . _text . append ( group ( ) ) self . modify_board_state ( ( ( remove , self . _active_color , self . _castling_availability , self . _en_passant_target_square , self . _halfmove_clock , self . _fullmove_number ) , ( ( place , ) , OTHER_SIDE [ self . _active_color ] , self . get_castling_options_after_move_applied ( remove ) , FEN_NULL , 0 , fullmove_number_for_next_halfmove ) , ) ) ( self . _active_color , self . _castling_availability , self . _en_passant_target_square , self . _halfmove_clock , self . _fullmove_number , ) = self . _position_deltas [ - 1 ] [ 1 ] [ 1 : ] self . append_check_indicator ( ) return destination = ( group ( IFG_PAWN_FROM_FILE ) + group ( IFG_PAWN_PROMOTE_TO_RANK ) ) if destination in piece_placement_data : self . append_token_and_set_error ( match ) return if self . _active_color == FEN_WHITE_ACTIVE : fullmove_number_for_next_halfmove = self . _fullmove_number source_squares_index = FEN_WHITE_PAWN else : fullmove_number_for_next_halfmove = self . _fullmove_number + 1 source_squares_index = FEN_BLACK_PAWN try : source_squares = SOURCE_SQUARES [ source_squares_index ] [ destination ] except KeyError : self . append_token_and_set_error ( match ) return for source in source_squares : if source in piece_placement_data : piece = piece_placement_data [ source ] if FEN_PAWNS . get ( piece . name ) == self . _active_color : if self . line_empty ( destination , source ) : break else : self . append_token_and_set_error ( match ) return remove = piece . square . name , piece self . remove_piece_from_board ( remove ) new_en_passant_target_square = FEN_NULL promoted_pawn = piece . promoted_pawn ( PROMOTED_PIECE_NAME [ self . _active_color ] [ group ( IFG_PAWN_PROMOTE_PIECE ) ] , destination ) place = destination , promoted_pawn self . place_piece_on_board ( place ) if self . is_square_attacked_by_other_side ( self . _pieces_on_board [ PIECE_TO_KING [ promoted_pawn . name ] ] [ 0 ] . square . name ) : self . remove_piece_from_board ( place ) self . place_piece_on_board ( remove ) self . append_token_and_set_error ( match ) return self . _text . append ( group ( ) ) self . modify_board_state ( ( ( ( remove , ) , self . _active_color , self . _castling_availability , self . _en_passant_target_square , self . _halfmove_clock , self . _fullmove_number ) , ( ( place , ) , OTHER_SIDE [ self . _active_color ] , self . get_castling_options_after_move_applied ( ( remove , ) ) , new_en_passant_target_square , 0 , fullmove_number_for_next_halfmove ) , ) ) ( self . _active_color , self . _castling_availability , self . _en_passant_target_square , self . _halfmove_clock , self . _fullmove_number , ) = self . _position_deltas [ - 1 ] [ 1 ] [ 1 : ] self . append_check_indicator ( )
Append pawn promotion move token to game score and update board
state.
Append pawn promotion move token to game score and update board state .
"""Append pawn promotion move token to game score and update board
        state.

        Put game in error state if the token represents an illegal move.

        """ # Promotion move and capture. # Promotion move without capture.
self
match