]> git.sesse.net Git - stockfish/blob - src/piece.h
e0dcc783baa433881814deba72097cd39ab0919a
[stockfish] / src / piece.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008 Marco Costalba
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #if !defined(PIECE_H_INCLUDED)
22 #define PIECE_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "color.h"
29 #include "misc.h"
30 #include "square.h"
31
32
33 ////
34 //// Types
35 ////
36
37 enum PieceType {
38   NO_PIECE_TYPE = 0,
39   PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6
40 };
41
42 enum Piece {
43   NO_PIECE = 0, WP = 1, WN = 2, WB = 3, WR = 4, WQ = 5, WK = 6,
44   BP = 9, BN = 10, BB = 11, BR = 12, BQ = 13, BK = 14,
45   EMPTY = 16, OUTSIDE = 17
46 };
47
48
49 ////
50 //// Constants
51 ////
52
53 const PieceType PieceTypeMin = PAWN;
54 const PieceType PieceTypeMax = KING;
55
56 const int SlidingArray[18] = {
57   0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0
58 };
59
60 const SquareDelta Directions[16][16] = {
61   {DELTA_ZERO},
62   {DELTA_NW, DELTA_NE, DELTA_ZERO},
63   {DELTA_SSW, DELTA_SSE, DELTA_SWW, DELTA_SEE,
64    DELTA_NWW, DELTA_NEE, DELTA_NNW, DELTA_NNE, DELTA_ZERO},
65   {DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
66   {DELTA_S, DELTA_E, DELTA_W, DELTA_N, DELTA_ZERO},
67   {DELTA_S, DELTA_E, DELTA_W, DELTA_N,
68    DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
69   {DELTA_S, DELTA_E, DELTA_W, DELTA_N,
70    DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
71   {DELTA_ZERO},
72   {DELTA_ZERO},
73   {DELTA_SW, DELTA_SE, DELTA_ZERO},
74   {DELTA_SSW, DELTA_SSE, DELTA_SWW, DELTA_SEE,
75    DELTA_NWW, DELTA_NEE, DELTA_NNW, DELTA_NNE, DELTA_ZERO},
76   {DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
77   {DELTA_S, DELTA_E, DELTA_W, DELTA_N, DELTA_ZERO},
78   {DELTA_S, DELTA_E, DELTA_W, DELTA_N,
79    DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
80   {DELTA_S, DELTA_E, DELTA_W, DELTA_N,
81    DELTA_SE, DELTA_SW, DELTA_NE, DELTA_NW, DELTA_ZERO},
82 };
83
84 const SquareDelta PawnPush[2] = {
85   DELTA_N, DELTA_S
86 };
87
88
89 ////
90 //// Inline functions
91 ////
92
93 inline Piece operator+ (Piece p, int i) { return Piece(int(p) + i); }
94 inline void operator++ (Piece &p, int) { p = Piece(int(p) + 1); }
95 inline Piece operator- (Piece p, int i) { return Piece(int(p) - i); }
96 inline void operator-- (Piece &p, int) { p = Piece(int(p) - 1); }
97 inline PieceType operator+ (PieceType p, int i) {return PieceType(int(p) + i);}
98 inline void operator++ (PieceType &p, int) { p = PieceType(int(p) + 1); }
99 inline PieceType operator- (PieceType p, int i) {return PieceType(int(p) - i);}
100 inline void operator-- (PieceType &p, int) { p = PieceType(int(p) - 1); }
101
102 inline PieceType type_of_piece(Piece p)  {
103   return PieceType(int(p) & 7);
104 }
105
106 inline Color color_of_piece(Piece p) {
107   return Color(int(p) >> 3);
108 }
109
110 inline Piece piece_of_color_and_type(Color c, PieceType pt) {
111   return Piece((int(c) << 3) | int(pt));
112 }
113
114 inline Piece pawn_of_color(Color c) {
115   return piece_of_color_and_type(c, PAWN);
116 }
117
118 inline Piece knight_of_color(Color c) {
119   return piece_of_color_and_type(c, KNIGHT);
120 }
121
122 inline Piece bishop_of_color(Color c) {
123   return piece_of_color_and_type(c, BISHOP);
124 }
125
126 inline Piece rook_of_color(Color c) {
127   return piece_of_color_and_type(c, ROOK);
128 }
129
130 inline Piece queen_of_color(Color c) {
131   return piece_of_color_and_type(c, QUEEN);
132 }
133
134 inline Piece king_of_color(Color c) {
135   return piece_of_color_and_type(c, KING);
136 }
137
138 inline int piece_is_slider(Piece p) {
139   return SlidingArray[int(p)];
140 }
141
142 inline int piece_type_is_slider(PieceType pt) {
143   return SlidingArray[int(pt)];
144 }
145
146 inline SquareDelta pawn_push(Color c) {
147   return PawnPush[c];
148 }
149
150 inline bool piece_type_is_ok(PieceType pc) {
151   return pc >= PAWN && pc <= KING;
152 }
153
154 inline bool piece_is_ok(Piece pc) {
155   return piece_type_is_ok(type_of_piece(pc)) && color_is_ok(color_of_piece(pc));
156 }
157
158
159 ////
160 //// Prototypes
161 ////
162
163 extern char piece_type_to_char(PieceType pt, bool upcase = false);
164 extern PieceType piece_type_from_char(char c);
165
166
167 #endif // !defined(PIECE_H_INCLUDED)