]> git.sesse.net Git - stockfish/blobdiff - src/position.cpp
Space inflate position until do_promotion_move()
[stockfish] / src / position.cpp
index e1d05f614738c52c485606a94526ec2a2e8e3b76..0e6d0a6bc92293548917935ff46754bb5749e6aa 100644 (file)
@@ -1,13 +1,14 @@
 /*
-  Glaurung, a UCI chess playing engine.
-  Copyright (C) 2004-2008 Tord Romstad
+  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+  Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
+  Copyright (C) 2008 Marco Costalba
 
-  Glaurung is free software: you can redistribute it and/or modify
+  Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
-  Glaurung is distributed in the hope that it will be useful,
+  Stockfish is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
@@ -291,77 +292,70 @@ void Position::print() const {
 /// Position::copy() creates a copy of the input position.
 
 void Position::copy(const Position &pos) {
+
   memcpy(this, &pos, sizeof(Position));
 }
 
 
 /// Position:pinned_pieces() returns a bitboard of all pinned (against the
 /// king) pieces for the given color.
-
 Bitboard Position::pinned_pieces(Color c) const {
-  Bitboard b1, b2, pinned, pinners, sliders;
-  Square ksq = king_square(c), s;
-  Color them = opposite_color(c);
-
-  pinned = EmptyBoardBB;
-  b1 = occupied_squares();
-
-  sliders = rooks_and_queens(them) & ~checkers();
-  if(sliders & RookPseudoAttacks[ksq]) {
-    b2 = piece_attacks<ROOK>(ksq) & pieces_of_color(c);
-    pinners = rook_attacks_bb(ksq, b1 ^ b2) & sliders;
-    while(pinners) {
-      s = pop_1st_bit(&pinners);
-      pinned |= (squares_between(s, ksq) & b2);
-    }
-  }
-
-  sliders = bishops_and_queens(them) & ~checkers();
-  if(sliders & BishopPseudoAttacks[ksq]) {
-    b2 = piece_attacks<BISHOP>(ksq) & pieces_of_color(c);
-    pinners = bishop_attacks_bb(ksq, b1 ^ b2) & sliders;
-    while(pinners) {
-      s = pop_1st_bit(&pinners);
-      pinned |= (squares_between(s, ksq) & b2);
-    }
-  }
 
-  return pinned;
+  Square ksq = king_square(c);
+  return hidden_checks<ROOK, true>(c, ksq) | hidden_checks<BISHOP, true>(c, ksq);
 }
 
+
 /// Position:discovered_check_candidates() returns a bitboard containing all
 /// pieces for the given side which are candidates for giving a discovered
 /// check.  The code is almost the same as the function for finding pinned
 /// pieces.
 
 Bitboard Position::discovered_check_candidates(Color c) const {
-  Bitboard b1, b2, dc, checkers, sliders;
-  Square ksq = king_square(opposite_color(c)), s;
-
-  dc = EmptyBoardBB;
-  b1 = occupied_squares();
-
-  sliders = rooks_and_queens(c);
-  if(sliders & RookPseudoAttacks[ksq]) {
-    b2 = piece_attacks<ROOK>(ksq) & pieces_of_color(c);
-    checkers = rook_attacks_bb(ksq, b1 ^ b2) & sliders;
-    while(checkers) {
-      s = pop_1st_bit(&checkers);
-      dc |= (squares_between(s, ksq) & b2);
-    }
-  }
 
-  sliders = bishops_and_queens(c);
-  if(sliders & BishopPseudoAttacks[ksq]) {
-    b2 = piece_attacks<BISHOP>(ksq) & pieces_of_color(c);
-    checkers = bishop_attacks_bb(ksq, b1 ^ b2) & sliders;
-    while(checkers) {
-      s = pop_1st_bit(&checkers);
-      dc |= (squares_between(s, ksq) & b2);
-    }
-  }
+  Square ksq = king_square(opposite_color(c));
+  return hidden_checks<ROOK, false>(c, ksq) | hidden_checks<BISHOP, false>(c, ksq);
+}
+
 
-  return dc;
+/// Position:hidden_checks<>() returns a bitboard of all pinned (against the
+/// king) pieces for the given color and for the given pinner type. Or, when
+/// template parameter FindPinned is false, the pinned pieces of opposite color
+/// that are, indeed, the pieces candidate for a discovery check.
+template<PieceType Piece, bool FindPinned>
+Bitboard Position::hidden_checks(Color c, Square ksq) const {
+
+  Square s;
+  Bitboard sliders, result = EmptyBoardBB;
+  
+  if (Piece == ROOK) // Resolved at compile time
+      sliders = rooks_and_queens(FindPinned ? opposite_color(c) : c) & RookPseudoAttacks[ksq];
+  else
+      sliders = bishops_and_queens(FindPinned ? opposite_color(c) : c) & BishopPseudoAttacks[ksq];
+
+  if (sliders && (!FindPinned || (sliders & ~checkersBB)))
+  {
+       // King blockers are candidate pinned pieces
+      Bitboard candidate_pinned = piece_attacks<Piece>(ksq) & pieces_of_color(c);
+
+      // Pinners are sliders, not checkers, that give check when 
+      // candidate pinned are removed.
+      Bitboard pinners = (FindPinned ? sliders & ~checkersBB : sliders);
+
+      if (Piece == ROOK)
+          pinners &= rook_attacks_bb(ksq, occupied_squares() ^ candidate_pinned);
+      else
+          pinners &= bishop_attacks_bb(ksq, occupied_squares() ^ candidate_pinned);
+
+      // Finally for each pinner find the corresponding pinned piece (if same color of king)
+      // or discovery checker (if opposite color) among the candidates.
+      while (pinners)
+      {
+          s = pop_1st_bit(&pinners);
+          result |= (squares_between(s, ksq) & candidate_pinned);
+      }
+  }
+  return result;
 }
 
 
@@ -369,12 +363,12 @@ Bitboard Position::discovered_check_candidates(Color c) const {
 /// given square.
 
 bool Position::square_is_attacked(Square s, Color c) const {
-  return
-    (pawn_attacks(opposite_color(c), s) & pawns(c)) ||
-    (piece_attacks<KNIGHT>(s) & knights(c)) ||
-    (piece_attacks<KING>(s)   & kings(c)) ||
-    (piece_attacks<ROOK>(s)   & rooks_and_queens(c)) ||
-    (piece_attacks<BISHOP>(s) & bishops_and_queens(c));
+
+  return   (pawn_attacks(opposite_color(c), s) & pawns(c))
+        || (piece_attacks<KNIGHT>(s) & knights(c))
+        || (piece_attacks<KING>(s)   & kings(c))
+        || (piece_attacks<ROOK>(s)   & rooks_and_queens(c))
+        || (piece_attacks<BISHOP>(s) & bishops_and_queens(c));
 }
 
 
@@ -384,16 +378,17 @@ bool Position::square_is_attacked(Square s, Color c) const {
 /// attackers for one side.
 
 Bitboard Position::attacks_to(Square s) const {
-  return
-    (pawn_attacks(BLACK, s) & pawns(WHITE)) |
-    (pawn_attacks(WHITE, s) & pawns(BLACK)) |
-    (piece_attacks<KNIGHT>(s) & pieces_of_type(KNIGHT)) |
-    (piece_attacks<ROOK>(s) & rooks_and_queens()) |
-    (piece_attacks<BISHOP>(s) & bishops_and_queens()) |
-    (piece_attacks<KING>(s) & pieces_of_type(KING));
+
+  return  (pawn_attacks(BLACK, s)   & pawns(WHITE))
+        | (pawn_attacks(WHITE, s)   & pawns(BLACK))
+        | (piece_attacks<KNIGHT>(s) & pieces_of_type(KNIGHT))
+        | (piece_attacks<ROOK>(s)   & rooks_and_queens())
+        | (piece_attacks<BISHOP>(s) & bishops_and_queens())
+        | (piece_attacks<KING>(s)   & pieces_of_type(KING));
 }
 
 Bitboard Position::attacks_to(Square s, Color c) const {
+
   return attacks_to(s) & pieces_of_color(c);
 }
 
@@ -402,20 +397,49 @@ Bitboard Position::attacks_to(Square s, Color c) const {
 /// attacks square t.
 
 bool Position::piece_attacks_square(Square f, Square t) const {
+
   assert(square_is_ok(f));
   assert(square_is_ok(t));
 
-  switch(piece_on(f)) {
-  case WP: return white_pawn_attacks_square(f, t);
-  case BP: return black_pawn_attacks_square(f, t);
+  switch (piece_on(f))
+  {
+  case WP:          return pawn_attacks_square(WHITE, f, t);
+  case BP:          return pawn_attacks_square(BLACK, f, t);
   case WN: case BN: return piece_attacks_square<KNIGHT>(f, t);
   case WB: case BB: return piece_attacks_square<BISHOP>(f, t);
   case WR: case BR: return piece_attacks_square<ROOK>(f, t);
   case WQ: case BQ: return piece_attacks_square<QUEEN>(f, t);
   case WK: case BK: return piece_attacks_square<KING>(f, t);
-  default: return false;
+  default:          return false;
   }
+  return false;
+}
+
+
+/// Position::move_attacks_square() tests whether a move from the current
+/// position attacks a given square.  Only attacks by the moving piece are
+/// considered; the function does not handle X-ray attacks.
+
+bool Position::move_attacks_square(Move m, Square s) const {
+
+  assert(move_is_ok(m));
+  assert(square_is_ok(s));
+
+  Square f = move_from(m), t = move_to(m);
 
+  assert(square_is_occupied(f));
+
+  switch (piece_on(f))
+  {
+  case WP:          return pawn_attacks_square(WHITE, t, s);
+  case BP:          return pawn_attacks_square(BLACK, t, s);
+  case WN: case BN: return piece_attacks_square<KNIGHT>(t, s);
+  case WB: case BB: return piece_attacks_square<BISHOP>(t, s);
+  case WR: case BR: return piece_attacks_square<ROOK>(t, s);
+  case WQ: case BQ: return piece_attacks_square<QUEEN>(t, s);
+  case WK: case BK: return piece_attacks_square<KING>(t, s);
+  default: assert(false);
+  }
   return false;
 }
 
@@ -427,23 +451,24 @@ bool Position::piece_attacks_square(Square f, Square t) const {
 /// played, like in non-bitboard versions of Glaurung.
 
 void Position::find_checkers() {
-  checkersBB = attacks_to(king_square(side_to_move()),
-                          opposite_color(side_to_move()));
+
+  checkersBB = attacks_to(king_square(side_to_move()),opposite_color(side_to_move()));
 }
 
 
 /// Position::move_is_legal() tests whether a pseudo-legal move is legal.
 /// There are two versions of this function:  One which takes only a
 /// move as input, and one which takes a move and a bitboard of pinned
-/// pieces.  The latter function is faster, and should always be preferred
+/// pieces. The latter function is faster, and should always be preferred
 /// when a pinned piece bitboard has already been computed.
 
 bool Position::move_is_legal(Move m)  const {
+
   return move_is_legal(m, pinned_pieces(side_to_move()));
 }
 
-
 bool Position::move_is_legal(Move m, Bitboard pinned) const {
+
   Color us, them;
   Square ksq, from;
 
@@ -453,14 +478,15 @@ bool Position::move_is_legal(Move m, Bitboard pinned) const {
 
   // If we're in check, all pseudo-legal moves are legal, because our
   // check evasion generator only generates true legal moves.
-  if(is_check()) return true;
+  if (is_check())
+      return true;
 
   // Castling moves are checked for legality during move generation.
-  if(move_is_castle(m)) return true;
+  if (move_is_castle(m))
+      return true;
 
   us = side_to_move();
   them = opposite_color(us);
-
   from = move_from(m);
   ksq = king_square(us);
 
@@ -469,33 +495,36 @@ bool Position::move_is_legal(Move m, Bitboard pinned) const {
 
   // En passant captures are a tricky special case.  Because they are
   // rather uncommon, we do it simply by testing whether the king is attacked
-  // after the move is made:
-  if(move_is_ep(m)) {
-    Square to = move_to(m);
-    Square capsq = make_square(square_file(to), square_rank(from));
-    Bitboard b = occupied_squares();
-
-    assert(to == ep_square());
-    assert(piece_on(from) == pawn_of_color(us));
-    assert(piece_on(capsq) == pawn_of_color(them));
-    assert(piece_on(to) == EMPTY);
-
-    clear_bit(&b, from); clear_bit(&b, capsq); set_bit(&b, to);
-    return
-      (!(rook_attacks_bb(ksq, b) & rooks_and_queens(them)) &&
-       !(bishop_attacks_bb(ksq, b) & bishops_and_queens(them)));
+  // after the move is made
+  if (move_is_ep(m))
+  {
+      Square to = move_to(m);
+      Square capsq = make_square(square_file(to), square_rank(from));
+      Bitboard b = occupied_squares();
+
+      assert(to == ep_square());
+      assert(piece_on(from) == pawn_of_color(us));
+      assert(piece_on(capsq) == pawn_of_color(them));
+      assert(piece_on(to) == EMPTY);
+
+      clear_bit(&b, from);
+      clear_bit(&b, capsq);
+      set_bit(&b, to);
+
+      return   !(rook_attacks_bb(ksq, b) & rooks_and_queens(them))
+            && !(bishop_attacks_bb(ksq, b) & bishops_and_queens(them));
   }
 
   // If the moving piece is a king, check whether the destination
   // square is attacked by the opponent.
-  if(from == ksq) return !(square_is_attacked(move_to(m), them));
+  if (from == ksq)
+      return !(square_is_attacked(move_to(m), them));
 
   // A non-king move is legal if and only if it is not pinned or it
   // is moving along the ray towards or away from the king.
-  if(!bit_is_set(pinned, from)) return true;
-  if(direction_between_squares(from, ksq) ==
-     direction_between_squares(move_to(m), ksq))
-    return true;
+  if (   !bit_is_set(pinned, from)
+      || (direction_between_squares(from, ksq) == direction_between_squares(move_to(m), ksq)))
+      return true;
 
   return false;
 }
@@ -508,138 +537,126 @@ bool Position::move_is_legal(Move m, Bitboard pinned) const {
 /// when a discovered check candidates bitboard has already been computed.
 
 bool Position::move_is_check(Move m) const {
+
   Bitboard dc = discovered_check_candidates(side_to_move());
   return move_is_check(m, dc);
 }
 
-
 bool Position::move_is_check(Move m, Bitboard dcCandidates) const {
+
   Color us, them;
   Square ksq, from, to;
 
   assert(is_ok());
   assert(move_is_ok(m));
-  assert(dcCandidates ==
-         discovered_check_candidates(side_to_move()));
+  assert(dcCandidates == discovered_check_candidates(side_to_move()));
 
   us = side_to_move();
   them = opposite_color(us);
-
   from = move_from(m);
   to = move_to(m);
   ksq = king_square(them);
+
   assert(color_of_piece_on(from) == us);
   assert(piece_on(ksq) == king_of_color(them));
 
-  // Proceed according to the type of the moving piece:
-  switch(type_of_piece_on(from)) {
+  // Proceed according to the type of the moving piece
+  switch (type_of_piece_on(from))
+  {
   case PAWN:
-    // Normal check?
-    if(bit_is_set(pawn_attacks(them, ksq), to))
-      return true;
-    // Discovered check?
-    else if(bit_is_set(dcCandidates, from) &&
-            direction_between_squares(from, ksq) !=
-            direction_between_squares(to, ksq))
-      return true;
-    // Promotion with check?
-    else if(move_promotion(m)) {
-      Bitboard b = occupied_squares();
-      clear_bit(&b, from);
+      
+      if (bit_is_set(pawn_attacks(them, ksq), to)) // Normal check?
+          return true;
+      
+      if (    bit_is_set(dcCandidates, from)      // Discovered check?
+          && (direction_between_squares(from, ksq) != direction_between_squares(to, ksq)))
+          return true;
+      
+      if (move_promotion(m)) // Promotion with check?
+      {
+          Bitboard b = occupied_squares();
+          clear_bit(&b, from);
 
-      switch(move_promotion(m)) {
-      case KNIGHT:
-        return piece_attacks_square<KNIGHT>(to, ksq);
-      case BISHOP:
-        return bit_is_set(bishop_attacks_bb(to, b), ksq);
-      case ROOK:
-        return bit_is_set(rook_attacks_bb(to, b), ksq);
-      case QUEEN:
-        return bit_is_set(queen_attacks_bb(to, b), ksq);
-      default:
-        assert(false);
+          switch (move_promotion(m))
+          {
+          case KNIGHT:
+              return bit_is_set(piece_attacks<KNIGHT>(to), ksq);
+          case BISHOP:
+              return bit_is_set(bishop_attacks_bb(to, b), ksq);
+          case ROOK:
+              return bit_is_set(rook_attacks_bb(to, b), ksq);
+          case QUEEN:
+              return bit_is_set(queen_attacks_bb(to, b), ksq);
+          default:
+              assert(false);
+          }
       }
-    }
-    // En passant capture with check?  We have already handled the case
-    // of direct checks and ordinary discovered check, the only case we
-    // need to handle is the unusual case of a discovered check through the
-    // captured pawn.
-    else if(move_is_ep(m)) {
-      Square capsq = make_square(square_file(to), square_rank(from));
-      Bitboard b = occupied_squares();
-
-      clear_bit(&b, from); clear_bit(&b, capsq); set_bit(&b, to);
-      return
-        ((rook_attacks_bb(ksq, b) & rooks_and_queens(us)) ||
-         (bishop_attacks_bb(ksq, b) & bishops_and_queens(us)));
-    }
-    return false;
+      // En passant capture with check?  We have already handled the case
+      // of direct checks and ordinary discovered check, the only case we
+      // need to handle is the unusual case of a discovered check through the
+      // captured pawn.
+      else if (move_is_ep(m))
+      {
+          Square capsq = make_square(square_file(to), square_rank(from));
+          Bitboard b = occupied_squares();
+          clear_bit(&b, from);
+          clear_bit(&b, capsq);
+          set_bit(&b, to);
+          return  (rook_attacks_bb(ksq, b) & rooks_and_queens(us))
+                ||(bishop_attacks_bb(ksq, b) & bishops_and_queens(us));
+      }
+      return false;
 
-  case KNIGHT:
-    // Discovered check?
-    if(bit_is_set(dcCandidates, from))
-      return true;
-    // Normal check?
-    else
-      return bit_is_set(piece_attacks<KNIGHT>(ksq), to);
+  case KNIGHT:    
+    return   bit_is_set(dcCandidates, from)              // Discovered check?
+          || bit_is_set(piece_attacks<KNIGHT>(ksq), to); // Normal check?
 
   case BISHOP:
-    // Discovered check?
-    if(bit_is_set(dcCandidates, from))
-      return true;
-    // Normal check?
-    else
-      return bit_is_set(piece_attacks<BISHOP>(ksq), to);
+    return   bit_is_set(dcCandidates, from)              // Discovered check?
+          || bit_is_set(piece_attacks<BISHOP>(ksq), to); // Normal check?
 
   case ROOK:
-    // Discovered check?
-    if(bit_is_set(dcCandidates, from))
-      return true;
-    // Normal check?
-    else
-      return bit_is_set(piece_attacks<ROOK>(ksq), to);
+    return   bit_is_set(dcCandidates, from)              // Discovered check?
+          || bit_is_set(piece_attacks<ROOK>(ksq), to);   // Normal check?
 
   case QUEEN:
-    // Discovered checks are impossible!
-    assert(!bit_is_set(dcCandidates, from));
-    // Normal check?
-    return bit_is_set(piece_attacks<QUEEN>(ksq), to);
+      // Discovered checks are impossible!
+      assert(!bit_is_set(dcCandidates, from));    
+      return bit_is_set(piece_attacks<QUEEN>(ksq), to);  // Normal check?
 
   case KING:
-    // Discovered check?
-    if(bit_is_set(dcCandidates, from) &&
-       direction_between_squares(from, ksq) !=
-       direction_between_squares(to, ksq))
-      return true;
-    // Castling with check?
-    if(move_is_castle(m)) {
-      Square kfrom, kto, rfrom, rto;
-      Bitboard b = occupied_squares();
+      // Discovered check?
+      if (   bit_is_set(dcCandidates, from)
+          && (direction_between_squares(from, ksq) != direction_between_squares(to, ksq)))
+          return true;
 
-      kfrom = from;
-      rfrom = to;
-      if(rfrom > kfrom) {
-        kto = relative_square(us, SQ_G1);
-        rto = relative_square(us, SQ_F1);
-      }
-      else {
-        kto = relative_square(us, SQ_C1);
-        rto = relative_square(us, SQ_D1);
-      }
-
-      clear_bit(&b, kfrom); clear_bit(&b, rfrom);
-      set_bit(&b, rto); set_bit(&b, kto);
-
-      return bit_is_set(rook_attacks_bb(rto, b), ksq);
-    }
+      // Castling with check?
+      if (move_is_castle(m))
+      {
+          Square kfrom, kto, rfrom, rto;
+          Bitboard b = occupied_squares();
+          kfrom = from;
+          rfrom = to;
 
-    return false;
+          if (rfrom > kfrom)
+          {
+              kto = relative_square(us, SQ_G1);
+              rto = relative_square(us, SQ_F1);
+          } else {
+              kto = relative_square(us, SQ_C1);
+              rto = relative_square(us, SQ_D1);
+          }
+          clear_bit(&b, kfrom);
+          clear_bit(&b, rfrom);
+          set_bit(&b, rto);
+          set_bit(&b, kto);
+          return bit_is_set(rook_attacks_bb(rto, b), ksq);
+      }
+      return false;
 
   default:
-    assert(false);
-    return false;
+      assert(false);
   }
-
   assert(false);
   return false;
 }
@@ -649,48 +666,21 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const {
 /// position is a capture.
 
 bool Position::move_is_capture(Move m) const {
-  return
-    color_of_piece_on(move_to(m)) == opposite_color(side_to_move())
-    || move_is_ep(m);
-}
-
-
-/// Position::move_attacks_square() tests whether a move from the current
-/// position attacks a given square.  Only attacks by the moving piece are
-/// considered; the function does not handle X-ray attacks.
-
-bool Position::move_attacks_square(Move m, Square s) const {
-  assert(move_is_ok(m));
-  assert(square_is_ok(s));
-
-  Square f = move_from(m), t = move_to(m);
-
-  assert(square_is_occupied(f));
 
-  switch(piece_on(f)) {
-  case WP: return white_pawn_attacks_square(t, s);
-  case BP: return black_pawn_attacks_square(t, s);
-  case WN: case BN: return piece_attacks_square<KNIGHT>(t, s);
-  case WB: case BB: return piece_attacks_square<BISHOP>(t, s);
-  case WR: case BR: return piece_attacks_square<ROOK>(t, s);
-  case WQ: case BQ: return piece_attacks_square<QUEEN>(t, s);
-  case WK: case BK: return piece_attacks_square<KING>(t, s);
-  default: assert(false);
-  }
-
-  return false;
+  return   color_of_piece_on(move_to(m)) == opposite_color(side_to_move())
+        || move_is_ep(m);
 }
 
 
-
-/// Position::backup() is called when making a move.  All information
+/// Position::backup() is called when making a move. All information
 /// necessary to restore the position when the move is later unmade
-/// is saved to an UndoInfo object.  The function Position::restore
+/// is saved to an UndoInfo object. The function Position::restore
 /// does the reverse operation:  When one does a backup followed by
 /// a restore with the same UndoInfo object, the position is restored
 /// to the state before backup was called.
 
-void Position::backup(UndoInfo &u) const {
+void Position::backup(UndoInfo& u) const {
+
   u.castleRights = castleRights;
   u.epSquare = epSquare;
   u.checkersBB = checkersBB;
@@ -708,7 +698,8 @@ void Position::backup(UndoInfo &u) const {
 /// Position::restore() is called when unmaking a move.  It copies back
 /// the information backed up during a previous call to Position::backup.
 
-void Position::restore(const UndoInfo &u) {
+void Position::restore(const UndoInfo& u) {
+
   castleRights = u.castleRights;
   epSquare = u.epSquare;
   checkersBB = u.checkersBB;
@@ -717,6 +708,7 @@ void Position::restore(const UndoInfo &u) {
   materialKey = u.materialKey;
   rule50 = u.rule50;
   lastMove = u.lastMove;
+  // u.capture is restored in undo_move()
   mgValue = u.mgValue;
   egValue = u.egValue;
 }
@@ -731,91 +723,88 @@ void Position::restore(const UndoInfo &u) {
 /// the discovered check candidates makes it easier to update the checkersBB
 /// member variable in the position object.
 
-void Position::do_move(Move m, UndoInfo &u) {
+void Position::do_move(Move m, UndoInfo& u) {
+
   do_move(m, u, discovered_check_candidates(side_to_move()));
 }
 
-void Position::do_move(Move m, UndoInfo &u, Bitboard dcCandidates) {
+void Position::do_move(Move m, UndoInfo& u, Bitboard dcCandidates) {
+
   assert(is_ok());
   assert(move_is_ok(m));
 
   // Back up the necessary information to our UndoInfo object (except the
-  // captured piece, which is taken care of later:
+  // captured piece, which is taken care of later.
   backup(u);
 
   // Save the current key to the history[] array, in order to be able to
-  // detect repetition draws:
+  // detect repetition draws.
   history[gamePly] = key;
 
-  // Increment the 50 moves rule draw counter.  Resetting it to zero in the
+  // Increment the 50 moves rule draw counter. Resetting it to zero in the
   // case of non-reversible moves is taken care of later.
   rule50++;
 
-  if(move_is_castle(m))
-    do_castle_move(m);
-  else if(move_promotion(m))
-    do_promotion_move(m, u);
-  else if(move_is_ep(m))
-    do_ep_move(m);
-  else {
-    Color us, them;
-    Square from, to;
-    PieceType piece, capture;
-
-    us = side_to_move();
-    them = opposite_color(us);
-
-    from = move_from(m);
-    to = move_to(m);
+  if (move_is_castle(m))
+      do_castle_move(m);
+  else if (move_promotion(m))
+      do_promotion_move(m, u);
+  else if (move_is_ep(m))
+      do_ep_move(m);
+  else
+  {
+    Color us = side_to_move();
+    Color them = opposite_color(us);
+    Square from = move_from(m);
+    Square to = move_to(m);
 
     assert(color_of_piece_on(from) == us);
     assert(color_of_piece_on(to) == them || piece_on(to) == EMPTY);
 
-    piece = type_of_piece_on(from);
-    capture = type_of_piece_on(to);
+    PieceType piece = type_of_piece_on(from);
+    PieceType capture = type_of_piece_on(to);
 
-    if(capture) {
-      assert(capture != KING);
+    if (capture)
+    {
+        assert(capture != KING);
 
-      // Remove captured piece:
-      clear_bit(&(byColorBB[them]), to);
-      clear_bit(&(byTypeBB[capture]), to);
+        // Remove captured piece
+        clear_bit(&(byColorBB[them]), to);
+        clear_bit(&(byTypeBB[capture]), to);
 
-      // Update hash key:
-      key ^= zobrist[them][capture][to];
+        // Update hash key
+        key ^= zobrist[them][capture][to];
 
-      // If the captured piece was a pawn, update pawn hash key:
-      if(capture == PAWN)
-        pawnKey ^= zobrist[them][PAWN][to];
+        // If the captured piece was a pawn, update pawn hash key
+        if (capture == PAWN)
+            pawnKey ^= zobrist[them][PAWN][to];
 
-      // Update incremental scores:
-      mgValue -= mg_pst(them, capture, to);
-      egValue -= eg_pst(them, capture, to);
+        // Update incremental scores
+        mgValue -= mg_pst(them, capture, to);
+        egValue -= eg_pst(them, capture, to);
 
-      // Update material:
-      if(capture != PAWN)
-        npMaterial[them] -= piece_value_midgame(capture);
+        // Update material
+        if (capture != PAWN)
+            npMaterial[them] -= piece_value_midgame(capture);
 
-      // Update material hash key:
-      materialKey ^= zobMaterial[them][capture][pieceCount[them][capture]];
+        // Update material hash key
+        materialKey ^= zobMaterial[them][capture][pieceCount[them][capture]];
 
-      // Update piece count:
-      pieceCount[them][capture]--;
+        // Update piece count
+        pieceCount[them][capture]--;
 
-      // Update piece list:
-      pieceList[them][capture][index[to]] =
-        pieceList[them][capture][pieceCount[them][capture]];
-      index[pieceList[them][capture][index[to]]] = index[to];
+        // Update piece list
+        pieceList[them][capture][index[to]] = pieceList[them][capture][pieceCount[them][capture]];
+        index[pieceList[them][capture][index[to]]] = index[to];
 
-      // Remember the captured piece, in order to be able to undo the move
-      // correctly:
-      u.capture = capture;
+        // Remember the captured piece, in order to be able to undo the move correctly
+        u.capture = capture;
 
-      // Reset rule 50 counter:
-      rule50 = 0;
+        // Reset rule 50 counter
+        rule50 = 0;
     }
 
-    // Move the piece:
+    // Move the piece
     clear_bit(&(byColorBB[us]), from);
     clear_bit(&(byTypeBB[piece]), from);
     clear_bit(&(byTypeBB[0]), from); // HACK: byTypeBB[0] == occupied squares
@@ -825,103 +814,105 @@ void Position::do_move(Move m, UndoInfo &u, Bitboard dcCandidates) {
     board[to] = board[from];
     board[from] = EMPTY;
 
-    // Update hash key:
+    // Update hash key
     key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
 
-    // Update incremental scores:
+    // Update incremental scores
     mgValue -= mg_pst(us, piece, from);
     mgValue += mg_pst(us, piece, to);
     egValue -= eg_pst(us, piece, from);
     egValue += eg_pst(us, piece, to);
 
-    // If the moving piece was a king, update the king square:
-    if(piece == KING)
-      kingSquare[us] = to;
+    // If the moving piece was a king, update the king square
+    if (piece == KING)
+        kingSquare[us] = to;
 
     // If the move was a double pawn push, set the en passant square.
     // This code is a bit ugly right now, and should be cleaned up later.
     // FIXME
-    if(epSquare != SQ_NONE) {
-      key ^= zobEp[epSquare];
-      epSquare = SQ_NONE;
+    if (epSquare != SQ_NONE)
+    {
+        key ^= zobEp[epSquare];
+        epSquare = SQ_NONE;
     }
-    if(piece == PAWN) {
-      if(abs(int(to) - int(from)) == 16) {
-        if((us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) &
-                            pawns(BLACK))) ||
-           (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) &
-                            pawns(WHITE)))) {
-          epSquare = Square((int(from) + int(to)) / 2);
-          key ^= zobEp[epSquare];
+    if (piece == PAWN)
+    {
+        if (abs(int(to) - int(from)) == 16)
+        {
+            if(   (   us == WHITE
+                   && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK)))
+               || (   us == BLACK
+                   && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE))))
+            {
+                epSquare = Square((int(from) + int(to)) / 2);
+                key ^= zobEp[epSquare];
+            }
         }
-      }
-      // Reset rule 50 draw counter.
-      rule50 = 0;
-      // Update pawn hash key:
-      pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
-    }
+        // Reset rule 50 draw counter
+        rule50 = 0;
 
-    // Update piece lists:
+        // Update pawn hash key
+        pawnKey ^= zobrist[us][PAWN][from] ^ zobrist[us][PAWN][to];
+    }
+    // Update piece lists
     pieceList[us][piece][index[from]] = to;
     index[to] = index[from];
 
-    // Update castle rights:
+    // Update castle rights
     key ^= zobCastle[castleRights];
     castleRights &= castleRightsMask[from];
     castleRights &= castleRightsMask[to];
     key ^= zobCastle[castleRights];
 
-    // Update checkers bitboard:
+    // Update checkers bitboard
     checkersBB = EmptyBoardBB;
     Square ksq = king_square(them);
-
-    switch(piece) {
-
+    switch (piece)
+    {
     case PAWN:
-      if(bit_is_set(pawn_attacks(them, ksq), to))
-        set_bit(&checkersBB, to);
-      if(bit_is_set(dcCandidates, from))
-        checkersBB |=
-          ((piece_attacks<ROOK>(ksq) & rooks_and_queens(us)) |
-           (piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
-      break;
+        if (bit_is_set(pawn_attacks(them, ksq), to))
+            set_bit(&checkersBB, to);
+
+        if (bit_is_set(dcCandidates, from))
+            checkersBB |= ( (piece_attacks<ROOK>(ksq) & rooks_and_queens(us))
+                           |(piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
+        break;
 
     case KNIGHT:
-      if(bit_is_set(piece_attacks<KNIGHT>(ksq), to))
-        set_bit(&checkersBB, to);
-      if(bit_is_set(dcCandidates, from))
-        checkersBB |=
-          ((piece_attacks<ROOK>(ksq) & rooks_and_queens(us)) |
-           (piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
-      break;
+        if (bit_is_set(piece_attacks<KNIGHT>(ksq), to))
+            set_bit(&checkersBB, to);
+
+        if (bit_is_set(dcCandidates, from))
+            checkersBB |= ( (piece_attacks<ROOK>(ksq) & rooks_and_queens(us))
+                           |(piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
+        break;
 
     case BISHOP:
-      if(bit_is_set(piece_attacks<BISHOP>(ksq), to))
-        set_bit(&checkersBB, to);
-      if(bit_is_set(dcCandidates, from))
-        checkersBB |=
-          (piece_attacks<ROOK>(ksq) & rooks_and_queens(us));
-      break;
+        if  (bit_is_set(piece_attacks<BISHOP>(ksq), to))
+            set_bit(&checkersBB, to);
+
+        if (bit_is_set(dcCandidates, from))
+            checkersBB |= (piece_attacks<ROOK>(ksq) & rooks_and_queens(us));
+        break;
 
     case ROOK:
-      if(bit_is_set(piece_attacks<ROOK>(ksq), to))
-        set_bit(&checkersBB, to);
-      if(bit_is_set(dcCandidates, from))
-        checkersBB |=
-          (piece_attacks<BISHOP>(ksq) & bishops_and_queens(us));
-      break;
+        if (bit_is_set(piece_attacks<ROOK>(ksq), to))
+            set_bit(&checkersBB, to);
+
+        if (bit_is_set(dcCandidates, from))
+            checkersBB |= (piece_attacks<BISHOP>(ksq) & bishops_and_queens(us));
+        break;
 
     case QUEEN:
-      if(bit_is_set(piece_attacks<QUEEN>(ksq), to))
-        set_bit(&checkersBB, to);
-      break;
+        if (bit_is_set(piece_attacks<QUEEN>(ksq), to))
+            set_bit(&checkersBB, to);
+        break;
 
     case KING:
-      if(bit_is_set(dcCandidates, from))
-        checkersBB |=
-          ((piece_attacks<ROOK>(ksq) & rooks_and_queens(us)) |
-           (piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
-      break;
+        if (bit_is_set(dcCandidates, from))
+            checkersBB |= ( (piece_attacks<ROOK>(ksq) & rooks_and_queens(us))
+                           |(piece_attacks<BISHOP>(ksq) & bishops_and_queens(us)));
+        break;
 
     default:
       assert(false);
@@ -947,34 +938,33 @@ void Position::do_move(Move m, UndoInfo &u, Bitboard dcCandidates) {
 /// instance white short castling in a non-Chess960 game is encoded as e1h1.
 
 void Position::do_castle_move(Move m) {
-  Color us, them;
-  Square kfrom, kto, rfrom, rto;
 
   assert(is_ok());
   assert(move_is_ok(m));
   assert(move_is_castle(m));
 
-  us = side_to_move();
-  them = opposite_color(us);
+  Color us = side_to_move();
+  Color them = opposite_color(us);
 
-  // Find source squares for king and rook:
-  kfrom = move_from(m);
-  rfrom = move_to(m);  // HACK: See comment at beginning of function.
+  // Find source squares for king and rook
+  Square kfrom = move_from(m);
+  Square rfrom = move_to(m);  // HACK: See comment at beginning of function
+  Square kto, rto;
 
   assert(piece_on(kfrom) == king_of_color(us));
   assert(piece_on(rfrom) == rook_of_color(us));
 
-  // Find destination squares for king and rook:
-  if(rfrom > kfrom) { // O-O
-    kto = relative_square(us, SQ_G1);
-    rto = relative_square(us, SQ_F1);
-  }
-  else { // O-O-O
-    kto = relative_square(us, SQ_C1);
-    rto = relative_square(us, SQ_D1);
+  // Find destination squares for king and rook
+  if (rfrom > kfrom) // O-O
+  {
+      kto = relative_square(us, SQ_G1);
+      rto = relative_square(us, SQ_F1);
+  else { // O-O-O
+      kto = relative_square(us, SQ_C1);
+      rto = relative_square(us, SQ_D1);
   }
 
-  // Remove pieces from source squares:
+  // Remove pieces from source squares
   clear_bit(&(byColorBB[us]), kfrom);
   clear_bit(&(byTypeBB[KING]), kfrom);
   clear_bit(&(byTypeBB[0]), kfrom); // HACK: byTypeBB[0] == occupied squares
@@ -982,7 +972,7 @@ void Position::do_castle_move(Move m) {
   clear_bit(&(byTypeBB[ROOK]), rfrom);
   clear_bit(&(byTypeBB[0]), rfrom); // HACK: byTypeBB[0] == occupied squares
 
-  // Put pieces on destination squares:
+  // Put pieces on destination squares
   set_bit(&(byColorBB[us]), kto);
   set_bit(&(byTypeBB[KING]), kto);
   set_bit(&(byTypeBB[0]), kto); // HACK: byTypeBB[0] == occupied squares
@@ -990,22 +980,22 @@ void Position::do_castle_move(Move m) {
   set_bit(&(byTypeBB[ROOK]), rto);
   set_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
 
-  // Update board array:
+  // Update board array
   board[kfrom] = board[rfrom] = EMPTY;
   board[kto] = king_of_color(us);
   board[rto] = rook_of_color(us);
 
-  // Update king square:
+  // Update king square
   kingSquare[us] = kto;
 
-  // Update piece lists:
+  // Update piece lists
   pieceList[us][KING][index[kfrom]] = kto;
   pieceList[us][ROOK][index[rfrom]] = rto;
   int tmp = index[rfrom];
   index[kto] = index[kfrom];
   index[rto] = tmp;
 
-  // Update incremental scores:
+  // Update incremental scores
   mgValue -= mg_pst(us, KING, kfrom);
   mgValue += mg_pst(us, KING, kto);
   egValue -= eg_pst(us, KING, kfrom);
@@ -1015,25 +1005,26 @@ void Position::do_castle_move(Move m) {
   egValue -= eg_pst(us, ROOK, rfrom);
   egValue += eg_pst(us, ROOK, rto);
 
-  // Update hash key:
+  // Update hash key
   key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
   key ^= zobrist[us][ROOK][rfrom] ^ zobrist[us][ROOK][rto];
 
-  // Clear en passant square:
-  if(epSquare != SQ_NONE) {
-    key ^= zobEp[epSquare];
-    epSquare = SQ_NONE;
+  // Clear en passant square
+  if(epSquare != SQ_NONE)
+  {
+      key ^= zobEp[epSquare];
+      epSquare = SQ_NONE;
   }
 
-  // Update castling rights:
+  // Update castling rights
   key ^= zobCastle[castleRights];
   castleRights &= castleRightsMask[kfrom];
   key ^= zobCastle[castleRights];
 
-  // Reset rule 50 counter:
+  // Reset rule 50 counter
   rule50 = 0;
 
-  // Update checkers BB:
+  // Update checkers BB
   checkersBB = attacks_to(king_square(them), us);
 }