lib/chess/state.rb
###
# Gets a bitmask of possible problems with the position.
# Move making, generation and validation are only guaranteed to work on
# a completely valid board.
###
Gets a bitmask of possible problems with the position.
Move making, generation and validation are only guaranteed to work on
a completely valid board.
Gets a bitmask of possible problems with the position . Move making generation and validation are only guaranteed to work on a completely valid board .
def status
      errors = Board::STATUS_VALID
      if !occupied_co[Board::WHITE] & kings != 0
        errors = errors | Board::STATUS_NO_WHITE_KING
      end
      if !occupied_co[Board::BLACK] & kings != 0
        errors = erorrs | Board::STATUS_NO_BLACK_KING
      end
      if pop_count(occupied & kings) > 2
        errors = errors | Board::STATUS_TOO_MANY_KINGS
      end
      if pop_count(occupied_co[Board::WHITE] & pawns) > 8
        errors = errors | Board::STATUS_TOO_MANY_WHITE_PAWNS
      end
      if pop_count(occupied_co[Board::BLACK] & pawns) > 8
        errors = errors | Board::STATUS_TOO_MANY_BLACK_PAWNS
      end
      if pawns & (Board::BB_RANK_1 | Board::BB_RANK_8) != 0
        errors = errors | Board::STATUS_PAWNS_ON_BACKRANK
      end
      if pop_count(occupied_co[Board::WHITE]) > 16
        errors = errors | Board::STATUS_TOO_MANY_WHITE_PIECES
      end
      if pop_count(occupied_co[Board::BLACK]) > 16
        errors = errors | Board::STATUS_TOO_MANY_BLACK_PIECES
      end
      if castling_rights & Board::CASTLING_WHITE != 0
        if !king_squares[Board::WHITE] == E1
          errors = errors | Board::STATUS_BAD_CASTLING_RIGHTS
        end
        if castling_rights & Board::CASTLING_WHITE_QUEENSIDE != 0
          if !Board::BB_A1 & occupied_co[Board::WHITE] & rooks != 0
            errors = errors | Board::STATUS_BAD_CASTLING_RIGHTS
          end
        end
        if castling_rights & Board::CASTLING_WHITE_KINGSIDE != 0
          if !Board::BB_H1 & occupied_co[Board::WHITE] & rooks != 0
            errors = errors | Board::STATUS_BAD_CASTLING_RIGHTS
          end
        end
      end
      if castling_rights & Board::CASTLING_BLACK != 0
        if !king_squares[Board::BLACK] == Board::E8
          errors = errors | Board::STATUS_BAD_CASTLING_RIGHTS
        end
        if castling_rights & Board::CASTLING_BLACK_QUEENSIDE != 0
          if !Board::BB_A8 & occupied_co[Board::BLACK] & rooks != 0
            errors = errors | Board::STATUS_BAD_CASTLING_RIGHTS
          end
        end
        if castling_rights & Board::CASTLING_BLACK_KINGSIDE != 0
          if !Board::BB_H8 & occupied_co[Board::BLACK] & rooks != 0
            errors = errors | Board::STATUS_BAD_CASTLING_RIGHTS
          end
        end
      end
      if ep_square
        if turn == Board::WHITE
          ep_rank = 5
          pawn_mask = shift_down(Board::BB_SQUARES[ep_square])
        else
          ep_rank = 2
          pawn_mask = shift_up(Board::BB_SQUARES[ep_square])
        end
        if rank_index(ep_square) != ep_rank
          errors = errors | Board::STATUS_INVALID_EP_SQUARE
        end
        if !pawns & occupied_co[turn ^ 1] & pawn_mask != 0
          errors = errors | Board::STATUS_INVALID_EP_SQUARE
        end
      end
      if !errors & (Board::STATUS_NO_WHITE_KING | Board::STATUS_NO_BLACK_KING | Board::STATUS_TOO_MANY_KINGS) != 0
        if was_into_check?
          errors = errors | Board::STATUS_OPPOSITE_CHECK
        end
      end
      return errors
    end
