-
Notifications
You must be signed in to change notification settings - Fork 0
/
position.hpp
223 lines (175 loc) · 5.76 KB
/
position.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// position.hpp
// Chess Engine
//
// Created by Blake Johnson on 12/12/18.
// Copyright © 2018 Blake Johnson. All rights reserved.
//
#ifndef position_hpp
#define position_hpp
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include "bitboard.hpp"
#include "evaluate.hpp"
#include "types.hpp"
struct returnState {
int fiftyMove;
square enPassent;
int castling;
piece captured;
move m;
returnState* prev;
bitboard absolutePinned [numOfPlayers];
bitboard pinners [numOfPlayers];
value material[numOfPlayers];
returnState () {
castling = noRights; //Always take from the fen
enPassent = empty;
fiftyMove = 0;
captured = not_piece;
m = none;
prev = NULL;
absolutePinned[white] = 0;
absolutePinned[black] = 0;
pinners[white] = 0;
pinners[black] = 0;
material[white] = draw;
material [black] = draw;
};
};
class position {
private:
piece board[numOfSq]; //a board listing every piece at every square for printing purposes
bitboard pieces[numOfPieces]; //bitboards for each piece, black and white. mulitple pieces of a type will be in them
bitboard allWhitePieces;
bitboard allBlackPieces;
bitboard allPieces;
int ply;
player sideToPlay;
bitboard nodes;
returnState* state;
public:
position();
void updatePositionFen (std::string fen);
void reset();
void setboard();
void fenParser(std::string& fen);
//just for debugging
move bestMove;
//bitboards and squares
bitboard get_pieces () const ;
bitboard get_pieces (player color) const;
bitboard get_pieces (piece pt) const;
bitboard get_pieces (pieceType pt) const;
bitboard get_pieces (pieceType pt, pieceType pt2) const;
bitboard get_pieces (player color, pieceType pt) const;
bitboard get_king (player color) const;
bitboard get_pinned (player color) const;
square get_king_sq (player color) const;
square get_enPassent () const;
//move related function
void do_move(move m, returnState& newState);
void undo_move(move m);
void place_piece(square sq, piece pt);
void remove_piece(square sq, piece pt);
//state related functions
player get_sideToPlay() const;
value get_material (player color) const;
void update_check_boards(returnState* state);
void update_material(returnState* state);
//castling
int get_castlingRights () const;
int get_castlingRights (player color) const;
//attacks and checks
bitboard squareAttackedBy (square sq) const;
bitboard attacksFrom (square sq, pieceType pt) const;
bitboard attacksFromPawn (square sq, player color) const;
bitboard xRayRook (bitboard occupied, bitboard ownPieces, square sq) const;
bitboard xRayBishop (bitboard occupied, bitboard ownPieces, square sq) const;
bitboard pinningBoards (square sq, bitboard& pinners, player color);
//moves
bool legal(move m) const;
bitboard get_nodes() const;
void inc_nodes();
void reset_nodes();
int get_ply() const;
int numberOfPieces () const;
returnState* get_returnState() const;
void printAllBitboards() const;
void printBoard() const;
};
inline bitboard position::get_pieces() const{
return allPieces;
}
inline bitboard position::get_pieces(player color) const{
return color == white ? allWhitePieces : allBlackPieces;
}
inline bitboard position::get_pieces(piece pt) const{
return pieces[pt];
}
inline bitboard position::get_pieces(pieceType pt) const{
return pieces[pt] | pieces[pt + numOfPieceType] ;
}
inline bitboard position::get_pieces(pieceType pt, pieceType pt2) const{
return pieces[pt] | pieces[pt + numOfPieceType] | pieces[pt2] | pieces[pt2 + numOfPieceType] ;
}
inline bitboard position::get_pieces(player color, pieceType pt) const{
return color == white ? pieces[pt] : pieces[pt + numOfPieceType];
}
inline bitboard position::get_king(player color) const {
return color == white ? pieces[w_king] : pieces[b_king];
}
inline bitboard position::get_pinned(player color) const {
return state -> absolutePinned[color];
}
inline square position::get_king_sq(player color) const
{
return color == white ? lsb_sq(pieces[w_king])
: lsb_sq(pieces[b_king]);
}
inline square position::get_enPassent() const {
return state->enPassent;
}
inline player position::get_sideToPlay() const{
return sideToPlay;
}
inline value position::get_material(player color) const{
return state->material[color];
}
inline int position::get_castlingRights() const{
return state-> castling;
}
inline int position::get_castlingRights(player color) const{
return color == white ? (state-> castling) & 0x3 : (state-> castling) & 0xC ;
}
inline bitboard position::attacksFrom (square sq, pieceType pt) const {
bitboard attacks = pt == p_pawn ? attacksFromPawn(sq, sideToPlay) :
pt == p_rook ? sliderAttacks(sq, allPieces, p_rook) :
pt == p_bishop ? sliderAttacks(sq, allPieces, p_bishop) :
pt == p_queen ? sliderAttacks(sq, allPieces, p_rook) | sliderAttacks(sq, allPieces, p_bishop) : openBoardAttacks[pt][sq];
return attacks;
}
inline bitboard position::attacksFromPawn(square sq, player color) const{
return openBoardPawnAttacks[color][sq];
}
inline bitboard position::get_nodes() const {
return nodes;
}
inline void position::inc_nodes() {
nodes++;
}
inline void position::reset_nodes(){
nodes = 0;
}
inline int position::get_ply() const{
return ply;
}
inline int position::numberOfPieces()const{
return popcount(allPieces);
}
inline returnState* position::get_returnState()const {
return state;
}
#endif /* position_hpp */