Belofte version 2.1.8
A promising chess program using the UCI or Winboard interface
board.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: board.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(BOARD_H)
9#define BOARD_H
10
11enum tCFlags { WCASTLE_S = 1, WCASTLE_L = 2, BCASTLE_S = 4, BCASTLE_L = 8 };
12
13typedef
14 union {
16 struct {
19 plynum_t ply; // counting from zero for white to move at start position
20 case_t ep; // en passant field
22 uint8_t castling; /// 4 castling bits, plus incheck flag and capture move
23 /// 0b00001111
24 /// ^ latest was capture move (0x40 - 64)
25 /// ^ latest was castle move (0x20 - 32)
26 /// ^ in check (0x10 - 16)
27 /// ^ black castle long allowed (0x08 - 8)
28 /// ^ black castle short allowed (0x04 - 4)
29 /// ^ white castle long allowed (0x02 - 2)
30 /// ^ white castle short allowed (0x01 - 1)
31 uint8_t capturedpiece; // piece captured during executing move, not part
32 // of oldflags but of move being executed (optimising return flags)
33 };
35
36//-----------------------------------------------------------------------
37
39protected:
41 explicit bBasicBoard(bBasicBoard const& bb);
42 explicit bBasicBoard(bBasicBoard&& bb);
43 explicit bBasicBoard(bFen const& fen);
44 virtual ~bBasicBoard();
45
46 // no move ctor nor assignment defined
49
50public:
51 // move ply
52 plynum_t getPly() const { return m_ply; }
53 void setPly(plynum_t const p) { m_ply = p; }
54 void incPly() { ++m_ply; }
55 void decPly() { --m_ply; }
56
57 // e.p. case
58 case_t getEp() const { return m_ep; }
59 void setEp(case_t const e);
61
62 // 50 moves counter
64 void setPly50Moves(movenum50_t const p) { m_ply50moves = p; }
67
68 // castle
69 bool canCastle() const;
70 bool canCastle(uint8_t const f) const;
71 void setCastle(uint8_t const f);
72 void setCastle(uint8_t const f, bool const c);
73 void clearCastle(uint8_t const f);
74
75 bool isInCheck() const;
76 virtual void setInCheck();
77 void clearInCheck();
78
79 // board information methods
80 bool whiteToMove() const { return ((m_ply & 0x0001) == 0); }
81 int16_t getMoveNumber() const { return (m_ply >> 1) + 1; }
83 return static_cast<side_t>((m_ply & 0x0001) + 1); } /// 1 for white, 2 for black
84
85 // field and piece access methods
86 piece_t getPiece(case_t const bc) const;
87 piece_t getPiece(column_t const iColumn, rank_t const iRank) const;
88 piece_t getPieceCtl(column_t const iColumn, rank_t const iRank) const;
89 void setPiece(case_t const bc, piece_t const piece);
90 void setPiece(case_t const bc, uint8_t const piece);
91 piece_t setGetPiece(column_t const newcol, rank_t const newrank,
92 piece_t const piece);
93 piece_t removePiece(column_t const newcol, rank_t const newrank);
94 void movePiece(case_t const f, case_t const t, piece_t const p);
95 void swapPiece(case_t const t, piece_t const op, piece_t const np);
96 void clearPiece(case_t const bc);
97 void moveWhiteKing(case_t const f, case_t const t);
98 void moveBlackKing(case_t const f, case_t const t);
99 bool isFieldEmpty(case_t const bc) const;
100 bool isFieldEmpty(column_t const iColumn, rank_t const iRank) const;
101 virtual void setCapture(piece_t const p, case_t const c);
104 bFen getFEN() const;
105 std::string getHashStr() const;
106 hashkey_t getHash() const { return m_hash; }
107 void setHash(hashkey_t const newHash) { m_hash = newHash; }
108
109 // make and unmake move on board
111 rank_t const oldrank, column_t const newcol,
112 rank_t const newrank, piece_t const promotion);
113 void unMakeBoardMove(case_t const& cf, column_t const newcol,
114 rank_t const newrank, u_positionFlags_t const oldFlags,
115 piece_t const promotion);
116
117protected:
118 void calcHash(); /// calculate and set hash
119 void setPieceKU(case_t const bc, piece_t const piece); // and update king position
120
121 union {
122 uint64_t u_positionFlags = 0ULL; // initialise all subsequent fields to 0
123 struct {
126 plynum_t m_ply; // counting from zero for white to move at start position
127 case_t m_ep; // en passant field
129 uint8_t m_castling; /// 4 castling bits as uints as copy ctor stumbles on bool array
130 piece_t m_capturedpiece; /// @todo move to bBoard
131 };
132 };
134
135private:
136 std::array<piece_t, 64> m_fields;
137};
138
139//-----------------------------------------------------------------------
140
141typedef std::vector<std::string> movesequence_t;
143
144//-----------------------------------------------------------------------
145
146/** board */
147class bBoard final : public bBasicBoard {
148public:
149 explicit bBoard(bBoard&& b) noexcept;
150 explicit bBoard(bBoard const& b);
151 explicit bBoard(bBoard const& b, movenum_t const moveid);
152 explicit bBoard(bBoard const& b, bMove const& m);
153 explicit bBoard(bBoard const& b, bMove& m);
154 explicit bBoard(bFen const& fen);
155 ~bBoard() override;
156
157 // no move ctor nor assignment defined
158 bBoard& operator=(bBoard const&) = delete;
159 bBoard& operator=(bBoard&&) = delete;
160
161 operator std::string() const;
162
163 bool atLeastOneMovePossible() const;
165 bMove const& getMove(movenum_t const moveid) const;
166
167 void calcPieces(); /// calculate pieces
168 void calcGameStage(); /// calculate gamestage
169
170 bool isNonSilent() const { return m_bNonSilent; }
171 void setNonSilent() { m_bNonSilent = true; }
172 bool isCapture() const { return m_capturedpiece; }
173 void setCapture(piece_t const p, case_t const c) override;
174 void setInCheck() final;
175
176 void invertColours();
177 bGameStage getStage() const { return m_gamestage; }
179 void setBoardEvaluation(bScore const s) { m_boardeval = s; }
180 bScore minimizing() const { return whiteToMove() ? 1 : -1; } /// -1 black to move, 1 white to move
181 bool castleDoneWhite() const { return m_castledoneWhite; }
182 bool castleDoneBlack() const { return m_castledoneBlack; }
183 void setCastleDoneWhite(bool const c) { m_castledoneWhite = c; }
184 void setCastleDoneBlack(bool const c) { m_castledoneBlack = c; }
185
186 movesequence_t const& getPreviousMoves() const { return m_previousmoves; }
187 void setPreviousMoves(movesequence_t const& v) { m_previousmoves = v; }
188 basicmove_t const& getMovePlayed() const { return m_moveplayed; }
189 movesequence_t const& getVariation() const { return m_variation; }
190 void clearVariation();
191 void setVariation(bBoard const& chldbrd);
192
193protected:
196 friend class bWhitePiece;
197 friend class bBlackPiece;
198
199private:
200 friend std::ostream& operator<<(std::ostream& os, bBoard const& dt);
201
202private:
203 void applyMove(bMove const& m);
204 void applyWhiteMove(bMove const& m);
205 void applyBlackMove(bMove const& m);
206
207 union {
208 uint64_t u_boardFlags1 = 0ULL; // initialise all subsequent fields to 0
209 struct {
211 uint8_t m_bNonSilent; // = false; /// used for promotion, capture and in-check
212 uint8_t m_piecesonboard; // = 0;
215 bGameStage m_gamestage; // = bGameStage::ST_UNKNOWN;
217 };
218 };
219
220 union {
221 uint32_t u_boardFlags2; // initialise all subsequent fields to 0
222 struct {
225 bScore m_boardeval = SCORE_UNDEFINED; /// result of board evaluation
226 };
227 };
228
229 bMoveList m_moves; // moves possible in position
230
231 // postbox representation of board
232 std::array<caselist_t ,tStatPiece::STAT_SIZE> m_whitePieces;
233 std::array<caselist_t ,tStatPiece::STAT_SIZE> m_blackPieces;
234
235 movesequence_t m_variation; /// variation leading to score
236 movesequence_t m_previousmoves; /// movesequence to get here
237};
238
239// --------------------------------------------------------------------
240
241#endif // defined BOARD_H
242
243// eof
unsigned long long hashkey_t
Definition bel_hash.h:12
@ HP_NOEP
Definition bel_hash.h:19
int8_t rank_t
Definition belofte.h:104
uint8_t movenum50_t
Definition belofte.h:110
uint16_t basicmove_t
Definition belofte.h:107
int8_t column_t
Definition belofte.h:105
uint_fast8_t movenum_t
Definition belofte.h:109
uint8_t case_t
Definition belofte.h:106
uint16_t plynum_t
Definition belofte.h:108
int16_t bScore
tCFlags
Definition board.h:11
@ BCASTLE_L
Definition board.h:11
@ BCASTLE_S
Definition board.h:11
@ WCASTLE_S
Definition board.h:11
@ WCASTLE_L
Definition board.h:11
std::vector< std::string > movesequence_t
Definition board.h:141
bGameStage
Definition board.h:142
void setPiece(case_t const bc, piece_t const piece)
used for promotion move generation
Definition board.cpp:190
bFen getFEN() const
Definition board.cpp:114
bBasicBoard & operator=(bBasicBoard &&)=delete
uint8_t m_castling
Definition board.h:129
case_t m_blackKing
Definition board.h:125
void incPly()
Definition board.h:54
piece_t getPiece(case_t const bc) const
1 for white, 2 for black
Definition board.cpp:157
bBasicBoard & operator=(bBasicBoard const &)=delete
void setCastle(uint8_t const f)
Definition board.cpp:260
int16_t getMoveNumber() const
Definition board.h:81
void setPly50Moves(movenum50_t const p)
Definition board.h:64
hashkey_t m_hash
Definition board.h:133
bool whiteToMove() const
Definition board.h:80
plynum_t m_ply
Definition board.h:126
hashkey_t getHash() const
Definition board.h:106
case_t m_whiteKing
Definition board.h:124
u_positionFlags_t makeBoardMove(column_t const oldcol, rank_t const oldrank, column_t const newcol, rank_t const newrank, piece_t const promotion)
apply move to check if in check only, board to be discarded as it will be in incomplete state,...
Definition board.cpp:400
void setEp(case_t const e)
Definition board.cpp:233
void movePiece(case_t const f, case_t const t, piece_t const p)
Definition board.cpp:317
void moveBlackKing(case_t const f, case_t const t)
Definition board.cpp:338
side_t getColourToMove() const
Definition board.h:82
std::string getHashStr() const
Definition board.cpp:542
void setPieceKU(case_t const bc, piece_t const piece)
calculate and set hash
Definition board.cpp:221
void setHash(hashkey_t const newHash)
Definition board.h:107
void clearPiece(case_t const bc)
Definition board.cpp:228
virtual void setInCheck()
Definition board.cpp:283
void moveWhiteKing(case_t const f, case_t const t)
Definition board.cpp:330
void decPly()
Definition board.h:55
case_t getBlackKingPos() const
Definition board.h:103
virtual void setCapture(piece_t const p, case_t const c)
Clear castle flag if rook captured update piece counter, update hash, clear field.
Definition board.cpp:297
movenum50_t incPly50Moves()
Definition board.h:65
case_t m_ep
Definition board.h:127
uint64_t u_positionFlags
Definition board.h:122
piece_t m_capturedpiece
4 castling bits as uints as copy ctor stumbles on bool array
Definition board.h:130
void clearInCheck()
Definition board.cpp:288
piece_t setGetPiece(column_t const newcol, rank_t const newrank, piece_t const piece)
used for makeBoardMove
Definition board.cpp:202
piece_t removePiece(column_t const newcol, rank_t const newrank)
used for makeBoardMove
Definition board.cpp:212
void unMakeBoardMove(case_t const &cf, column_t const newcol, rank_t const newrank, u_positionFlags_t const oldFlags, piece_t const promotion)
undo makeBoardMove, restoring previous situation
Definition board.cpp:481
virtual ~bBasicBoard()
Definition board.cpp:26
case_t getWhiteKingPos() const
Definition board.h:102
void swapPiece(case_t const t, piece_t const op, piece_t const np)
Definition board.cpp:324
void clearPly50Moves()
Definition board.h:66
void calcHash()
Set hash based on board position, also calc pieces Byte 0: bits 4-6 capture count (masked) bit 7 play...
Definition board.cpp:355
case_t getEp() const
Definition board.h:58
bool isInCheck() const
Definition board.cpp:278
movenum50_t getPly50Moves() const
Definition board.h:63
bool canCastle() const
Definition board.cpp:243
piece_t getPieceCtl(column_t const iColumn, rank_t const iRank) const
retrieve piece with bounds checking, return field empty in case of out of bounds
Definition board.cpp:171
movenum50_t m_ply50moves
Definition board.h:128
void clearCastle(uint8_t const f)
Definition board.cpp:269
plynum_t getPly() const
Definition board.h:52
void clearEp()
Definition board.h:60
bool isFieldEmpty(case_t const bc) const
Definition board.cpp:178
void setPly(plynum_t const p)
Definition board.h:53
board
Definition board.h:147
void setCapture(piece_t const p, case_t const c) override
set non-silent flag, store captured piece and case (for undo)
Definition board.cpp:795
void setCastleDoneWhite(bool const c)
Definition board.h:183
void clearVariation()
Definition board.cpp:974
uint32_t u_boardFlags2
Definition board.h:221
bGameStage getStage() const
Definition board.h:177
uint8_t m_whiteminor
Definition board.h:213
void invertColours()
invert colours update kingpos, update colour to move, castle rights, ...
Definition board.cpp:764
basicmove_t m_moveplayed
Definition board.h:210
bool isCapture() const
Definition board.h:172
uint8_t m_bNonSilent
Definition board.h:211
bMove const & getMove(movenum_t const moveid) const
Definition board.cpp:967
uint64_t u_boardFlags1
Definition board.h:208
void setVariation(bBoard const &chldbrd)
Definition board.cpp:979
bBoard & operator=(bBoard const &)=delete
uint8_t m_castledoneWhite
Definition board.h:223
bool isNonSilent() const
calculate gamestage
Definition board.h:170
void setInCheck() final
Definition board.cpp:803
bMoveList & getMoveListRef()
return reference to movelist
Definition board.cpp:954
uint8_t m_blackminor
Definition board.h:214
bBoard & operator=(bBoard &&)=delete
bool castleDoneWhite() const
-1 black to move, 1 white to move
Definition board.h:181
void setBoardEvaluation(bScore const s)
Definition board.h:179
bScore m_boardeval
Definition board.h:225
bGameStage m_gamestage
Definition board.h:215
movesequence_t const & getPreviousMoves() const
Definition board.h:186
void setCastleDoneBlack(bool const c)
Definition board.h:184
bool castleDoneBlack() const
Definition board.h:182
movesequence_t const & getVariation() const
Definition board.h:189
friend std::ostream & operator<<(std::ostream &os, bBoard const &dt)
print board
Definition board.cpp:1022
case_t m_capturedcase
Definition board.h:216
uint8_t m_piecesonboard
Definition board.h:212
bScore minimizing() const
Definition board.h:180
void calcGameStage()
calculate pieces
Definition board.cpp:737
bScore getBoardEvaluation() const
Definition board.h:178
uint8_t m_castledoneBlack
Definition board.h:224
~bBoard() override
Definition board.cpp:662
void setPreviousMoves(movesequence_t const &v)
Definition board.h:187
void calcPieces()
Definition board.cpp:672
void setNonSilent()
Definition board.h:171
bool atLeastOneMovePossible() const
see if at least one move can be played e.g.
Definition board.cpp:962
basicmove_t const & getMovePlayed() const
Definition board.h:188
FEN string.
Definition fen.h:14
Definition move.h:69
constexpr bScore SCORE_UNDEFINED
Definition eval.h:20
enum tSide side_t
Definition piece.h:66
enum tPiece piece_t
Definition piece.h:44
plynum_t ply
Definition board.h:19
uint8_t castling
Definition board.h:22
uint64_t u_positionFlags
Definition board.h:15
case_t whiteKing
Definition board.h:17
uint8_t capturedpiece
4 castling bits, plus incheck flag and capture move
Definition board.h:31
case_t blackKing
Definition board.h:18
movenum50_t ply50moves
Definition board.h:21