Belofte  version 2.1.5
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 
13 
14 //-----------------------------------------------------------------------
15 
16 /** FEN string */
17 class bFen final {
18 public:
19  explicit bFen(std::string const& fs);
20  bFen(bFen&& f);
21  ~bFen();
22 
23  // no copy or move ctor nor assignment defined
24  bFen(bFen const&) = delete;
25  bFen& operator=(bFen const&) = delete;
26  bFen& operator=(bFen&&) = delete;
27 
28  operator std::string() const;
29 
30 private:
31  friend std::ostream& operator<<(std::ostream& os, bFen const& f);
32 
33 private:
34  std::string m_fen;
35 };
36 
37 //-----------------------------------------------------------------------
38 
39 class bBasicBoard {
40 protected:
42  explicit bBasicBoard(bFen const& fen);
43  virtual ~bBasicBoard();
44 
45  bBasicBoard(bBasicBoard const&) = default;
46  bBasicBoard(bBasicBoard&&) = default;
47 
48  // no move ctor nor assignment defined
49  bBasicBoard& operator=(bBasicBoard const&) = delete;
51 
52 public:
53  // access methods
54  int16_t getPly() const { return m_ply; }
55  void setPly(int16_t const p) { m_ply = p; }
56 
57  case_t getEp() const { return m_ep; }
58  void setEp(case_t const e);
59  int16_t getPly50Moves() const { return m_ply50moves; }
60  void setPly50Moves(int16_t const p) { m_ply50moves = p; }
61 
62  bool canCastle(uint8_t const f) const;
63  void setCastle(uint8_t const f, bool const c);
64  void clearCastle(uint8_t const f);
65 
66  // board information methods
67  bool whiteToMove() const { return ((m_ply & 0x0001) == 0); }
68  int16_t getMoveNumber() const { return (m_ply >> 1) + 1; }
70  return static_cast<side_t>((m_ply & 0x0001) + 1); } /// 1 for white, 2 for black
71 
72  // field and piece access methods
73  piece_t getPiece(case_t const bc) const;
74  piece_t getPiece(column_t const iColumn, rank_t const iRank) const;
75  piece_t getPieceCtl(column_t const iColumn, rank_t const iRank) const;
76  void setPiece(case_t const bc, piece_t const piece);
77  void movePiece(case_t const f, case_t const t, piece_t const p);
78  void swapPiece(case_t const t, piece_t const op, piece_t const np);
79  void moveWhiteKing(case_t const f, case_t const t);
80  void moveBlackKing(case_t const f, case_t const t);
81  bool isFieldEmpty(case_t const bc) const;
82  bool isFieldEmpty(column_t const iColumn, rank_t const iRank) const;
83  virtual void setCapture(piece_t const p, case_t const c);
84 
85  bFen getFEN() const;
86  std::string getHashStr() const;
87 
88 protected:
89  void setPieceKU(case_t const bc, piece_t const piece); // and update king position
90  case_t getWhiteKingPos() const { return m_whiteKing; }
91  case_t getBlackKingPos() const { return m_blackKing; }
92  void calcHash(); /// calculate and set hash
93  hashkey_t getHash() const { return m_hash; }
94 
97 
98 private:
99  uint8_t m_castling = 0; // white king, queen, black king, queen castle rights
100  case_t m_whiteKing;
101  case_t m_blackKing;
102  std::array<piece_t, 64> m_fields;
103  int16_t m_ply = 0; // counting from zero for white to move at start position
104  case_t m_ep = tHSpecial::HP_NOEP; // en passant field
105  int16_t m_ply50moves = 0;
106 };
107 
108 //-----------------------------------------------------------------------
109 
110 /** board */
111 class bBoard final : public bBasicBoard {
112 public:
113  bBoard(bBoard&& b) noexcept;
114  explicit bBoard(bBoard const& b, bMove const& m, move_t const& mt);
115  explicit bBoard(bBoard const& b, bMove const& m);
116  explicit bBoard(bFen const& fen);
117  ~bBoard() override;
118 
119  // no move ctor nor assignment defined
120  bBoard(bBoard const&) = delete;
121  bBoard& operator=(bBoard const&) = delete;
122  bBoard& operator=(bBoard&&) = delete;
123 
124  /// do full initialisation
125  void delayed_ctor(bool const& needMaterialUpdate = true);
126 
127  operator std::string() const;
128 
129  bMoveList& getMoves();
134 
135  bool isNonSilent() const { return m_bNonSilent; }
136  void setNonSilent() { m_bNonSilent = true; }
137  bool isCapture() const { return m_capturedpiece; }
138  void setCapture(piece_t const p, case_t const c) override;
139  bool isInCheck() const { return m_bInCheck; }
140  void setInCheck() { m_bInCheck = true; m_bNonSilent = true; }
141  bool isMate() const { return m_bIsMate; }
142  void setMate() { m_bIsMate = true; }
143 
144  void invert();
145  bGameStage getStage() const { return m_gamestage; }
146  bScore getBoardEvaluation() const { return m_boardeval; }
147  void setBoardEvaluation(bScore const s) { m_boardeval = s; }
148  bScore getMaterialEvaluation() const { return m_materialeval; }
149  bScore minimizing() const { return whiteToMove() ? 1 : -1; } /// -1 black to move, 1 white to move
150  bool castleDoneWhite() const { return m_castledoneWhite; }
151  bool castleDoneBlack() const { return m_castledoneBlack; }
152  void setCastleDoneWhite(bool const c) { m_castledoneWhite = c; }
153  void setCastleDoneBlack(bool const c) { m_castledoneBlack = c; }
154 
155  movesequence_t getPreviousMoves() const { return m_previousmoves; }
156  void clearPreviousMoves() { m_previousmoves = {}; }
157  move_t getMovePlayed() const { return m_moveplayed; }
158  movesequence_t getVariation() const { return m_variation; }
159  void clearVariation();
160  void setVariation(bBoard const& chldbrd);
161  uint8_t getPiecesOnBoard() const { return m_piecesonboard; }
162 
163 protected:
164  friend class bGame;
165  friend class bMoveList;
166  friend class bPositionEvaluation;
168 
169 protected:
170  bMoveList* m_moves = nullptr; // moves possible in position
171 
172 private:
173  friend std::ostream& operator<<(std::ostream& os, bBoard const& dt);
174 
175 private:
176  void applyMove(bMove const& m);
177  void applyWhiteMove(bMove const& m);
178  void applyBlackMove(bMove const& m);
179 
180  void calcPieces(); /// calculate pieces
181  void calcMaterial(); /// calculate material score
182  void calcGameStage(); /// calculate gamestage
183 
184  bool m_fullyinitialised = false;
185  move_t m_moveplayed = 0;
186  uint8_t m_piecesonboard = 0;
187 
188  // postbox representation of board
189  std::array<caselist_t ,tStatPiece::STAT_SIZE> m_whitePieces;
190  std::array<caselist_t ,tStatPiece::STAT_SIZE> m_blackPieces;
191 
192  bGameStage m_gamestage = bGameStage::ST_UNKNOWN;
193  uint8_t m_whiteminor = 0;
194  uint8_t m_blackminor = 0;
195 
196  case_t m_capturedcase = 0;
197  bool m_bNonSilent = false; /// used for promotion, capture and in-check
198  bool m_bInCheck = false;
199  bool m_bIsMate = false;
200 
201  bool m_castledoneWhite;
202  bool m_castledoneBlack;
203 
204  bScore m_boardeval = SCORE_UNDEFINED; /// result of board evaluation
205  bScore m_materialeval = SCORE_UNDEFINED; /// materialevaluation
206  movesequence_t m_variation; /// variation leading to score
207  movesequence_t m_previousmoves; /// movesequence to get here
208 };
209 
210 // --------------------------------------------------------------------
211 
212 #endif // defined BOARD_H
213 
214 // eof
unsigned long long hashkey_t
Definition: bel_hash.h:12
@ HP_NOEP
Definition: bel_hash.h:18
uint_fast16_t movenum_t
moveflags (high order word) & basicmove (low order word)
Definition: belofte.h:103
int_fast8_t rank_t
Definition: belofte.h:96
int_fast16_t bScore
used to return id of move in movelist
Definition: belofte.h:104
uint8_t case_t
Definition: belofte.h:98
int_fast8_t column_t
Definition: belofte.h:97
uint32_t move_t
moveflags: bitfield
Definition: belofte.h:101
std::vector< move_t > movesequence_t
Definition: belofte.h:108
tCFlags
Definition: board.h:12
@ BCASTLE_L
Definition: board.h:12
@ BCASTLE_S
Definition: board.h:12
@ CASTLE_SIZE
Definition: board.h:12
@ WCASTLE_S
Definition: board.h:12
@ WCASTLE_L
Definition: board.h:12
bGameStage
Definition: board.h:11
void setPiece(case_t const bc, piece_t const piece)
used for promotion move generation only
Definition: board.cpp:171
bFen getFEN() const
Definition: board.cpp:92
bBasicBoard & operator=(bBasicBoard &&)=delete
bool canCastle(uint8_t const f) const
Definition: board.cpp:194
void setCastle(uint8_t const f, bool const c)
Definition: board.cpp:199
piece_t getPiece(case_t const bc) const
1 for white, 2 for black
Definition: board.cpp:138
int16_t getMoveNumber() const
Definition: board.h:68
hashkey_t m_hash
Definition: board.h:95
bBasicBoard(bBasicBoard const &)=default
bool whiteToMove() const
Definition: board.h:67
hashkey_t getHash() const
calculate and set hash
Definition: board.h:93
void setEp(case_t const e)
Definition: board.cpp:184
void movePiece(case_t const f, case_t const t, piece_t const p)
Definition: board.cpp:241
void moveBlackKing(case_t const f, case_t const t)
Definition: board.cpp:262
side_t getColourToMove() const
Definition: board.h:69
std::string getHashStr() const
Definition: board.cpp:315
void setPieceKU(case_t const bc, piece_t const piece)
used for setboard only
Definition: board.cpp:177
void moveWhiteKing(case_t const f, case_t const t)
Definition: board.cpp:254
case_t getBlackKingPos() const
Definition: board.h:91
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:221
bBasicBoard(bBasicBoard &&)=default
piece_t m_capturedpiece
Definition: board.h:96
int16_t getPly() const
Definition: board.h:54
void setPly(int16_t const p)
Definition: board.h:55
virtual ~bBasicBoard()
Definition: board.cpp:12
case_t getWhiteKingPos() const
Definition: board.h:90
void swapPiece(case_t const t, piece_t const op, piece_t const np)
Definition: board.cpp:248
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:279
case_t getEp() const
Definition: board.h:57
int16_t getPly50Moves() const
Definition: board.h:59
bBasicBoard & operator=(bBasicBoard const &)=delete
void setPly50Moves(int16_t const p)
Definition: board.h:60
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:152
void clearCastle(uint8_t const f)
Definition: board.cpp:208
bool isFieldEmpty(case_t const bc) const
Definition: board.cpp:159
board
Definition: board.h:111
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:556
void setCastleDoneWhite(bool const c)
Definition: board.h:152
bBoard & operator=(bBoard &&)=delete
uint8_t getPiecesOnBoard() const
Definition: board.h:161
movenum_t generateMoves()
generate moves if not yet generated
Definition: board.cpp:710
void clearVariation()
Definition: board.cpp:739
void clearPreviousMoves()
Definition: board.h:156
bBoard(bBoard &&b) noexcept
Push board on gamestack.
Definition: board.cpp:333
bGameStage getStage() const
Definition: board.h:145
bool isCapture() const
Definition: board.h:137
movesequence_t getVariation() const
Definition: board.h:158
move_t getMovePlayed() const
Definition: board.h:157
bool isInCheck() const
Definition: board.h:139
void setVariation(bBoard const &chldbrd)
Definition: board.cpp:744
bool isNonSilent() const
Definition: board.h:135
movesequence_t getPreviousMoves() const
Definition: board.h:155
bool isMate() const
Definition: board.h:141
friend std::ostream & operator<<(std::ostream &os, bBoard const &dt)
print board
Definition: board.cpp:786
bool castleDoneWhite() const
-1 black to move, 1 white to move
Definition: board.h:150
bMoveList * m_moves
Definition: board.h:170
void setBoardEvaluation(bScore const s)
Definition: board.h:147
void setInCheck()
Definition: board.h:140
void setMate()
Definition: board.h:142
void setCastleDoneBlack(bool const c)
Definition: board.h:153
bool castleDoneBlack() const
Definition: board.h:151
movenum_t getNumberOfMoves() const
bMoveList & getMoves()
return moves in position, initialise structure if needed
Definition: board.cpp:731
bScore minimizing() const
Definition: board.h:149
bScore getBoardEvaluation() const
Definition: board.h:146
bScore getMaterialEvaluation() const
Definition: board.h:148
void delayed_ctor(bool const &needMaterialUpdate=true)
do full initialisation
Definition: board.cpp:405
movenum_t generateAtLeastOneQSMove()
Definition: board.cpp:723
movenum_t generateAtLeastOneMove()
see if at least one move can be played e.g.
Definition: board.cpp:718
void invert()
invert board position update kingpos, update colour to move, castle rights, ...
Definition: board.cpp:524
~bBoard() override
Definition: board.cpp:419
bBoard(bBoard const &)=delete
void setNonSilent()
Definition: board.h:136
bBoard & operator=(bBoard const &)=delete
FEN string.
Definition: board.h:17
~bFen()
Definition: board.cpp:806
bFen & operator=(bFen const &)=delete
bFen(std::string const &fs)
Definition: board.cpp:796
bFen & operator=(bFen &&)=delete
bFen(bFen const &)=delete
friend std::ostream & operator<<(std::ostream &os, bFen const &f)
Definition: board.cpp:815
game representation, singleton
Definition: game.h:20
Definition: move.h:164
constexpr bScore SCORE_UNDEFINED
Definition: eval.h:20
enum tSide side_t
Definition: piece.h:70
@ P_EMPTY
Definition: piece.h:38
enum tPiece piece_t
Definition: piece.h:44