Add forgotten files 1.4.70b
[polyglot.git] / square.c
1
2 // square.c
3
4 // includes
5
6 #include "colour.h"
7 #include "square.h"
8 #include "util.h"
9
10 // "constants"
11
12 static const uint8 SquareFrom64[64] = {
13    A1, B1, C1, D1, E1, F1, G1, H1,
14    A2, B2, C2, D2, E2, F2, G2, H2,
15    A3, B3, C3, D3, E3, F3, G3, H3,
16    A4, B4, C4, D4, E4, F4, G4, H4,
17    A5, B5, C5, D5, E5, F5, G5, H5,
18    A6, B6, C6, D6, E6, F6, G6, H6,
19    A7, B7, C7, D7, E7, F7, G7, H7,
20    A8, B8, C8, D8, E8, F8, G8, H8,
21 };
22
23 // variables
24
25 static sint8 SquareTo64[SquareNb];
26
27 // functions
28
29 // square_init()
30
31 void square_init() {
32
33    int sq;
34
35    for (sq = 0; sq < SquareNb; sq++) SquareTo64[sq] = -1;
36
37    for (sq = 0; sq < 64; sq++) {
38       SquareTo64[SquareFrom64[sq]] = sq;
39    }
40 }
41
42 // square_is_ok()
43
44 bool square_is_ok(int square) {
45
46    if (square < 0 || square >= SquareNb) return FALSE;
47
48    if (SquareTo64[square] < 0) return FALSE;
49
50    return TRUE;
51 }
52
53 // square_make()
54
55 int square_make(int file, int rank) {
56
57    int sq_64;
58
59    ASSERT(file>=0&&file<8);
60    ASSERT(rank>=0&&rank<8);
61
62    sq_64 = (rank << 3) | file;
63
64    return square_from_64(sq_64);
65 }
66
67 // square_file()
68
69 int square_file(int square) {
70
71    int file;
72
73    ASSERT(square_is_ok(square));
74
75    file = (square - 4) & 7;
76    ASSERT(file==(square_to_64(square)&7));
77
78    return file;
79 }
80
81 // square_rank()
82
83 int square_rank(int square) {
84
85    int rank;
86
87    ASSERT(square_is_ok(square));
88
89    rank = (square >> 4) - 2;
90    ASSERT(rank==square_to_64(square)>>3);
91
92    return rank;
93 }
94
95 // square_side_rank()
96
97 int square_side_rank(int square, int colour) {
98
99    int rank;
100
101    ASSERT(square_is_ok(square));
102    ASSERT(colour_is_ok(colour));
103
104    rank = square_rank(square);
105    if (colour_is_black(colour)) rank = 7-rank;
106
107    return rank;
108 }
109
110 // square_from_64()
111
112 int square_from_64(int square) {
113
114    ASSERT(square>=0&&square<64);
115
116    return SquareFrom64[square];
117 }
118
119 // square_to_64()
120
121 int square_to_64(int square) {
122
123    ASSERT(square_is_ok(square));
124
125    return SquareTo64[square];
126 }
127
128 // square_is_promote()
129
130 bool square_is_promote(int square) {
131
132    int rank;
133
134    ASSERT(square_is_ok(square));
135
136    rank = square_rank(square);
137
138    return rank == Rank1 || rank == Rank8;
139 }
140
141 // square_ep_dual()
142
143 int square_ep_dual(int square) {
144
145    ASSERT(square_is_ok(square));
146    ASSERT(square_rank(square)>=2&&square_rank(square)<=5);
147
148    return square ^ 16;
149 }
150
151 // square_colour()
152
153 int square_colour(int square) {
154
155    ASSERT(square_is_ok(square));
156
157    return (square ^ (square >> 4)) & 1;
158 }
159
160 // file_from_char()
161
162 int file_from_char(int c) {
163
164    ASSERT(c>='a'&&c<='h');
165
166    return c - 'a';
167 }
168
169 // rank_from_char()
170
171 int rank_from_char(int c) {
172
173    ASSERT(c>='1'&&c<='8');
174
175    return c - '1';
176 }
177
178 // file_to_char()
179
180 int file_to_char(int file) {
181
182    ASSERT(file>=0&&file<8);
183
184    return 'a' + file;
185 }
186
187 // rank_to_char()
188
189 int rank_to_char(int rank) {
190
191    ASSERT(rank>=0&&rank<8);
192
193    return '1' + rank;
194 }
195
196 // char_is_file()
197
198 bool char_is_file(int c) {
199
200    return c >= 'a' && c <= 'h';
201 }
202
203 // char_is_rank()
204
205 bool char_is_rank(int c) {
206
207    return c >= '1' && c <= '8';
208 }
209
210 // square_to_string()
211
212 bool square_to_string(int square, char string[], int size) {
213
214    ASSERT(square_is_ok(square));
215    ASSERT(string!=NULL);
216    ASSERT(size>=3);
217
218    if (size < 3) return FALSE;
219
220    string[0] = 'a' + square_file(square);
221    string[1] = '1' + square_rank(square);
222    string[2] = '\0';
223
224    return TRUE;
225 }
226
227 // square_from_string()
228
229 int square_from_string(const char string[]) {
230
231    int file, rank;
232
233    ASSERT(string!=NULL);
234
235    if (string[0] < 'a' || string[0] > 'h') return SquareNone;
236    if (string[1] < '1' || string[1] > '8') return SquareNone;
237    if (string[2] != '\0') return SquareNone;
238
239    file = file_from_char(string[0]);
240    rank = rank_from_char(string[1]);
241
242    return square_make(file,rank);
243 }
244
245 // end of square.cpp
246