]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Use self-describing constants instead of numbers
[stockfish] / src / position.cpp
index e8689ea727316d8f474abcd679999f2326826de6..28da877525a4773a87b1f25c31bd8ccca1da068d 100644 (file)
@@ -40,16 +40,16 @@ static const string PieceToChar(" PNBRQK  pnbrqk");
 
 CACHE_LINE_ALIGNMENT
 
-Score pieceSquareTable[16][64]; // [piece][square]
-Value PieceValue[2][18] = {     // [Mg / Eg][piece / pieceType]
+Score pieceSquareTable[PIECE_NB][SQUARE_NB];
+Value PieceValue[2][18] = { // [Mg / Eg][piece / pieceType]
 { VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg },
 { VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg } };
 
 namespace Zobrist {
 
-Key psq[2][8][64]; // [color][pieceType][square / piece count]
-Key enpassant[8];  // [file]
-Key castle[16];    // [castleRight]
+Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
+Key enpassant[FILE_NB];
+Key castle[CASTLE_RIGHT_NB];
 Key side;
 Key exclusion;
 
@@ -102,29 +102,30 @@ void init() {
 } // namespace Zobrist
 
 
+namespace {
+
 /// next_attacker() is an helper function used by see() to locate the least
 /// valuable attacker for the side to move, remove the attacker we just found
 /// from the 'occupied' bitboard and scan for new X-ray attacks behind it.
 
-template<PieceType Pt> static FORCE_INLINE
+template<int Pt> FORCE_INLINE
 PieceType next_attacker(const Bitboard* bb, const Square& to, const Bitboard& stmAttackers,
                         Bitboard& occupied, Bitboard& attackers) {
 
-  const PieceType NextPt = PieceType((int)Pt + 1);
-
-  if (!(stmAttackers & bb[Pt]))
-      return next_attacker<NextPt>(bb, to, stmAttackers, occupied, attackers);
-
-  Bitboard b = stmAttackers & bb[Pt];
-  occupied ^= b & ~(b - 1);
+  if (stmAttackers & bb[Pt])
+  {
+      Bitboard b = stmAttackers & bb[Pt];
+      occupied ^= b & ~(b - 1);
 
-  if (Pt == PAWN || Pt == BISHOP || Pt == QUEEN)
-      attackers |= attacks_bb<BISHOP>(to, occupied) & (bb[BISHOP] | bb[QUEEN]);
+      if (Pt == PAWN || Pt == BISHOP || Pt == QUEEN)
+          attackers |= attacks_bb<BISHOP>(to, occupied) & (bb[BISHOP] | bb[QUEEN]);
 
-  if (Pt == ROOK || Pt == QUEEN)
-      attackers |= attacks_bb<ROOK>(to, occupied) & (bb[ROOK] | bb[QUEEN]);
+      if (Pt == ROOK || Pt == QUEEN)
+          attackers |= attacks_bb<ROOK>(to, occupied) & (bb[ROOK] | bb[QUEEN]);
 
-  return Pt;
+      return (PieceType)Pt;
+  }
+  return next_attacker<Pt+1>(bb, to, stmAttackers, occupied, attackers);
 }
 
 template<> FORCE_INLINE
@@ -132,6 +133,8 @@ PieceType next_attacker<KING>(const Bitboard*, const Square&, const Bitboard&, B
   return KING; // No need to update bitboards, it is the last cycle
 }
 
+} // namespace
+
 
 /// CheckInfo c'tor
 
@@ -1312,11 +1315,12 @@ int Position::see(Move m) const {
       stm = ~stm;
       stmAttackers = attackers & pieces(stm);
 
-      // Stop before processing a king capture
-      if (captured == KING && stmAttackers)
+      if (captured == KING)
       {
-          assert(slIndex < 32);
-          swapList[slIndex++] = QueenValueMg * 16;
+          // Stop before processing a king capture
+          if (stmAttackers)
+              swapList[slIndex++] = QueenValueMg * 16;
+
           break;
       }
 
@@ -1589,7 +1593,7 @@ bool Position::pos_is_ok(int* failedStep) const {
 
   if ((*step)++, debugKingCount)
   {
-      int kingCount[2] = {};
+      int kingCount[COLOR_NB] = {};
 
       for (Square s = SQ_A1; s <= SQ_H8; s++)
           if (type_of(piece_on(s)) == KING)