Belofte version 2.1.9
A promising chess program using the UCI or Winboard interface
search_bf.cpp
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: search_bf.cpp
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#include "belofte.h"
9
10//-----------------------------------------------------------------------
11
12/**
13 * Root search for BF, depth 0
14 * @param b board
15 * @param ml movelist of this position
16 * @todo search from depth till zero
17 */
19{
20 depth_t const nDepth = 0;
21 return CalcBestMove(b, ml, nDepth);
22}
23
24/**
25 * Search from depth 0 on
26 * @param b board
27 * @param ml movelist of this position
28 * @param nDepth remaining depth to search on
29 * @return SCORE_UNDEFINED | [-]SCORE_INFINITE | actual score
30 */
32{
34 if (isNoBench() && gr != GR_UNKNOWN) {
35 ++m_leafnodes; // leaf node because of final position
37 DEBUG_sendInfoSearchingNS(b, nDepth, "(draw)");
38 return 0;
39 }
41 DEBUG_sendInfoSearching(b, nDepth, "(final score)", terminalScore);
42 return terminalScore;
43 }
44
46
47 if (getLevel()->searchDepthReached(nDepth)) {
50 bScore terminalScore = RetrieveBoardEvaluation(b, gr);
51 DEBUG_sendInfoSearching(b, nDepth, "Depth reached", terminalScore);
52 return terminalScore;
53 }
54
55 if (nDepth) ++m_nonleafnodes;
56 if (nDepth > 2 && nDepth % 2) CheckIfAbortingSearch();
57
58 ml.clearNeedSorted();
59 ml.clearKeepScores();
60 movenum_t n_moves = ml.generateMoves(b);
61
62 if (!n_moves) {
64 bScore terminalScore = RetrieveBoardEvaluation(b, gr);
65 DEBUG_sendInfoSearching(b, nDepth, "Dead position", terminalScore);
66 return terminalScore;
67 }
68
69 depth_t nNewDepth = nDepth + 1;
70 ml.clearScores();
71 ml.setKeepScores();
72 for (movenum_t moveid = 1; moveid <= n_moves; ++moveid) {
73 bMove m(ml[moveid]);
74 sendInfoCurrMove(b, nDepth, m, moveid);
75 boardInfo_t bi = b.applyMove(m);
76 bMoveList chldML;
77 bScore chldscore = bSearchScore::convergeScore(-CalcBestMove(b, chldML, nNewDepth));
78 b.unApplyMove(m, bi);
79 ml.setScoreOfMoveUnsorted(moveid, chldscore);
80 }
81
82 DEBUG_sendInfoSearching(b, nDepth, "(return best)", ml.getBestMoveScore());
83 return ml.getBestMoveScore();
84}
85
86// eof
union boardInfo boardInfo_t
This is the main include file, needs to be included before any other include.
uint_fast8_t movenum_t
Definition belofte.h:100
int_fast8_t depth_t
Definition belofte.h:103
bScore CalcBestMove(bBoard &b, bMoveList &ml) override
Root search for BF, depth 0.
Definition search_bf.cpp:18
constexpr bScore minimizing() const
Definition basicboard.h:166
void unApplyMove(bMove const &m, boardInfo_t const oldBoardInfo)
exact restoration of basic board using move details
board
Definition board.h:45
boardInfo_t applyMove(bMove const &m) override
modification of board move is kept on previous board newboard does not have move stored and has flag ...
Definition board.cpp:217
void calcMinorPieces(bool const bForceRecalc=false)
Recalculate minor pieces, used for evaluation and end of game condition in case of less than 5 pieces...
Definition board.cpp:68
void clearScores()
Definition movelist.h:52
void clearNeedSorted()
Definition movelist.h:75
movenum_t generateMoves(bBasicBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:340
void setScoreOfMoveUnsorted(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:156
constexpr bScore getBestMoveScore() const
Definition movelist.h:59
void clearKeepScores()
Definition movelist.h:79
void setKeepScores()
Definition movelist.h:77
static bScore resultToScoreFlag(gameResult_t const gr)
Class static function convert all draw scores to SCORE_THEORETIC_DRAW.
Definition eval.cpp:77
static gameResult_t gameEndedResult(bBoard const &b)
Class static function See if board is in finite state, meaning game is ended.
Definition eval.cpp:42
static bool isDrawResult(gameResult_t const gr)
Definition eval.h:70
void adjustMaxSearchedDepth(depth_t const nDepth)
Definition search.h:132
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid) const
Definition search.cpp:223
constexpr bool isNoBench() const
Definition search.h:114
bLevel * getLevel()
Definition search.h:141
int64_t m_nonleafnodes
Definition search.h:125
int64_t m_leafnodes
Definition search.h:124
bScore RetrieveBoardEvaluation(bBoard &b, gameResult_t const gr=GR_UNSET) const
Get score of board, eventually from cache.
Definition search.cpp:242
void CheckIfAbortingSearch() const
Definition search.cpp:168
bScore convergeScore()
Definition searchscore.h:95
@ GR_UNKNOWN
Definition eval.h:36
enum gameResult gameResult_t
Definition eval.h:43
int16_t bScore
Definition eval.h:11
#define DEBUG_sendInfoSearchingNS(b, depth, msg)
Definition search.h:36
#define DEBUG_sendInfoSearching(b, depth, msg, sc)
Definition search.h:35