diff --git a/src/scout.cpp b/src/scout.cpp index 2a55120..3fc3ddf 100644 --- a/src/scout.cpp +++ b/src/scout.cpp @@ -82,6 +82,7 @@ void search(Thread* th) { size_t maxMatches = d.limit ? d.skip + d.limit : 0; d.matches.reserve(maxMatches ? maxMatches : 100000); matchPlies.reserve(128); + Piece movedPiece = NO_PIECE; // Lambda helper to copy hot stuff to local variables auto set_condition = [&] (size_t idx) { @@ -136,14 +137,15 @@ void search(Thread* th) { st = states; Position pos = th->rootPos; + Move move = *data; // Could be MOVE_NONE data++; // First move of the game // Loop across the game (that could be empty) do { - Move move = *data; // Could be MOVE_NONE - assert(!move || (pos.pseudo_legal(move) && pos.legal(move))); + Move nextMove = *data; + // If we are looking for a streak, fail and reset as soon as last // matched ply is more than one half-move behind. We take care to // verify the last matched ply comes form the same streak. @@ -174,7 +176,7 @@ void search(Thread* th) { goto SkipToNextGame; // Shortcut: result will not change case RuleResultType: - if ( !move // End of game + if ( !nextMove // End of game && !MoveList(pos).size() && !!pos.checkers() == (cond->resultType == ResultMate)) goto NextRule; @@ -219,7 +221,7 @@ void search(Thread* th) { if (cond->moveSquares & to_sq(move)) for (const ScoutMove& m : cond->moves) { - if ( pos.moved_piece(move) != m.pc + if ( movedPiece != m.pc || to_sq(move) != m.to || m.castle != (type_of(move) == CASTLING)) continue; @@ -228,21 +230,21 @@ void search(Thread* th) { break; case RuleQuietMove: - if (move && !pos.capture(move)) + if (move && !pos.captured_piece()) goto NextRule; break; case RuleCapturedPiece: if (move && pos.capture(move)) { - PieceType pt = type_of(move) == NORMAL ? type_of(pos.piece_on(to_sq(move))) : PAWN; + PieceType pt = type_of(move) == NORMAL ? type_of(pos.captured_piece()) : PAWN; if (cond->capturedFlags & (1 << int(pt))) goto NextRule; } break; case RuleMovedPiece: - if (move && (cond->movedFlags & (1 << int(type_of(pos.moved_piece(move)))))) + if (move && (cond->movedFlags & (1 << int(type_of(movedPiece))))) goto NextRule; break; @@ -282,7 +284,9 @@ void search(Thread* th) { } // Do the move after rule checking + move = *data; if (move) + movedPiece = pos.moved_piece(move); pos.do_move(move, *st++, pos.gives_check(move)); } while (*data++ != MOVE_NONE); // Exit the game loop pointing to next ofs diff --git a/src/test.py b/src/test.py index ec89f0d..b7087f3 100755 --- a/src/test.py +++ b/src/test.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -import json import os import sys import unittest @@ -36,28 +35,28 @@ 'count': 4, 'matches': [{'ofs': 666, 'ply': [77]}, {'ofs': 164246, 'ply': [83]}]}, {'q': {'white-move': 'Nb7'}, - 'count': 6, 'matches': [{'ofs': 141745, 'ply': [34]}, {'ofs': 538533, 'ply': [36]}]}, + 'count': 6, 'matches': [{'ofs': 141745, 'ply': [35]}, {'ofs': 538533, 'ply': [37]}]}, {'q': {'black-move': 'c3'}, - 'count': 27, 'matches': [{'ofs': 10226, 'ply': [33]}, {'ofs': 26360, 'ply': [7]}]}, + 'count': 27, 'matches': [{'ofs': 10226, 'ply': [34]}, {'ofs': 26360, 'ply': [8]}]}, {'q': {'black-move': 'e1=Q'}, - 'count': 1, 'matches': [{'ofs': 666, 'ply': [103]}]}, + 'count': 1, 'matches': [{'ofs': 666, 'ply': [104]}]}, {'q': {'black-move': 'O-O'}, - 'count': 354, 'matches': [{'ofs': 0, 'ply': [15]}, {'ofs': 666, 'ply': [31]}]}, + 'count': 354, 'matches': [{'ofs': 0, 'ply': [16]}, {'ofs': 666, 'ply': [32]}]}, {'q': {'skip': 200, 'limit': 100, 'black-move': 'O-O'}, - 'count': 100, 'matches': [{'ofs': 485616, 'ply': [15]}, {'ofs': 487518, 'ply': [11]}]}, + 'count': 100, 'matches': [{'ofs': 485616, 'ply': [16]}, {'ofs': 487518, 'ply': [12]}]}, {'q': {'black-move': 'O-O-O'}, - 'count': 28, 'matches': [{'ofs': 10226, 'ply': [35]}, {'ofs': 64548, 'ply': [31]}]}, + 'count': 28, 'matches': [{'ofs': 10226, 'ply': [36]}, {'ofs': 64548, 'ply': [32]}]}, {'q': {'black-move': ['O-O-O', 'O-O']}, - 'count': 382, 'matches': [{'ofs': 0, 'ply': [15]}, {'ofs': 666, 'ply': [31]}]}, + 'count': 382, 'matches': [{'ofs': 0, 'ply': [16]}, {'ofs': 666, 'ply': [32]}]}, {'q': {'white-move': ['a7', 'b7']}, - 'count': 16, 'matches': [{'ofs': 2008, 'ply': [32]}, {'ofs': 10226, 'ply': [38]}]}, + 'count': 16, 'matches': [{'ofs': 2008, 'ply': [33]}, {'ofs': 10226, 'ply': [39]}]}, {'q': {'imbalance': 'vPP'}, 'count': 52, 'matches': [{'ofs': 3313, 'ply': [12]}, {'ofs': 8436, 'ply': [12]}]}, @@ -66,7 +65,7 @@ 'count': 142, 'matches': [{'ofs': 666, 'ply': [42]}, {'ofs': 16551, 'ply': [25]}]}, {'q': {'moved': 'KP', 'captured': 'Q'}, - 'count': 51, 'matches': [{'ofs': 666, 'ply': [45]}, {'ofs': 8436, 'ply': [41]}]}, + 'count': 51, 'matches': [{'ofs': 666, 'ply': [46]}, {'ofs': 8436, 'ply': [42]}]}, {'q': {'result-type': 'mate', 'result': '0-1'}, 'count': 10, 'matches': [{'ofs': 11831, 'ply': [24]}, {'ofs': 30634, 'ply': [40]}]}, @@ -87,28 +86,31 @@ {'q': {'streak': [{'sub-fen': 'r1bqkb1r/pppp1ppp/2n2n2/1B2p3/4P3/2N2N2/PPPP1PPP/R1BQK2R'}, {'result': '0-1'}, {'result': '0-1'}]}, - 'count': 2 , 'matches': [{'ofs': 19722, 'ply': [7, 8, 9]}, {'ofs': 21321, 'ply': [7, 8, 9]}]}, + 'count': 2, 'matches': [{'ofs': 19722, 'ply': [7, 8, 9]}, {'ofs': 21321, 'ply': [7, 8, 9]}]}, {'q': {'sequence': [{'sub-fen': 'rnbqkb1r/pp1p1ppp/4pn2/2pP4/2P5/2N5/PP2PPPP/R1BQKBNR'}, {'streak': [{'white-move': 'e5'}, {'black-move': 'dxe5'}, {'white-move': 'f5'}]}, {'white-move': 'Ne4'}]}, - 'count': 1 , 'matches': [{'ofs': 0, 'ply': [7, 36, 37, 38, 42]}]}, + 'count': 1, 'matches': [{'ofs': 0, 'ply': [7, 37, 38, 39, 43]}]}, {'q': {'sequence': [{'sub-fen': 'rnbqkb1r/pp1p1ppp/4pn2/2pP4/2P5/2N5/PP2PPPP/R1BQKBNR'}, {'streak': [{'white-move': 'e5'}, {'black-move': 'dxe5'}, {'white-move': 'f5'}, {'white-move': 'Ne4'}]}]}, - 'count':0 , 'matches': []}, + 'count': 0, 'matches': []}, {'q': {'sequence': [{'sub-fen': 'rnbqkb1r/pp1p1ppp/4pn2/2pP4/2P5/2N5/PP2PPPP/R1BQKBNR'}, {'streak': [{'white-move': 'e5'}, {'pass': ''}, {'white-move': 'f5'}]}, {'white-move': 'Ne4'}]}, - 'count': 1, 'matches': [{'ofs': 0, 'ply': [7, 36, 37, 38, 42]}]}, + 'count': 1, 'matches': [{'ofs': 0, 'ply': [7, 37, 38, 39, 43]}]}, {'q': {'streak': [{'imbalance': 'NNvB'}, {'imbalance': 'NNvB'}, {'imbalance': 'NNvB'}]}, 'count': 4, 'matches': [{'ofs': 82982, 'ply': [39, 40, 41]}, {'ofs': 99933, 'ply': [37, 38, 39]}]}, {'q': {'streak': [{'moved': 'P', 'captured': 'Q'}, {'captured': ''}]}, - 'count': 24, 'matches': [{'ofs': 19722, 'ply': [34, 35]}, {'ofs': 21321, 'ply': [34, 35]}]}, + 'count': 24, 'matches': [{'ofs': 19722, 'ply': [35, 36]}, {'ofs': 21321, 'ply': [35, 36]}]}, + + {'q': {'white-move': 'e4', 'stm': 'balck', 'sub-fen': 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR'}, + 'count': 229, 'matches': [{'ofs': 666, 'ply': [1]}]}, ]