Belofte version 2.2.0
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 bBoard searchBoard(b);
21 depth_t const nDepth = 0;
22 bScore score = CalcBestMove(searchBoard, ml, nDepth);
23#if defined(_DEBUG)
24 ml.setNeedSorted();
25 ml.sortMoves(false);
26#endif
27 return score;
28}
29
30/**
31 * Search from depth 0 on
32 * @param b board
33 * @param ml movelist of this position
34 * @param nDepth remaining depth to search on
35 * @return [-]SCORE_UNDEFINED | [-]SCORE_INFINITE | actual score
36 */
38{
40 if (isNoBench() && gr != GR_UNKNOWN) {
41 ++m_leafnodes; // leaf node because of final position
43 DEBUG_sendInfoSearchingNS(b, nDepth, "(draw)");
45 }
47 DEBUG_sendInfoSearching(b, nDepth, "(final score)", terminalScore);
48 return terminalScore;
49 }
50
52
53 if (getLevel()->searchDepthReached(nDepth)) {
55 bScore terminalScore = RetrieveBoardEvaluation(b, gr, true);
56 DEBUG_sendInfoSearching(b, nDepth, "Depth reached", terminalScore);
57 return terminalScore;
58 }
59
60 if (nDepth) ++m_nonleafnodes;
61 if (nDepth > 2 && nDepth % 2) CheckIfAbortingSearch();
62
63 ml.clearNeedSorted();
64 ml.clearKeepScores();
65 movenum_t n_moves = ml.generateMoves(b);
66
67 if (!n_moves && isBench()) {
68 bScore terminalScore = RetrieveBoardEvaluation(b, gr, true);
69 DEBUG_sendInfoSearching(b, nDepth, "Dead position", terminalScore);
70 return terminalScore;
71 }
72
73 depth_t nNewDepth = nDepth + 1;
74 ml.clearScores();
75 ml.setKeepScores();
76 for (ml.curmoveid = 1; ml.curmoveid <= n_moves; ++ml.curmoveid) {
77 bMove m(ml[ml.curmoveid]);
78 sendInfoCurrMove(b, nDepth, m, ml.curmoveid);
79 bMoveList chldML;
80 boardInfo_t bi = b.bBasicBoard::applyMove(m);
81 bScore chldscore = -CalcBestMove(b, chldML, nNewDepth);
82 b.bBasicBoard::unApplyMove(m, bi);
83 ml.setScoreOfMoveUnsorted(ml.curmoveid, chldscore);
84 }
85
87 ml.setBestMoveScore(bestScore);
88 DEBUG_sendInfoSearching(b, nDepth, "(return best)", bestScore);
89 return bestScore;
90}
91
92// 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:103
int_fast8_t depth_t
Definition belofte.h:106
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:172
board
Definition board.h:45
void clearScores()
Definition movelist.h:67
void clearNeedSorted()
Definition movelist.h:96
movenum_t generateMoves(bBasicBoard const &b)
generate moves if not yet generated
Definition movelist.cpp:417
void setScoreOfMoveUnsorted(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:210
constexpr bScore getBestMoveScore() const
Definition movelist.h:78
void clearKeepScores()
Definition movelist.h:100
movenum_t curmoveid
Definition movelist.h:104
void setNeedSorted()
Definition movelist.h:94
void setKeepScores()
Definition movelist.h:98
void setBestMoveScore(bScore const score)
Definition movelist.h:80
void sortMoves(bool const bFastSort)
sort moves and update bestmove id if less than 5 moves, sort all if more than 5 moves,...
Definition movelist.cpp:369
static bScore resultToScoreFlag(gameResult_t const gr)
Class static function convert all draw scores to SCORE_THEORETIC_DRAW.
Definition eval.cpp:74
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:67
void adjustMaxSearchedDepth(depth_t const nDepth)
Definition search.h:152
void sendInfoCurrMove(bBoard const &b, depth_t const nCurDepth, bMove const &m, movenum_t const moveid) const
Definition search.cpp:282
constexpr bool isNoBench() const
Definition search.h:132
bLevel * getLevel()
Definition search.h:161
int64_t m_nonleafnodes
Definition search.h:145
constexpr bool isBench() const
Definition search.h:134
int64_t m_leafnodes
Definition search.h:144
bScore RetrieveBoardEvaluation(bBoard &b, gameResult_t const gr, bool const bRecalcFirst) const
Get score of board, eventually from cache.
Definition search.cpp:302
void CheckIfAbortingSearch() const
Definition search.cpp:223
bScore convergeScore()
@ GR_UNKNOWN
Definition eval.h:33
enum gameResult gameResult_t
Definition eval.h:40
int16_t bScore
Definition eval.h:11
constexpr bScore SCORE_THEORETIC_DRAW
Definition eval.h:16
#define DEBUG_sendInfoSearchingNS(b, depth, msg)
Definition search.h:54
#define DEBUG_sendInfoSearching(b, depth, msg, sc)
Definition search.h:53