def status errors = Board :: STATUS_VALID if ! occupied_co [ Board :: WHITE ] & kings != 0 errors = errors | Board :: STATUS_NO_WHITE_KING end if ! occupied_co [ Board :: BLACK ] & kings != 0 errors = erorrs | Board :: STATUS_NO_BLACK_KING end if pop_count ( occupied & kings ) > 2 errors = errors | Board :: STATUS_TOO_MANY_KINGS end if pop_count ( occupied_co [ Board :: WHITE ] & pawns ) > 8 errors = errors | Board :: STATUS_TOO_MANY_WHITE_PAWNS end if pop_count ( occupied_co [ Board :: BLACK ] & pawns ) > 8 errors = errors | Board :: STATUS_TOO_MANY_BLACK_PAWNS end if pawns & ( Board :: BB_RANK_1 | Board :: BB_RANK_8 ) != 0 errors = errors | Board :: STATUS_PAWNS_ON_BACKRANK end if pop_count ( occupied_co [ Board :: WHITE ] ) > 16 errors = errors | Board :: STATUS_TOO_MANY_WHITE_PIECES end if pop_count ( occupied_co [ Board :: BLACK ] ) > 16 errors = errors | Board :: STATUS_TOO_MANY_BLACK_PIECES end if castling_rights & Board :: CASTLING_WHITE != 0 if ! king_squares [ Board :: WHITE ] == E1 errors = errors | Board :: STATUS_BAD_CASTLING_RIGHTS end if castling_rights & Board :: CASTLING_WHITE_QUEENSIDE != 0 if ! Board :: BB_A1 & occupied_co [ Board :: WHITE ] & rooks != 0 errors = errors | Board :: STATUS_BAD_CASTLING_RIGHTS end end if castling_rights & Board :: CASTLING_WHITE_KINGSIDE != 0 if ! Board :: BB_H1 & occupied_co [ Board :: WHITE ] & rooks != 0 errors = errors | Board :: STATUS_BAD_CASTLING_RIGHTS end end end if castling_rights & Board :: CASTLING_BLACK != 0 if ! king_squares [ Board :: BLACK ] == Board :: E8 errors = errors | Board :: STATUS_BAD_CASTLING_RIGHTS end if castling_rights & Board :: CASTLING_BLACK_QUEENSIDE != 0 if ! Board :: BB_A8 & occupied_co [ Board :: BLACK ] & rooks != 0 errors = errors | Board :: STATUS_BAD_CASTLING_RIGHTS end end if castling_rights & Board :: CASTLING_BLACK_KINGSIDE != 0 if ! Board :: BB_H8 & occupied_co [ Board :: BLACK ] & rooks != 0 errors = errors | Board :: STATUS_BAD_CASTLING_RIGHTS end end end if ep_square if turn == Board :: WHITE ep_rank = 5 pawn_mask = shift_down ( Board :: BB_SQUARES [ ep_square ] ) else ep_rank = 2 pawn_mask = shift_up ( Board :: BB_SQUARES [ ep_square ] ) end if rank_index ( ep_square ) != ep_rank errors = errors | Board :: STATUS_INVALID_EP_SQUARE end if ! pawns & occupied_co [ turn ^ 1 ] & pawn_mask != 0 errors = errors | Board :: STATUS_INVALID_EP_SQUARE end end if ! errors & ( Board :: STATUS_NO_WHITE_KING | Board :: STATUS_NO_BLACK_KING | Board :: STATUS_TOO_MANY_KINGS ) != 0 if was_into_check? errors = errors | Board :: STATUS_OPPOSITE_CHECK end end return errors end
Gets a bitmask of possible problems with the position.
Gets a bitmask of possible problems with the position .
# The en-passant square must be on the third or sixth rank. # The last move must have been a double pawn push, so there must # be a pawn of the correct color on the fourth or fifth rank.