forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.c
203 lines (116 loc) · 2.92 KB
/
piece.c
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
// piece.c
// includes
#include <string.h>
#include "colour.h"
#include "piece.h"
#include "util.h"
// "constants"
static const uint8 MakePawn[ColourNb] = { PieceNone256, BlackPawn256, WhitePawn256 }; // -BW
static const uint8 PieceFrom12[12] = {
BlackPawn256, WhitePawn256,
BlackKnight256, WhiteKnight256,
BlackBishop256, WhiteBishop256,
BlackRook256, WhiteRook256,
BlackQueen256, WhiteQueen256,
BlackKing256, WhiteKing256,
};
static const char PieceString[12+1] = "pPnNbBrRqQkK";
// variables
static sint8 PieceTo12[256];
// functions
// piece_init()
void piece_init() {
int piece;
for (piece = 0; piece < 256; piece++) PieceTo12[piece] = -1;
for (piece = 0; piece < 12; piece++) {
PieceTo12[PieceFrom12[piece]] = piece;
}
}
// piece_is_ok()
bool piece_is_ok(int piece) {
if (piece < 0 || piece >= 256) return FALSE;
if (PieceTo12[piece] < 0) return FALSE;
return TRUE;
}
// piece_make_pawn()
int piece_make_pawn(int colour) {
ASSERT(colour_is_ok(colour));
return MakePawn[colour];
}
// piece_pawn_opp()
int piece_pawn_opp(int piece) {
ASSERT(piece==BlackPawn256||piece==WhitePawn256);
return piece ^ 15;
}
// piece_colour()
int piece_colour(int piece) {
ASSERT(piece_is_ok(piece));
return piece & 3;
}
// piece_type()
int piece_type(int piece) {
ASSERT(piece_is_ok(piece));
return piece & ~3;
}
// piece_is_pawn()
bool piece_is_pawn(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & PawnFlags) != 0;
}
// piece_is_knight()
bool piece_is_knight(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & KnightFlag) != 0;
}
// piece_is_bishop()
bool piece_is_bishop(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & QueenFlags) == BishopFlag;
}
// piece_is_rook()
bool piece_is_rook(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & QueenFlags) == RookFlag;
}
// piece_is_queen()
bool piece_is_queen(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & QueenFlags) == QueenFlags;
}
// piece_is_king()
bool piece_is_king(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & KingFlag) != 0;
}
// piece_is_slider()
bool piece_is_slider(int piece) {
ASSERT(piece_is_ok(piece));
return (piece & QueenFlags) != 0;
}
// piece_to_12()
int piece_to_12(int piece) {
ASSERT(piece_is_ok(piece));
return PieceTo12[piece];
}
// piece_from_12()
int piece_from_12(int piece) {
ASSERT(piece>=0&&piece<12);
return PieceFrom12[piece];
}
// piece_to_char()
int piece_to_char(int piece) {
ASSERT(piece_is_ok(piece));
return PieceString[piece_to_12(piece)];
}
// piece_from_char()
int piece_from_char(int c) {
const char * ptr;
ptr = strchr(PieceString,c);
if (ptr == NULL) return PieceNone256;
return piece_from_12(ptr-PieceString);
}
// char_is_piece()
bool char_is_piece(int c) {
return strchr("PNBRQK",c) != NULL;
}
// end of piece.cpp