TranspositionTable: spaces inflate
[glaurung_clone.git] / src / square.h
blob3afa127aaf14aac7bbd600dd7f81e59d89aa02c1
1 /*
2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #if !defined(SQUARE_H_INCLUDED)
21 #define SQUARE_H_INCLUDED
23 ////
24 //// Includes
25 ////
27 #include <string>
29 #include "color.h"
30 #include "misc.h"
33 ////
34 //// Types
35 ////
37 enum Square {
38 SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
39 SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
40 SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
41 SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
42 SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
43 SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
44 SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
45 SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
46 SQ_NONE
47 };
49 enum File {
50 FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NONE
53 enum Rank {
54 RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NONE
57 enum SquareDelta {
58 DELTA_SSW = -021, DELTA_SS = -020, DELTA_SSE = -017, DELTA_SWW = -012,
59 DELTA_SW = -011, DELTA_S = -010, DELTA_SE = -07, DELTA_SEE = -06,
60 DELTA_W = -01, DELTA_ZERO = 0, DELTA_E = 01, DELTA_NWW = 06, DELTA_NW = 07,
61 DELTA_N = 010, DELTA_NE = 011, DELTA_NEE = 012, DELTA_NNW = 017,
62 DELTA_NN = 020, DELTA_NNE = 021
66 ////
67 //// Constants
68 ////
70 const int FlipMask = 070;
71 const int FlopMask = 07;
74 ////
75 //// Inline functions
76 ////
78 inline File operator+ (File x, int i) { return File(int(x) + i); }
79 inline File operator+ (File x, File y) { return x + int(y); }
80 inline void operator++ (File &x, int) { x = File(int(x) + 1); }
81 inline void operator+= (File &x, int i) { x = File(int(x) + i); }
82 inline File operator- (File x, int i) { return File(int(x) - i); }
83 inline void operator-- (File &x, int) { x = File(int(x) - 1); }
84 inline void operator-= (File &x, int i) { x = File(int(x) - i); }
86 inline Rank operator+ (Rank x, int i) { return Rank(int(x) + i); }
87 inline Rank operator+ (Rank x, Rank y) { return x + int(y); }
88 inline void operator++ (Rank &x, int) { x = Rank(int(x) + 1); }
89 inline void operator+= (Rank &x, int i) { x = Rank(int(x) + i); }
90 inline Rank operator- (Rank x, int i) { return Rank(int(x) - i); }
91 inline void operator-- (Rank &x, int) { x = Rank(int(x) - 1); }
92 inline void operator-= (Rank &x, int i) { x = Rank(int(x) - i); }
94 inline Square operator+ (Square x, int i) { return Square(int(x) + i); }
95 inline void operator++ (Square &x, int) { x = Square(int(x) + 1); }
96 inline void operator+= (Square &x, int i) { x = Square(int(x) + i); }
97 inline Square operator- (Square x, int i) { return Square(int(x) - i); }
98 inline void operator-- (Square &x, int) { x = Square(int(x) - 1); }
99 inline void operator-= (Square &x, int i) { x = Square(int(x) - i); }
100 inline Square operator+ (Square x, SquareDelta i) { return Square(int(x) + i); }
101 inline void operator+= (Square &x, SquareDelta i) { x = Square(int(x) + i); }
102 inline Square operator- (Square x, SquareDelta i) { return Square(int(x) - i); }
103 inline void operator-= (Square &x, SquareDelta i) { x = Square(int(x) - i); }
104 inline SquareDelta operator- (Square x, Square y) {
105 return SquareDelta(int(x) - int(y));
108 inline Square make_square(File f, Rank r) {
109 return Square(int(f) | (int(r) << 3));
112 inline File square_file(Square s) {
113 return File(int(s) & 7);
116 inline Rank square_rank(Square s) {
117 return Rank(int(s) >> 3);
120 inline Square flip_square(Square s) {
121 return Square(int(s) ^ FlipMask);
124 inline Square flop_square(Square s) {
125 return Square(int(s) ^ FlopMask);
128 inline Square relative_square(Color c, Square s) {
129 return Square(int(s) ^ (int(c) * FlipMask));
132 inline Rank pawn_rank(Color c, Square s) {
133 return square_rank(relative_square(c, s));
136 inline Color square_color(Square s) {
137 return Color((int(square_file(s)) + int(square_rank(s))) & 1);
140 inline int file_distance(File f1, File f2) {
141 return abs(int(f1) - int(f2));
144 inline int file_distance(Square s1, Square s2) {
145 return file_distance(square_file(s1), square_file(s2));
148 inline int rank_distance(Rank r1, Rank r2) {
149 return abs(int(r1) - int(r2));
152 inline int rank_distance(Square s1, Square s2) {
153 return rank_distance(square_rank(s1), square_rank(s2));
156 inline int square_distance(Square s1, Square s2) {
157 return Max(file_distance(s1, s2), rank_distance(s1, s2));
161 ////
162 //// Prototypes
163 ////
165 extern File file_from_char(char c);
166 extern char file_to_char(File f);
167 extern Rank rank_from_char(char c);
168 extern char rank_to_char(Rank r);
169 extern Square square_from_string(const std::string &str);
170 extern const std::string square_to_string(Square s);
172 extern bool file_is_ok(File f);
173 extern bool rank_is_ok(Rank r);
174 extern bool square_is_ok(Square s);
177 #endif // !defined(SQUARE_H_INCLUDED)