@@ -200,7 +200,7 @@ int pvSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Searc
200200#ifdef TRACE_SEARCH
201201 stats.ttProbes ++;
202202
203- if (ttScore != NO_TT_SCORE ) {
203+ if (ttEntry ) {
204204 stats.ttHits ++;
205205 }
206206#endif
@@ -500,6 +500,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
500500 const std::chrono::time_point<std::chrono::steady_clock>& endTime, SearchStack& searchStack) {
501501 assert (nodeType != ROOT);
502502 constexpr bool isPV = nodeType == PV;
503+ constexpr PieceColor opponentColor = !color;
503504
504505 if (board.isDraw ()) {
505506 return DRAW_SCORE;
@@ -512,30 +513,29 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
512513 }
513514 }
514515
515- if (!isPV) {
516- TTEntry* ttEntry = nullptr ;
517- const int16_t score = tt->probePosition (board.getZobristHash (), depth, alpha, beta, board.getPly (), ttEntry);
518-
519516#ifdef TRACE_SEARCH
520- stats.qTtProbes ++;
521- #endif
517+ stats.qTtProbes ++;
522518
523- if (score != NO_TT_SCORE ) {
524- # ifdef TRACE_SEARCH
525- stats. qTtHits ++;
519+ if (tt-> getEntry (board. getZobristHash ()) != nullptr ) {
520+ stats. qTtHits ++;
521+ }
526522#endif
527- return score;
523+
524+ TTEntry* ttEntry = nullptr ;
525+ const int16_t ttScore = tt->probePosition (board.getZobristHash (), depth, alpha, beta, board.getPly (), ttEntry);
526+
527+ if (!isPV) {
528+ if (ttScore != NO_TT_SCORE) {
529+ return ttScore;
528530 }
529531 }
530532
531533 stats.qNodesSearched += 1 ;
532534
533535 const bool isInCheck = board.isKingInCheck <color>();
534- int bestScore;
536+ int bestScore = -MATE_SCORE ;
535537
536- if (isInCheck) {
537- bestScore = -MATE_SCORE;
538- } else {
538+ if (!isInCheck) {
539539 bestScore = Evaluation (board).evaluate ();
540540
541541 // Stand pat
@@ -544,9 +544,16 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
544544#ifdef TRACE_SEARCH
545545 stats.ttWrites ++;
546546#endif
547- tt->savePosition (board.getZobristHash (), depth, board.getPly (), bestScore, NO_MOVE, BETA);
547+
548+ if (!ttEntry) {
549+ tt->savePosition (board.getZobristHash (), depth, board.getPly (), bestScore, NO_MOVE, BETA);
550+ }
548551 }
549552
553+ # ifdef TRACE_SEARCH
554+ stats.standpatPrunes ++;
555+ # endif
556+
550557 return bestScore;
551558 }
552559
@@ -598,8 +605,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
598605
599606 legalMoves += 1 ;
600607
601- const int score =
602- -qSearch<!color, nodeType>(engine, board, -beta, -alpha, depth - 1 , stats, endTime, searchStack);
608+ const int score = -qSearch<!color, nodeType>(engine, board, -beta, -alpha, depth - 1 , stats, endTime, searchStack);
603609
604610 board.unmakeMove ();
605611
@@ -611,14 +617,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
611617 alpha = score;
612618
613619 if (score >= beta) {
614- if (!engine.isSearchStopped ()) {
615- #ifdef TRACE_SEARCH
616- stats.ttWrites ++;
617- #endif
618- tt->savePosition (board.getZobristHash (), depth, board.getPly (), score, move, BETA);
619- }
620-
621- return score;
620+ break ;
622621 }
623622 }
624623 }
@@ -628,7 +627,7 @@ int qSearch(Engine& engine, Board& board, int alpha, int beta, int depth, Search
628627 bestScore = -MATE_SCORE + board.getPly ();
629628 }
630629
631- const TTNodeType ttNodeType = (isPV && bestMove != NO_MOVE) ? EXACT : ALPHA;
630+ const TTNodeType ttNodeType = bestScore >= beta ? BETA : ALPHA;
632631
633632 if (!engine.isSearchStopped ()) {
634633#ifdef TRACE_SEARCH
@@ -728,6 +727,7 @@ void SearchStats::printTrace(Engine& engine, int numPositions) const {
728727 printStatLine (" Check Extensions" , checkExtensions);
729728
730729 ss << " \n --- Quiescence Search ---\n " ;
730+ printStatLine (" Standpat Prunes" , standpatPrunes);
731731 printStatLine (" SEE Prunes" , seePrunes);
732732
733733 ss << " -------------------------" ;
@@ -755,6 +755,7 @@ SearchStats& SearchStats::operator+=(const SearchStats& other) {
755755 checkExtensions += other.checkExtensions ;
756756 singularExtensions += other.singularExtensions ;
757757 singularAttempts += other.singularAttempts ;
758+ standpatPrunes += other.standpatPrunes ;
758759 seePrunes += other.seePrunes ;
759760
760761 return *this ;
0 commit comments