forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare.c
246 lines (145 loc) · 3.64 KB
/
square.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// square.c
// includes
#include "colour.h"
#include "square.h"
#include "util.h"
// "constants"
static const uint8 SquareFrom64[64] = {
A1, B1, C1, D1, E1, F1, G1, H1,
A2, B2, C2, D2, E2, F2, G2, H2,
A3, B3, C3, D3, E3, F3, G3, H3,
A4, B4, C4, D4, E4, F4, G4, H4,
A5, B5, C5, D5, E5, F5, G5, H5,
A6, B6, C6, D6, E6, F6, G6, H6,
A7, B7, C7, D7, E7, F7, G7, H7,
A8, B8, C8, D8, E8, F8, G8, H8,
};
// variables
static sint8 SquareTo64[SquareNb];
// functions
// square_init()
void square_init() {
int sq;
for (sq = 0; sq < SquareNb; sq++) SquareTo64[sq] = -1;
for (sq = 0; sq < 64; sq++) {
SquareTo64[SquareFrom64[sq]] = sq;
}
}
// square_is_ok()
bool square_is_ok(int square) {
if (square < 0 || square >= SquareNb) return FALSE;
if (SquareTo64[square] < 0) return FALSE;
return TRUE;
}
// square_make()
int square_make(int file, int rank) {
int sq_64;
ASSERT(file>=0&&file<8);
ASSERT(rank>=0&&rank<8);
sq_64 = (rank << 3) | file;
return square_from_64(sq_64);
}
// square_file()
int square_file(int square) {
int file;
ASSERT(square_is_ok(square));
file = (square - 4) & 7;
ASSERT(file==(square_to_64(square)&7));
return file;
}
// square_rank()
int square_rank(int square) {
int rank;
ASSERT(square_is_ok(square));
rank = (square >> 4) - 2;
ASSERT(rank==square_to_64(square)>>3);
return rank;
}
// square_side_rank()
int square_side_rank(int square, int colour) {
int rank;
ASSERT(square_is_ok(square));
ASSERT(colour_is_ok(colour));
rank = square_rank(square);
if (colour_is_black(colour)) rank = 7-rank;
return rank;
}
// square_from_64()
int square_from_64(int square) {
ASSERT(square>=0&&square<64);
return SquareFrom64[square];
}
// square_to_64()
int square_to_64(int square) {
ASSERT(square_is_ok(square));
return SquareTo64[square];
}
// square_is_promote()
bool square_is_promote(int square) {
int rank;
ASSERT(square_is_ok(square));
rank = square_rank(square);
return rank == Rank1 || rank == Rank8;
}
// square_ep_dual()
int square_ep_dual(int square) {
ASSERT(square_is_ok(square));
ASSERT(square_rank(square)>=2&&square_rank(square)<=5);
return square ^ 16;
}
// square_colour()
int square_colour(int square) {
ASSERT(square_is_ok(square));
return (square ^ (square >> 4)) & 1;
}
// file_from_char()
int file_from_char(int c) {
ASSERT(c>='a'&&c<='h');
return c - 'a';
}
// rank_from_char()
int rank_from_char(int c) {
ASSERT(c>='1'&&c<='8');
return c - '1';
}
// file_to_char()
int file_to_char(int file) {
ASSERT(file>=0&&file<8);
return 'a' + file;
}
// rank_to_char()
int rank_to_char(int rank) {
ASSERT(rank>=0&&rank<8);
return '1' + rank;
}
// char_is_file()
bool char_is_file(int c) {
return c >= 'a' && c <= 'h';
}
// char_is_rank()
bool char_is_rank(int c) {
return c >= '1' && c <= '8';
}
// square_to_string()
bool square_to_string(int square, char string[], int size) {
ASSERT(square_is_ok(square));
ASSERT(string!=NULL);
ASSERT(size>=3);
if (size < 3) return FALSE;
string[0] = 'a' + square_file(square);
string[1] = '1' + square_rank(square);
string[2] = '\0';
return TRUE;
}
// square_from_string()
int square_from_string(const char string[]) {
int file, rank;
ASSERT(string!=NULL);
if (string[0] < 'a' || string[0] > 'h') return SquareNone;
if (string[1] < '1' || string[1] > '8') return SquareNone;
if (string[2] != '\0') return SquareNone;
file = file_from_char(string[0]);
rank = rank_from_char(string[1]);
return square_make(file,rank);
}
// end of square.cpp