Add Game::is_dangerous() for Futility Pruning
[purplehaze.git] / src / common.h
blob4ffbd300f15d50b55202ea6147b14dbc94b5cbca
1 /* Copyright (C) 2007-2011 Vincent Ollivier
3 * Purple Haze is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Purple Haze is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef COMMON_H
18 #define COMMON_H
20 #include <string>
22 #ifndef VERSION
23 #define VERSION "2.0.2"
24 #endif
26 static const int BOARD_SIZE = 128;
27 static const int MAX_PLY = 128; // Maximum search depth
28 static const int MAX_BF = 256; // Maximum number of moves per position
29 static const int INF = 29999;
30 static const int TT_SIZE = 1024 * 1024 * 128;
31 static const int MT_SIZE = 1024 * 1024;
32 static const int MAX_KILLERS = 2;
34 static const std::string DEFAULT_FEN =
35 "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
37 enum Square : unsigned char {
38 A1=0x00, B1, C1, D1, E1, F1, G1, H1,
39 A2=0x10, B2, C2, D2, E2, F2, G2, H2,
40 A3=0x20, B3, C3, D3, E3, F3, G3, H3,
41 A4=0x30, B4, C4, D4, E4, F4, G4, H4,
42 A5=0x40, B5, C5, D5, E5, F5, G5, H5,
43 A6=0x50, B6, C6, D6, E6, F6, G6, H6,
44 A7=0x60, B7, C7, D7, E7, F7, G7, H7,
45 A8=0x70, B8, C8, D8, E8, F8, G8, H8,
46 OUT
49 static const Square SQUARES[] = {
50 A1, C1, D1, E1, F1, G1, H1,
51 A2, C2, D2, E2, F2, G2, H2,
52 A3, C3, D3, E3, F3, G3, H3,
53 A4, C4, D4, E4, F4, G4, H4,
54 A5, C5, D5, E5, F5, G5, H5,
55 A6, C6, D6, E6, F6, G6, H6,
56 A7, C7, D7, E7, F7, G7, H7,
57 A8, C8, D8, E8, F8, G8, H8,
58 OUT
61 enum File : unsigned char {
62 FILE_A,
63 FILE_B,
64 FILE_C,
65 FILE_D,
66 FILE_E,
67 FILE_F,
68 FILE_G,
69 FILE_H
72 enum Rank : unsigned char {
73 RANK_1,
74 RANK_2,
75 RANK_3,
76 RANK_4,
77 RANK_5,
78 RANK_6,
79 RANK_7,
80 RANK_8
83 enum Direction {
84 NO_DIR = 0,
85 UP = 0x10,
86 RIGHT = 0x01,
87 DOWN = -UP,
88 LEFT = -RIGHT,
89 UP_RIGHT = UP + RIGHT,
90 DOWN_RIGHT = DOWN + RIGHT,
91 DOWN_LEFT = DOWN + LEFT,
92 UP_LEFT = UP + LEFT,
93 UP_UP_RIGHT = UP + UP_RIGHT,
94 RIGHT_UP_RIGHT = RIGHT + UP_RIGHT,
95 RIGHT_DOWN_RIGHT = RIGHT + DOWN_RIGHT,
96 DOWN_DOWN_RIGHT = DOWN + DOWN_RIGHT,
97 DOWN_DOWN_LEFT = DOWN + DOWN_LEFT,
98 LEFT_DOWN_LEFT = LEFT + DOWN_LEFT,
99 LEFT_UP_LEFT = LEFT + UP_LEFT,
100 UP_UP_LEFT = UP + UP_LEFT
103 #ifdef __INTEL_COMPILER
104 enum Color : unsigned char {
105 #else
106 enum Color : bool {
107 #endif
108 WHITE,
109 BLACK
111 static const Color COLORS[] = { WHITE, BLACK };
112 inline Color operator!(Color c)
114 return static_cast<Color>(!static_cast<bool>(c));
117 enum PieceType : unsigned char {
118 EMPTY,
119 PAWN,
120 KNIGHT,
121 BISHOP,
122 ROOK,
123 QUEEN,
124 KING
126 static const int NB_PIECE_TYPES = static_cast<int>(KING) + 1;
128 static const PieceType PIECE_TYPES[] = {
129 PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING
131 static const PieceType NOT_PAWN_TYPES[] = {
132 KNIGHT, BISHOP, ROOK, QUEEN, KING
134 static const PieceType SIDE_TYPES[] = {
135 QUEEN, KING
137 static const PieceType MINOR_TYPES[] = {
138 KNIGHT, BISHOP
140 static const PieceType MAJOR_TYPES[] = {
141 ROOK, QUEEN
144 enum MoveType : unsigned char {
145 QUIET_MOVE,
146 DOUBLE_PAWN_PUSH,
147 KING_CASTLE,
148 QUEEN_CASTLE,
149 CAPTURE,
150 EN_PASSANT,
151 NULL_MOVE, // Warning: is_capture() will return true
152 KNIGHT_PROMOTION = 8, // is_promotion() { MoveType[3] }
153 BISHOP_PROMOTION,
154 ROOK_PROMOTION,
155 QUEEN_PROMOTION,
156 KNIGHT_PROMOTION_CAPTURE,
157 BISHOP_PROMOTION_CAPTURE,
158 ROOK_PROMOTION_CAPTURE,
159 QUEEN_PROMOTION_CAPTURE
162 // Used in movegen.cpp and attack.cpp
163 static const int NB_DIRS[] = { 0, 0, 8, 4, 4, 8, 8 };
164 static const Direction PIECES_DIRS[][8] = {
165 { // Empty
166 NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR
168 { // Pawns
169 NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR, NO_DIR
171 { // Knights
172 UP_UP_RIGHT, RIGHT_UP_RIGHT, RIGHT_DOWN_RIGHT, DOWN_DOWN_RIGHT,
173 DOWN_DOWN_LEFT, LEFT_DOWN_LEFT, LEFT_UP_LEFT, UP_UP_LEFT
175 { // Bishops
176 UP_RIGHT, DOWN_RIGHT, DOWN_LEFT, UP_LEFT,
177 NO_DIR, NO_DIR, NO_DIR, NO_DIR
179 { // Rooks
180 UP, RIGHT, DOWN, LEFT, NO_DIR, NO_DIR, NO_DIR, NO_DIR
182 { // Queens
183 UP, UP_RIGHT, RIGHT, DOWN_RIGHT, DOWN, DOWN_LEFT, LEFT, UP_LEFT
185 { // Kings
186 UP, UP_RIGHT, RIGHT, DOWN_RIGHT, DOWN, DOWN_LEFT, LEFT, UP_LEFT
190 // Used in movegen.cpp and protocol.cpp
191 static const Direction PAWN_PUSH_DIRS[] = { UP, DOWN };
192 static const Direction PAWN_CAPTURE_DIRS[][2] = {
194 UP_LEFT, UP_RIGHT
197 DOWN_LEFT, DOWN_RIGHT
201 // Used in tt.h
202 enum Bound : unsigned char { EXACT, LOWER, UPPER, UNDEF_BOUND };
204 // Used for debugging
205 #define assert_msg(x) !(std::cerr << "Assertion failed: " << x << std::endl)
207 // Overload operators to solve ambiguous errors with Clang < 3.1
208 // TODO Find out if this could be better done
209 inline bool operator==(int i, Rank r)
211 return i == static_cast<int>(r);
213 inline bool operator<(Bound b, int i)
215 return static_cast<char>(b) < i;
217 inline int operator>>(Square s, int i)
219 return static_cast<int>(s) >> i;
221 inline int operator|(int i, MoveType mt)
223 return i | static_cast<int>(mt);
225 inline int operator&(Square s, int i)
227 return static_cast<int>(s) & i;
229 inline int operator*(Rank r, Color c)
231 return static_cast<int>(r) * static_cast<int>(c);
233 inline int operator*(int i, Rank r)
235 return i * static_cast<int>(r);
237 inline int operator*(int i, Color c)
239 return i * static_cast<int>(c);
241 inline int operator*(int i, PieceType pt)
243 return i * static_cast<int>(pt);
245 inline int operator+(int i, File f)
247 return i + static_cast<int>(f);
249 inline int operator+(int i, Square s)
251 return i + static_cast<int>(s);
253 inline int operator+(int i, PieceType pt)
255 return i + static_cast<int>(pt);
257 inline int operator+(PieceType pt, int i)
259 return i + static_cast<int>(pt);
261 inline int operator+(Square s, int i)
263 return i + static_cast<int>(s);
265 inline int operator+(Rank r, int i)
267 return static_cast<int>(r) + i;
269 inline int operator+(char c, Rank r)
271 return c + static_cast<char>(r);
273 inline int operator+(Square s, Direction d)
275 return static_cast<int>(s) + static_cast<int>(d);
277 inline int operator-(Square a, Square b)
279 return static_cast<int>(a) - static_cast<int>(b);
281 inline int operator-(Square s, int i)
283 return static_cast<int>(s) - i;
285 inline int operator-(int i, Square s)
287 return i - static_cast<int>(s);
289 inline int operator-(int i, PieceType pt)
291 return i - static_cast<int>(pt);
293 inline int operator-(Rank a, Rank b)
295 return static_cast<int>(a) - static_cast<int>(b);
297 inline int operator-(Rank r, int i)
299 return static_cast<int>(r) - i;
301 inline int operator+(MoveType a, MoveType b)
303 return static_cast<MoveType>(static_cast<int>(a) + static_cast<int>(b));
306 // ANSI Color
307 static const std::string color_red = "\x1b[31m";
308 static const std::string color_green = "\x1b[32m";
309 static const std::string color_reset = "\x1b[0m";
311 #endif /* !COMMON_H */