99#include " ../Board.h"
1010
1111#include " BishopMap.h"
12+ #include " Move.h"
1213#include " RookMap.h"
1314
1415struct ChessMechanics
@@ -35,7 +36,7 @@ struct ChessMechanics
3536
3637 ChessMechanics () = delete ;
3738
38- explicit ChessMechanics (Board &bd) : _board(bd) {}
39+ explicit ChessMechanics (const Board &bd) : _board(bd) {}
3940
4041 ChessMechanics (ChessMechanics &other) = default ;
4142
@@ -49,12 +50,35 @@ struct ChessMechanics
4950
5051 [[nodiscard]] bool IsCheck () const ;
5152
52- [[nodiscard]] uint64_t GetFullBitMap () const ;
53+ // Gets occupancy maps, which simply indicates whether some field is occupied or not. Does not distinguish colors.
54+ [[nodiscard]] INLINE uint64_t GetFullBitMap () const
55+ {
56+ uint64_t map = 0 ;
57+ for (const auto m : _board.BitBoards ) map |= m;
58+ return map;
59+ }
60+
61+ // Gets occupancy maps, which simply indicates whether some field is occupied or not, by desired color figures.
62+ [[nodiscard]] INLINE uint64_t GetColBitMap (const int col) const
63+ {
64+ TraceIfFalse (col == 1 || col == 0 , " Invalid color!" );
5365
54- [[nodiscard]] uint64_t GetColBitMap (int col) const ;
66+ uint64_t map = 0 ;
67+ for (size_t i = 0 ; i < Board::BitBoardsPerCol; ++i) map |= _board.BitBoards [Board::BitBoardsPerCol * col + i];
68+ return map;
69+ }
5570
5671 // does not check kings BitBoards!!!
57- [[nodiscard]] size_t GetIndexOfContainingBitBoard (uint64_t map, int col) const ;
72+ [[nodiscard]] INLINE size_t GetIndexOfContainingBitBoard (const uint64_t map, const int col) const
73+ {
74+ const size_t colIndex = col * Board::BitBoardsPerCol;
75+ size_t rv = 0 ;
76+ for (size_t i = 0 ; i < Board::BitBoardsPerCol; ++i)
77+ {
78+ rv += ((_board.BitBoards [colIndex + i] & map) != 0 ) * i;
79+ }
80+ return colIndex + rv;
81+ }
5882
5983 // [blockedFigMap, checksCount, checkType]
6084 [[nodiscard]] std::tuple<uint64_t , uint8_t , uint8_t > GetBlockedFieldBitMap (uint64_t fullMap) const ;
@@ -67,15 +91,90 @@ struct ChessMechanics
6791
6892 [[nodiscard]] uint64_t GetAllowedTilesWhenCheckedByNonSliding () const ;
6993
94+ [[nodiscard]] INLINE uint64_t GetLeastValuablePiece (uint64_t pieces, int color, int &pieceIndOut) const
95+ {
96+ const int range = static_cast <int >(kingIndex);
97+ for (int ind = 0 ; ind < range; ++ind)
98+ {
99+ const int colIndex = color * static_cast <int >(Board::BitBoardsPerCol) + ind;
100+ const uint64_t intersection = pieces & _board.BitBoards [colIndex];
101+
102+ if (intersection)
103+ {
104+ pieceIndOut = ind;
105+ return ExtractLsbBit (intersection);
106+ }
107+ }
108+
109+ return 0 ;
110+ }
111+
112+ /*
113+ * SEE - Static Exchange Evaluation - function used to get approximated gain
114+ * after making given move, that is: it performs every exchange on field given by the move
115+ * */
116+ [[nodiscard]] int SEE (Move mv) const ;
117+
118+ /* Function finds index of figure type based on given single bit BitBoard */
119+ static INLINE int FindFigType (const uint64_t BitBoard, const Board &bd)
120+ {
121+ int rv = 0 ;
122+ constexpr int range = static_cast <int >(Board::BitBoardsPerCol);
123+ for (int i = 0 ; i < range; ++i)
124+ {
125+ rv += ((bd.BitBoards [i] & BitBoard) != 0 ) * i;
126+ rv += ((bd.BitBoards [wPawnsIndex + i] & BitBoard) != 0 ) * i;
127+ }
128+ return rv;
129+ }
130+
70131 // ------------------------------
71132 // private methods
72133 // ------------------------------
73134
135+ private:
136+ [[nodiscard]] INLINE uint64_t _updateAttackers (const uint64_t fullMap, const int msbPos) const
137+ {
138+ const uint64_t bishops = (_board.BitBoards [wQueensIndex] | _board.BitBoards [bQueensIndex] |
139+ _board.BitBoards [wBishopsIndex] | _board.BitBoards [bBishopsIndex]) &
140+ fullMap;
141+ const uint64_t rooks = (_board.BitBoards [wQueensIndex] | _board.BitBoards [bQueensIndex] |
142+ _board.BitBoards [wRooksIndex] | _board.BitBoards [bRooksIndex]) &
143+ fullMap;
144+
145+ uint64_t attackers = 0 ;
146+ attackers |= (BishopMap::GetMoves (msbPos, fullMap) & bishops) | (RookMap::GetMoves (msbPos, fullMap) & rooks);
147+
148+ return attackers;
149+ }
150+
151+ struct _seePackage
152+ {
153+ uint64_t attackersBitBoard;
154+ uint64_t fullMap;
155+ uint64_t xrayMap;
156+ };
157+
158+ [[nodiscard]] inline INLINE _seePackage _prepareForSEE (int msbPos) const ;
159+
74160 static std::pair<uint64_t , uint8_t >
75161 _getRookBlockedMap (uint64_t rookMap, uint64_t fullMapWoutKing, uint64_t kingMap);
76162
77163 template <class MoveGeneratorT >
78- [[nodiscard]] static uint64_t _blockIterativeGenerator (uint64_t board, MoveGeneratorT mGen );
164+ [[nodiscard]] INLINE static uint64_t _blockIterativeGenerator (uint64_t board, MoveGeneratorT mGen )
165+ {
166+ uint64_t blockedMap = 0 ;
167+
168+ while (board != 0 )
169+ {
170+ const int figPos = ExtractMsbPos (board);
171+ board ^= (MaxMsbPossible >> figPos);
172+
173+ blockedMap |= mGen (figPos);
174+ }
175+
176+ return blockedMap;
177+ }
79178
80179 // returns [ pinnedFigMap, allowedTilesMap ]
81180 template <class MoveMapT , PinnedFigGen type>
@@ -85,7 +184,8 @@ struct ChessMechanics
85184 // Class fields
86185 // ------------------------------
87186
88- Board &_board;
187+ protected:
188+ const Board &_board;
89189};
90190
91191template <ChessMechanics::PinnedFigGen genType>
@@ -107,21 +207,6 @@ std::pair<uint64_t, uint64_t> ChessMechanics::GetPinnedFigsMap(const int col, co
107207 return {pinnedByBishops | pinnedByRooks, allowedBishops | allowedRooks};
108208}
109209
110- template <class MoveGeneratorT > uint64_t ChessMechanics::_blockIterativeGenerator (uint64_t board, MoveGeneratorT mGen )
111- {
112- uint64_t blockedMap = 0 ;
113-
114- while (board != 0 )
115- {
116- const int figPos = ExtractMsbPos (board);
117- board ^= (MaxMsbPossible >> figPos);
118-
119- blockedMap |= mGen (figPos);
120- }
121-
122- return blockedMap;
123- }
124-
125210template <class MoveMapT , ChessMechanics::PinnedFigGen type>
126211std::pair<uint64_t , uint64_t >
127212ChessMechanics::_getPinnedFigMaps (const uint64_t fullMap, const uint64_t possiblePinningFigs) const
0 commit comments