Count material after consecutive passing moves
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 11 Apr 2020 13:13:54 +0000 (15:13 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sat, 11 Apr 2020 13:13:54 +0000 (15:13 +0200)
Closes #99.

src/position.cpp
src/position.h
src/types.h
tests/perft.sh

index 5582b82..c21ed7d 100644 (file)
@@ -874,10 +874,10 @@ bool Position::legal(Move m) const {
       return false;
 
   // Illegal king passing move
-  if (king_pass_on_stalemate() && type_of(m) == SPECIAL && from == to && !checkers())
+  if (king_pass_on_stalemate() && is_pass(m) && !checkers())
   {
       for (const auto& move : MoveList<NON_EVASIONS>(*this))
-          if (!(type_of(move) == SPECIAL && from == to) && legal(move))
+          if (!is_pass(move) && legal(move))
               return false;
   }
 
@@ -926,7 +926,7 @@ bool Position::legal(Move m) const {
 
   // Flying general rule and bikjang
   // In case of bikjang passing is always allowed, even when in check
-  if (st->bikjang && type_of(m) == SPECIAL && from == to)
+  if (st->bikjang && is_pass(m))
       return true;
   if ((var->flyingGeneral && count<KING>(us)) || st->bikjang)
   {
@@ -1194,11 +1194,12 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
   Piece captured = type_of(m) == ENPASSANT ? make_piece(them, PAWN) : piece_on(to);
   if (to == from)
   {
-      assert((type_of(m) == PROMOTION && sittuyin_promotion()) || (type_of(m) == SPECIAL && king_pass()));
+      assert((type_of(m) == PROMOTION && sittuyin_promotion()) || (is_pass(m) && king_pass()));
       captured = NO_PIECE;
   }
   st->capturedpromoted = is_promoted(to);
   st->unpromotedCapturedPiece = captured ? unpromoted_piece_on(to) : NO_PIECE;
+  st->pass = is_pass(m);
 
   assert(color_of(pc) == us);
   assert(captured == NO_PIECE || color_of(captured) == (type_of(m) != CASTLING ? them : us));
@@ -1478,7 +1479,7 @@ void Position::undo_move(Move m) {
 
   assert(type_of(m) == DROP || empty(from) || type_of(m) == CASTLING || is_gating(m)
          || (type_of(m) == PROMOTION && sittuyin_promotion())
-         || (type_of(m) == SPECIAL && king_pass()));
+         || (is_pass(m) && king_pass()));
   assert(type_of(st->capturedPiece) != KING);
 
   // Remove gated piece
@@ -1940,7 +1941,8 @@ bool Position::is_immediate_game_end(Value& result, int ply) const {
       }
   }
   // Check for bikjang rule (Janggi)
-  if (var->bikjangRule && st->pliesFromNull > 0 && st->bikjang && st->previous->bikjang)
+  if (var->bikjangRule && st->pliesFromNull > 0 && (   (st->bikjang && st->previous->bikjang)
+                                                    || (st->pass && st->previous->pass)))
   {
       // material counting
       auto weigth_count = [this](PieceType pt, int v){ return v * (count(WHITE, pt) - count(BLACK, pt)); };
index 05ecc57..8b3824d 100644 (file)
@@ -63,6 +63,7 @@ struct StateInfo {
   bool       capturedpromoted;
   bool       shak;
   bool       bikjang;
+  bool       pass;
   int        repetition;
 };
 
index c643ca6..ffc79b9 100644 (file)
@@ -699,6 +699,10 @@ inline bool is_gating(Move m) {
   return gating_type(m) && (type_of(m) == NORMAL || type_of(m) == CASTLING);
 }
 
+inline bool is_pass(Move m) {
+  return type_of(m) == SPECIAL || from_sq(m) == to_sq(m);
+}
+
 constexpr Move make_move(Square from, Square to) {
   return Move((from << SQUARE_BITS) + to);
 }
index 6e292f3..8d88a90 100755 (executable)
@@ -75,9 +75,9 @@ if [[ $1 == "largeboard" ]]; then
   expect perft.exp grand startpos 3 259514 > /dev/null
   expect perft.exp xiangqi startpos 4 3290240 > /dev/null
   expect perft.exp xiangqi "fen 1rbaka2R/5r3/6n2/2p1p1p2/4P1bP1/PpC3Bc1/1nPR2P2/2N2AN2/1c2K1p2/2BAC4 w - - 0 1" 4 4485547 > /dev/null
-  expect perft.exp janggi startpos 4 1067293 > /dev/null
-  expect perft.exp janggi "fen 1n1kaabn1/cr2N4/5C1c1/p1pNp3p/9/9/P1PbP1P1P/3r1p3/4A4/R1BA1KB1R b - - 0 1" 4 76824 > /dev/null
-  expect perft.exp janggi "fen 1Pbcka3/3nNn1c1/N2CaC3/1pB6/9/9/5P3/9/4K4/9 w - - 0 23" 4 151944 > /dev/null
+  expect perft.exp janggi startpos 4 1065277 > /dev/null
+  expect perft.exp janggi "fen 1n1kaabn1/cr2N4/5C1c1/p1pNp3p/9/9/P1PbP1P1P/3r1p3/4A4/R1BA1KB1R b - - 0 1" 4 76763 > /dev/null
+  expect perft.exp janggi "fen 1Pbcka3/3nNn1c1/N2CaC3/1pB6/9/9/5P3/9/4K4/9 w - - 0 23" 4 151202 > /dev/null
 fi
 
 rm perft.exp