]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Introduce enum VALUE_ZERO instead of Value(0)
[stockfish] / src / position.cpp
index 9af2d9d8103a62b43bc19343e78e64f23e586260..058fab66b13940b2db943aef8548c46b63d1e936 100644 (file)
@@ -56,7 +56,7 @@ struct PieceLetters : std::map<char, Piece> {
       operator[]('B') = WB; operator[]('b') = BB;
       operator[]('N') = WN; operator[]('n') = BN;
       operator[]('P') = WP; operator[]('p') = BP;
-      operator[](' ') = EMPTY; operator[]('.') = EMPTY_BLACK_SQ;
+      operator[](' ') = PIECE_NONE; operator[]('.') = PIECE_NONE_DARK_SQ;
     }
 
     char from_piece(Piece p) const {
@@ -182,7 +182,7 @@ void Position::from_fen(const string& fen) {
   {
       if (isdigit(token))
       {
-          file += token - '0'; // Skip the given number of files
+          file += File(token - '0'); // Skip the given number of files
           continue;
       }
       else if (token == '/')
@@ -344,7 +344,7 @@ const string Position::to_fen() const {
   fen.erase(--fen.end());
   fen += (sideToMove == WHITE ? " w " : " b ");
 
-  if (st->castleRights != NO_CASTLES)
+  if (st->castleRights != CASTLES_NONE)
   {
       const bool Chess960 =   initialKFile  != FILE_E
                            || initialQRFile != FILE_A
@@ -400,8 +400,8 @@ void Position::print(Move move) const {
           char c = (color_of_piece_on(sq) == BLACK ? '=' : ' ');
           Piece piece = piece_on(sq);
 
-          if (piece == EMPTY && same_color_squares(sq, SQ_A1))
-              piece = EMPTY_BLACK_SQ;
+          if (piece == PIECE_NONE && square_color(sq) == DARK)
+              piece = PIECE_NONE_DARK_SQ;
 
           cout << c << pieceLetters.from_piece(piece) << c << '|';
       }
@@ -577,7 +577,7 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
       assert(to == ep_square());
       assert(piece_on(from) == piece_of_color_and_type(us, PAWN));
       assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN));
-      assert(piece_on(to) == EMPTY);
+      assert(piece_on(to) == PIECE_NONE);
 
       clear_bit(&b, from);
       clear_bit(&b, capsq);
@@ -825,7 +825,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
   do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
 
   board[to] = board[from];
-  board[from] = EMPTY;
+  board[from] = PIECE_NONE;
 
   // Update piece lists, note that index[from] is not updated and
   // becomes stale. This works as long as index[] is accessed just
@@ -897,7 +897,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
   st->value += pst_delta(piece, from, to);
 
   // Set capture piece
-  st->capture = capture;
+  st->capturedType = capture;
 
   // Update the key with the final value
   st->key = key;
@@ -954,10 +954,10 @@ void Position::do_capture_move(Key& key, PieceType capture, Color them, Square t
 
             assert(to == st->epSquare);
             assert(relative_rank(opposite_color(them), to) == RANK_6);
-            assert(piece_on(to) == EMPTY);
+            assert(piece_on(to) == PIECE_NONE);
             assert(piece_on(capsq) == piece_of_color_and_type(them, PAWN));
 
-            board[capsq] = EMPTY;
+            board[capsq] = PIECE_NONE;
         }
         st->pawnKey ^= zobrist[them][PAWN][capsq];
     }
@@ -1012,7 +1012,7 @@ void Position::do_castle_move(Move m) {
   Color them = opposite_color(us);
 
   // Reset capture field
-  st->capture = NO_PIECE_TYPE;
+  st->capturedType = PIECE_TYPE_NONE;
 
   // Find source squares for king and rook
   Square kfrom = move_from(m);
@@ -1051,7 +1051,7 @@ void Position::do_castle_move(Move m) {
   // Update board array
   Piece king = piece_of_color_and_type(us, KING);
   Piece rook = piece_of_color_and_type(us, ROOK);
-  board[kfrom] = board[rfrom] = EMPTY;
+  board[kfrom] = board[rfrom] = PIECE_NONE;
   board[kto] = king;
   board[rto] = rook;
 
@@ -1160,35 +1160,35 @@ void Position::undo_move(Move m) {
   do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
 
   board[from] = piece_of_color_and_type(us, pt);
-  board[to] = EMPTY;
+  board[to] = PIECE_NONE;
 
   // Update piece list
   index[from] = index[to];
   pieceList[us][pt][index[from]] = from;
 
-  if (st->capture)
+  if (st->capturedType)
   {
       Square capsq = to;
 
       if (ep)
           capsq = (us == WHITE)? (to - DELTA_N) : (to - DELTA_S);
 
-      assert(st->capture != KING);
+      assert(st->capturedType != KING);
       assert(!ep || square_is_empty(capsq));
 
       // Restore the captured piece
       set_bit(&(byColorBB[them]), capsq);
-      set_bit(&(byTypeBB[st->capture]), capsq);
+      set_bit(&(byTypeBB[st->capturedType]), capsq);
       set_bit(&(byTypeBB[0]), capsq);
 
-      board[capsq] = piece_of_color_and_type(them, st->capture);
+      board[capsq] = piece_of_color_and_type(them, st->capturedType);
 
       // Update piece count
-      pieceCount[them][st->capture]++;
+      pieceCount[them][st->capturedType]++;
 
       // Update piece list, add a new captured piece in capsq square
-      index[capsq] = pieceCount[them][st->capture] - 1;
-      pieceList[them][st->capture][index[capsq]] = capsq;
+      index[capsq] = pieceCount[them][st->capturedType] - 1;
+      pieceList[them][st->capturedType][index[capsq]] = capsq;
   }
 
   // Finally point our state pointer back to the previous state
@@ -1248,7 +1248,7 @@ void Position::undo_castle_move(Move m) {
   set_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares
 
   // Update board
-  board[rto] = board[kto] = EMPTY;
+  board[rto] = board[kto] = PIECE_NONE;
   board[rfrom] = piece_of_color_and_type(us, ROOK);
   board[kfrom] = piece_of_color_and_type(us, KING);
 
@@ -1392,7 +1392,7 @@ int Position::see(Square from, Square to) const {
   // Handle en passant moves
   if (st->epSquare == to && type_of_piece_on(from) == PAWN)
   {
-      assert(capture == EMPTY);
+      assert(capture == PIECE_NONE);
 
       Square capQq = (side_to_move() == WHITE)? (to - DELTA_N) : (to - DELTA_S);
       capture = piece_on(capQq);
@@ -1505,6 +1505,7 @@ void Position::clear() {
   st = &startState;
   memset(st, 0, sizeof(StateInfo));
   st->epSquare = SQ_NONE;
+  startPosPlyCounter = 0;
 
   memset(byColorBB,  0, sizeof(Bitboard) * 2);
   memset(byTypeBB,   0, sizeof(Bitboard) * 8);
@@ -1512,7 +1513,7 @@ void Position::clear() {
   memset(index,      0, sizeof(int) * 64);
 
   for (int i = 0; i < 64; i++)
-      board[i] = EMPTY;
+      board[i] = PIECE_NONE;
 
   for (int i = 0; i < 8; i++)
       for (int j = 0; j < 16; j++)
@@ -1539,6 +1540,10 @@ void Position::reset_game_ply() {
   st->gamePly = 0;
 }
 
+void Position::inc_startpos_ply_counter() {
+
+  startPosPlyCounter++;
+}
 
 /// Position::put_piece() puts a piece on the given square of the board,
 /// updating the board array, bitboards, and piece counts.
@@ -1681,7 +1686,7 @@ Score Position::compute_value() const {
 
 Value Position::compute_non_pawn_material(Color c) const {
 
-  Value result = Value(0);
+  Value result = VALUE_ZERO;
 
   for (PieceType pt = KNIGHT; pt <= QUEEN; pt++)
   {
@@ -1728,82 +1733,79 @@ bool Position::is_draw() const {
 bool Position::is_mate() const {
 
   MoveStack moves[256];
-  return is_check() && (generate_moves(*this, moves, false) == moves);
+  return is_check() && (generate_moves(*this, moves) == moves);
 }
 
 
-/// Position::has_mate_threat() tests whether a given color has a mate in one
-/// from the current position.
+/// Position::has_mate_threat() tests whether the side to move is under
+/// a threat of being mated in one from the current position.
 
-bool Position::has_mate_threat(Color c) {
+bool Position::has_mate_threat() {
 
+  MoveStack mlist[256], *last, *cur;
   StateInfo st1, st2;
-  Color stm = side_to_move();
+  bool mateFound = false;
 
+  // If we are under check it's up to evasions to do the job
   if (is_check())
       return false;
 
-  // If the input color is not equal to the side to move, do a null move
-  if (c != stm)
-      do_null_move(st1);
-
-  MoveStack mlist[120];
-  bool result = false;
-  Bitboard pinned = pinned_pieces(sideToMove);
+  // First pass the move to our opponent doing a null move
+  do_null_move(st1);
 
-  // Generate pseudo-legal non-capture and capture check moves
-  MoveStack* last = generate_non_capture_checks(*this, mlist);
+  // Then generate pseudo-legal moves that give check
+  last = generate_non_capture_checks(*this, mlist);
   last = generate_captures(*this, last);
 
-  // Loop through the moves, and see if one of them is mate
-  for (MoveStack* cur = mlist; cur != last; cur++)
+  // Loop through the moves, and see if one of them gives mate
+  Bitboard pinned = pinned_pieces(sideToMove);
+  CheckInfo ci(*this);
+  for (cur = mlist; cur != last && !mateFound; cur++)
   {
       Move move = cur->move;
-      if (!pl_move_is_legal(move, pinned))
+      if (   !pl_move_is_legal(move, pinned)
+          || !move_is_check(move, ci))
           continue;
 
-      do_move(move, st2);
+      do_move(move, st2, ci, true);
+
       if (is_mate())
-          result = true;
+          mateFound = true;
 
       undo_move(move);
   }
 
-  // Undo null move, if necessary
-  if (c != stm)
-      undo_null_move();
-
-  return result;
+  undo_null_move();
+  return mateFound;
 }
 
 
-/// Position::init_zobrist() is a static member function which initializes the
-/// various arrays used to compute hash keys.
+/// Position::init_zobrist() is a static member function which initializes at
+/// startup the various arrays used to compute hash keys.
 
 void Position::init_zobrist() {
 
-  for (int i = 0; i < 2; i++)
-      for (int j = 0; j < 8; j++)
-          for (int k = 0; k < 64; k++)
-              zobrist[i][j][k] = Key(genrand_int64());
+  int i,j, k;
 
-  for (int i = 0; i < 64; i++)
+  for (i = 0; i < 2; i++) for (j = 0; j < 8; j++) for (k = 0; k < 64; k++)
+      zobrist[i][j][k] = Key(genrand_int64());
+
+  for (i = 0; i < 64; i++)
       zobEp[i] = Key(genrand_int64());
 
-  for (int i = 0; i < 16; i++)
-      zobCastle[i] = genrand_int64();
+  for (i = 0; i < 16; i++)
+      zobCastle[i] = Key(genrand_int64());
 
-  zobSideToMove = genrand_int64();
-  zobExclusion = genrand_int64();
+  zobSideToMove = Key(genrand_int64());
+  zobExclusion  = Key(genrand_int64());
 }
 
 
 /// Position::init_piece_square_tables() initializes the piece square tables.
-/// This is a two-step operation:
-/// First, the white halves of the tables are
-/// copied from the MgPST[][] and EgPST[][] arrays.
-/// Second, the black halves of the tables are initialized by mirroring
-/// and changing the sign of the corresponding white scores.
+/// This is a two-step operation: First, the white halves of the tables are
+/// copied from the MgPST[][] and EgPST[][] arrays. Second, the black halves
+/// of the tables are initialized by mirroring and changing the sign of the
+/// corresponding white scores.
 
 void Position::init_piece_square_tables() {