Belofte version 2.2.0
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 {}
22 , m_lmoves{ml.m_lmoves}
23 , flags{ml.flags}
24 , m_nQSMoves{ml.m_nQSMoves}
25 , m_bestmoveid{ml.m_bestmoveid}
26 {}
28 {}
29
31 { curmoveid = ml.curmoveid;
32 m_lmoves = ml.m_lmoves;
33 flags = ml.flags;
34 m_nQSMoves = ml.m_nQSMoves;
35 m_bestmoveid = ml.m_bestmoveid;
36 return *this;
37 }
38
39 // no copy or move ctor nor assignment defined
40 bMoveList(bMoveList const& ml) = delete;
41 bMoveList(bMoveList&& ml) = delete;
42 bMoveList& operator=(bMoveList&& ml) = delete;
43
47 void emptyMoveList();
48
49 constexpr movenum_t getNumberOfMoves() const
50 { return static_cast<movenum_t>(m_lmoves.size()); }
51 constexpr movenum_t getNumberOfQSMoves() const // return number of non silent moves
52 { return m_nQSMoves; }
53
54 inline bMove const& operator[](movenum_t const moveid) const
55 { return m_lmoves[moveid - 1]; }
56 constexpr fromto_t getFromTo(movenum_t const moveid) const
57 { return m_lmoves[moveid - 1].getFromTo(); }
58 constexpr bmove_t getBMoveT(movenum_t const moveid) const
59 { return m_lmoves[moveid - 1].getBMoveT(); }
60
65
66 void sortMoves(bool const bFastSort);
68 { for (bMove& m: m_lmoves) m.clearScore(); }
69 void clearScores(bScore const sc)
70 { for (bMove& m: m_lmoves) m.clearScore(sc); }
71
72 // manage best score and best move
73 bool setScoreOfMove(movenum_t const moveid, bScore const score);
74 bool setMinScoreOfMove(movenum_t const moveid, bScore const score);
75 void setScoreOfMoveUnsorted(movenum_t const moveid, bScore const score);
76 void setMinScoreOfMoveUnsorted(movenum_t const moveid, bScore const score);
77
78 constexpr bScore getBestMoveScore() const
79 { return m_lmoves[m_bestmoveid - 1].getScore(); }
80 inline void setBestMoveScore(bScore const score)
81 { m_lmoves[m_bestmoveid - 1].setScore(score); }
82 inline void setBestMoveScore(movenum_t const moveid, bScore const score)
83 { m_bestmoveid = moveid; setMoveScore(moveid, score); }
84 inline void setMoveScore(movenum_t const moveid, bScore const score)
85 { m_lmoves[moveid - 1].setScore(score); }
86
87 constexpr movenum_t getBestMoveId() const
88 { return m_bestmoveid; }
89 inline void setBestMoveId(movenum_t const n)
90 { m_bestmoveid = n; }
91 inline void clearBestMoveId()
92 { m_bestmoveid = 0; }
93
94 inline void setNeedSorted()
95 { flags &= ~(0x10); }
96 inline void clearNeedSorted()
97 { flags |= 0x10; }
98 inline void setKeepScores()
99 { flags &= ~(0x20); }
100 inline void clearKeepScores()
101 { flags |= 0x20; }
102
103 // current move in iterator
105
106protected:
107 constexpr bool isGenerated() const
108 { return flags & 0x01; }
109 inline void setIsGenerated()
110 { flags |= 0x01; }
111 inline void clearIsGenerated()
112 { flags &= ~(0x01); }
113 constexpr bool isSorted() const
114 { return flags & 0x02; }
115 constexpr bool isNotSorted() const
116 { return !(flags & 0x02); }
117 inline void setIsSorted()
118 { flags |= 0x02; }
119 inline void clearIsSorted()
120 { flags &= ~(0x02); }
121
122 // the best move is much better than the second best move
123 constexpr bool isOnlyMove() const
124 { return flags & 0x04; }
125 inline void setIsOnlyMove()
126 { flags |= 0x04; }
127 inline void clearIsOnlyMove()
128 { flags &= ~(0x04); }
129
130 // a move was found previously on this board
131 constexpr bool isPossibleMove() const
132 { return getBestMoveId() || (flags & 0x08); }
133 inline void setIsPossibleMove()
134 { flags |= 0x08; }
136 { flags &= ~(0x08); }
137
138 constexpr bool isNeedSorted() const
139 { return !(flags & 0x10); }
140 constexpr bool isNoNeedSorted() const
141 { return flags & 0x10; }
142 constexpr bool isKeepScores() const
143 { return !(flags & 0x20); }
144 constexpr bool isNoKeepScores() const
145 { return flags & 0x20; }
146
147private:
148 friend std::ostream& operator<<(std::ostream& os, bMoveList const& ml);
149
150 inline bool bestMoveHasImproved(bScore const score) const
152 inline bool minMoveHasImproved(bScore const score) const
154
155 bool bestMoveHasImprovedNS(bScore const score) const;
156 void addMoveToList(bBasicBoard const& b, bMove& m, bool const isCheckMove);
157
158 movelist_t m_lmoves; // = decltype(bMoveList::ml_moves){};
159 uint8_t flags = 0; // 0x01 generated
160 // 0x02 sorted
161 // 0x04 large gap in between best and second best move
162 // 0x08 there is at least a possible move
163 // 0x10 no need to sort (random/perft/bf)
164 // 0x20 no need to keep scores (random/perft)
165 movenum_t m_nQSMoves = 0;
166 movenum_t m_bestmoveid = 0;
167};
168
169#endif // defined MOVELIST_H
170
171// eof
uint32_t bmove_t
Definition belofte.h:101
uint_fast8_t movenum_t
Definition belofte.h:103
uint16_t fromto_t
Definition belofte.h:100
Definition move.h:13
constexpr bool isGenerated() const
Definition movelist.h:107
~bMoveList()
Definition movelist.h:27
void emptyMoveList()
Definition movelist.cpp:447
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
void setBestMoveScore(movenum_t const moveid, bScore const score)
Definition movelist.h:82
constexpr bScore getBestMoveScore() const
Definition movelist.h:78
bMoveList & operator=(bMoveList &&ml)=delete
void setMinScoreOfMoveUnsorted(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:230
void clearKeepScores()
Definition movelist.h:100
constexpr bool isNoKeepScores() const
Definition movelist.h:144
constexpr bool isKeepScores() const
Definition movelist.h:142
movenum_t addBlackMoveIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:267
void clearIsSorted()
Definition movelist.h:119
constexpr bool isNoNeedSorted() const
Definition movelist.h:140
void clearBestMoveId()
Definition movelist.h:91
void setMoveScore(movenum_t const moveid, bScore const score)
Definition movelist.h:84
bool setScoreOfMove(movenum_t const moveid, bScore const score)
Store score of move and update best move.
Definition movelist.cpp:157
movenum_t addBlackPromotionIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:324
constexpr fromto_t getFromTo(movenum_t const moveid) const
Definition movelist.h:56
void setIsPossibleMove()
Definition movelist.h:133
void clearIsPossibleMove()
Definition movelist.h:135
void clearIsGenerated()
Definition movelist.h:111
bMoveList(bMoveList &&ml)=delete
constexpr bmove_t getBMoveT(movenum_t const moveid) const
Definition movelist.h:58
constexpr movenum_t getBestMoveId() const
Definition movelist.h:87
void setIsGenerated()
Definition movelist.h:109
constexpr bool isPossibleMove() const
Definition movelist.h:131
bMoveList & operator=(bMoveList const &ml)
Definition movelist.h:30
void setIsSorted()
Definition movelist.h:117
constexpr bool isOnlyMove() const
Definition movelist.h:123
bMoveList()
Definition movelist.h:17
friend std::ostream & operator<<(std::ostream &os, bMoveList const &ml)
Definition movelist.cpp:512
bMoveList(bMoveList const &ml)=delete
constexpr bool isNotSorted() const
Definition movelist.h:115
movenum_t addWhiteMoveIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:251
constexpr movenum_t getNumberOfMoves() const
Definition movelist.h:49
void setIsOnlyMove()
Definition movelist.h:125
bool atLeastOneMovePossible(bBasicBoard &b)
see if at least one move can be played e.g.
Definition movelist.cpp:481
movenum_t curmoveid
Definition movelist.h:104
void setNeedSorted()
Definition movelist.h:94
void clearIsOnlyMove()
Definition movelist.h:127
constexpr movenum_t getNumberOfQSMoves() const
Definition movelist.h:51
bMoveList(bMoveList &ml)
Definition movelist.h:20
void clearScores(bScore const sc)
Definition movelist.h:69
void setBestMoveId(movenum_t const n)
Definition movelist.h:89
void setKeepScores()
Definition movelist.h:98
movenum_t adjustQSMoves()
reduce the number of QS moves to filter out the best ones only
Definition movelist.cpp:460
constexpr bool isNeedSorted() const
Definition movelist.h:138
bMove const & operator[](movenum_t const moveid) const
Definition movelist.h:54
void setBestMoveScore(bScore const score)
Definition movelist.h:80
constexpr bool isSorted() const
Definition movelist.h:113
movenum_t addWhitePromotionIfValid(bBasicBoard const &b, bMove &m)
Only add move to movelist if valid.
Definition movelist.cpp:283
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
bool setMinScoreOfMove(movenum_t const moveid, bScore const score)
Store score of move and update best move (minimum)
Definition movelist.cpp:184
constexpr bScore realScore() const
Definition searchscore.h:66
int16_t bScore
Definition eval.h:11
std::vector< bMove > movelist_t
Definition movelist.h:11