Belofte version 2.1.9
A promising chess program using the UCI or Winboard interface
movelist.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: movelist.h
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#if !defined(MOVELIST_H)
9#define MOVELIST_H
10
11typedef std::vector<bMove> movelist_t;
12
13//-----------------------------------------------------------------------
14
15class bMoveList {
16public:
17 explicit bMoveList()
18 : m_lmoves{}
19 {}
21 {}
22
23 // no copy or move ctor nor assignment defined
24 bMoveList(bMoveList const& ml) = delete;
25 bMoveList(bMoveList&& ml) = delete;
26 bMoveList& operator=(bMoveList const& ml) = delete;
27 bMoveList& operator=(bMoveList&& ml) = delete;
28
32 void emptyMoveList();
33
34 constexpr movenum_t getNumberOfMoves() const
35 { return static_cast<movenum_t>(m_lmoves.size()); }
36 constexpr movenum_t getNumberOfQSMoves() const // return number of non silent moves
37 { return m_nQSMoves; }
38
39 inline bMove const& operator[](movenum_t const moveid) const
40 { return m_lmoves[moveid - 1]; }
41 constexpr fromto_t getFromTo(movenum_t const moveid) const
42 { return m_lmoves[moveid - 1].getFromTo(); }
43 constexpr bmove_t getBMoveT(movenum_t const moveid) const
44 { return m_lmoves[moveid - 1].getBMoveT(); }
45
50
51 void sortMoves();
53 { for (bMove& m: m_lmoves) m.clearScore(); }
54
55 // manage best score and best move
56 bool setScoreOfMove(movenum_t const moveid, bScore const score);
57 void setScoreOfMoveUnsorted(movenum_t const moveid, bScore const score);
58
59 constexpr bScore getBestMoveScore() const
60 { return m_lmoves[m_bestmoveid - 1].getScore(); }
61 inline void setBestMoveScore(movenum_t const moveid, bScore const score)
62 { m_bestmoveid = moveid; setMoveScore(moveid, score); }
63 inline void setMoveScore(movenum_t const moveid, bScore const score)
64 { m_lmoves[moveid - 1].setScore(score); }
65
66 constexpr movenum_t getBestMoveId() const
67 { return m_bestmoveid; }
68 inline void setBestMoveId(movenum_t const n)
69 { m_bestmoveid = n; }
70 inline void clearBestMoveId()
71 { m_bestmoveid = 0; }
72
73 inline void setNeedSorted()
74 { flags &= ~(0x10); }
75 inline void clearNeedSorted()
76 { flags |= 0x10; }
77 inline void setKeepScores()
78 { flags &= ~(0x20); }
79 inline void clearKeepScores()
80 { flags |= 0x20; }
81
82protected:
83 constexpr bool isGenerated() const
84 { return flags & 0x01; }
85 inline void setIsGenerated()
86 { flags |= 0x01; }
87 inline void clearIsGenerated()
88 { flags &= ~(0x01); }
89 constexpr bool isSorted() const
90 { return flags & 0x02; }
91 constexpr bool isNotSorted() const
92 { return !(flags & 0x02); }
93 inline void setIsSorted()
94 { flags |= 0x02; }
95 inline void clearIsSorted()
96 { flags &= ~(0x02); }
97
98 // the best move is much better than the second best move
99 constexpr bool isOnlyMove() const
100 { return flags & 0x04; }
101 inline void setIsOnlyMove()
102 { flags |= 0x04; }
103 inline void clearIsOnlyMove()
104 { flags &= ~(0x04); }
105
106 // a move was found previously on this board
107 constexpr bool isPossibleMove() const
108 { return getBestMoveId() || (flags & 0x08); }
109 inline void setIsPossibleMove()
110 { flags |= 0x08; }
112 { flags &= ~(0x08); }
113
114 constexpr bool isNeedSorted() const
115 { return !(flags & 0x10); }
116 constexpr bool isNoNeedSorted() const
117 { return flags & 0x10; }
118 constexpr bool isKeepScores() const
119 { return !(flags & 0x20); }
120 constexpr bool isNoKeepScores() const
121 { return flags & 0x20; }
122
123private:
124 friend std::ostream& operator<<(std::ostream& os, bMoveList const& ml);
125
126 inline bool bestMoveHasImproved(bScore const score) const
128
129 void addMoveToList(bBasicBoard const& b, bMove& m, bool const isCheckMove);
130
131 movelist_t m_lmoves; // = decltype(bMoveList::ml_moves){};
132 uint8_t flags = 0; // 0x01 generated
133 // 0x02 sorted
134 // 0x04 large gap in between best and second best move
135 // 0x08 there is at least a possible move
136 // 0x10 no need to sort (random/perft/bf)
137 // 0x20 no need to keep scores (random/perft)
138 movenum_t m_nQSMoves = 0;
139 movenum_t m_bestmoveid = 0;
140};
141
142#endif // defined MOVELIST_H
143
144// eof
uint32_t bmove_t
Definition belofte.h:98
uint_fast8_t movenum_t
Definition belofte.h:100
uint16_t fromto_t
Definition belofte.h:97
Definition move.h:13
constexpr bool isGenerated() const
Definition movelist.h:83
~bMoveList()
Definition movelist.h:20
void emptyMoveList()
Definition movelist.cpp:370
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
bMoveList & operator=(bMoveList const &ml)=delete
void setBestMoveScore(movenum_t const moveid, bScore const score)
Definition movelist.h:61
constexpr bScore getBestMoveScore() const
Definition movelist.h:59
bMoveList & operator=(bMoveList &&ml)=delete
void clearKeepScores()
Definition movelist.h:79
constexpr bool isNoKeepScores() const
Definition movelist.h:120
constexpr bool isKeepScores() const
Definition movelist.h:118
movenum_t addBlackMoveIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:193
void clearIsSorted()
Definition movelist.h:95
constexpr bool isNoNeedSorted() const
Definition movelist.h:116
void sortMoves()
sort moves and update bestmove id if less than 5 moves, sort all if more than 5 moves,...
Definition movelist.cpp:293
void clearBestMoveId()
Definition movelist.h:70
void setMoveScore(movenum_t const moveid, bScore const score)
Definition movelist.h:63
bool setScoreOfMove(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:130
movenum_t addBlackPromotionIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:250
constexpr fromto_t getFromTo(movenum_t const moveid) const
Definition movelist.h:41
void setIsPossibleMove()
Definition movelist.h:109
void clearIsPossibleMove()
Definition movelist.h:111
void clearIsGenerated()
Definition movelist.h:87
bMoveList(bMoveList &&ml)=delete
constexpr bmove_t getBMoveT(movenum_t const moveid) const
Definition movelist.h:43
constexpr movenum_t getBestMoveId() const
Definition movelist.h:66
void setIsGenerated()
Definition movelist.h:85
constexpr bool isPossibleMove() const
Definition movelist.h:107
void setIsSorted()
Definition movelist.h:93
constexpr bool isOnlyMove() const
Definition movelist.h:99
bMoveList()
Definition movelist.h:17
friend std::ostream & operator<<(std::ostream &os, bMoveList const &ml)
Definition movelist.cpp:435
bMoveList(bMoveList const &ml)=delete
constexpr bool isNotSorted() const
Definition movelist.h:91
movenum_t addWhiteMoveIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:177
constexpr movenum_t getNumberOfMoves() const
Definition movelist.h:34
void setIsOnlyMove()
Definition movelist.h:101
bool atLeastOneMovePossible(bBasicBoard &b)
see if at least one move can be played e.g.
Definition movelist.cpp:404
void setNeedSorted()
Definition movelist.h:73
void clearIsOnlyMove()
Definition movelist.h:103
constexpr movenum_t getNumberOfQSMoves() const
Definition movelist.h:36
void setBestMoveId(movenum_t const n)
Definition movelist.h:68
void setKeepScores()
Definition movelist.h:77
movenum_t adjustQSMoves()
reduce the number of QS moves to filter out the best ones only
Definition movelist.cpp:383
constexpr bool isNeedSorted() const
Definition movelist.h:114
bMove const & operator[](movenum_t const moveid) const
Definition movelist.h:39
constexpr bool isSorted() const
Definition movelist.h:89
movenum_t addWhitePromotionIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:209
constexpr bScore realScore() const
Definition searchscore.h:66
int16_t bScore
Definition eval.h:11
std::vector< bMove > movelist_t
Definition movelist.h:11