-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
263 lines (216 loc) · 7.02 KB
/
board.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "board.h"
#include "move_validation.h"
// This is the base string from which begin all the pieces; This is the black king
static const char *BASE = "\xe2\x99\x94";
// Here supplying the Player Color as index will provide you with the correct color scheme to use
static const char *POINTER_COLOR[] = {
"\033[32m\033[40m",
"\033[32m\033[44m"
};
static const char *SELECTION_COLOR[] = {
"\033[35m\033[40m",
"\033[35m\033[44m"
};
static const char *OP_POINTER_COLOR[] = {
"\033[33m\033[40m",
"\033[33m\033[44m"
};
static const char *OP_SELECTION_COLOR[] = {
"\033[31m\033[40m",
"\033[31m\033[44m"
};
static const char *BACKGROUND_COLOR[] = {
"\033[37m\033[40m",
"\033[37m\033[44m"
};
bool (*is_valid_move[13]) (const Move *, const Move *) = {
is_valid_move_king_black,
is_valid_move_queen,
is_valid_move_rook,
is_valid_move_bishop,
is_valid_move_knight,
is_valid_move_pawn_black,
is_valid_move_king_white,
is_valid_move_queen,
is_valid_move_rook,
is_valid_move_bishop,
is_valid_move_knight,
is_valid_move_pawn_white,
is_valid_move_empty,
};
Piece board[BOARD_SIZE][BOARD_SIZE];
Move *move_history;
int move_counter;
Piece pieceat(const Pos *p) {
return board[p->i][p->j];
}
void setpiece(const Pos *pos, const Piece p) {
board[pos->i][pos->j] = p;
}
void setpiece_index(int i, int j, const Piece p) {
board[i][j] = p;
}
Piece pieceatindex(const int i, const int j) {
return board[i][j];
}
int get_color(const Piece piece) {
if (piece >= KING_B && piece <= PAWN_B) return BLACK;
if (piece >= KING_W && piece <= PAWN_W) return WHITE;
return -1; // This is for when the "Piece" is an empty square
}
bool has_moved(const Pos p) { // Returns true if p is contained anywhere in the move_history ring buffer
Move *mp;
int size = move_counter > HISTSIZE ? HISTSIZE : move_counter;
for (mp = move_history; mp != move_history + size; mp++) {
if ((p.i == mp->dst.i && p.j == mp->dst.j) || (p.i == mp->src.i && p.j == mp->src.j)) return true;
// printf("p.i = %d, mp->dst.i = %d, p.j = %d, mp->dst.j = %d, p.i = %d, mp->src.i = %d, p.j = %d, mp->src.j = %d\n", p.i, mp->dst.i, p.j, mp->dst.j, p.i, mp->src.i, p.j, mp->src.j);
}
// exit(1);
return false;
}
void initialize_board() {
int i, j;
for (j = 0; j < BOARD_SIZE; j++) {
board[1][j] = PAWN_W;
board[6][j] = PAWN_B;
}
board[0][0] = ROOK_W;
board[0][1] = KNIGHT_W;
board[0][2] = BISHOP_W;
board[0][3] = QUEEN_W;
board[0][4] = KING_W;
board[0][5] = BISHOP_W;
board[0][6] = KNIGHT_W;
board[0][7] = ROOK_W;
board[7][0] = ROOK_B;
board[7][1] = KNIGHT_B;
board[7][2] = BISHOP_B;
board[7][3] = QUEEN_B;
board[7][4] = KING_B;
board[7][5] = BISHOP_B;
board[7][6] = KNIGHT_B;
board[7][7] = ROOK_B;
for (i = 2; i < BOARD_SIZE - 2; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
move_counter = 0;
move_history = malloc(HISTSIZE * sizeof(Move));
}
void print_board(const Move *move, const Move *op_move, const Color color, const bool selected) {
Pos ind; // indeces
int i, j;
char buf[4];
Color square_color;
for (i = 0; i < BOARD_SIZE; i++) {
if (color == WHITE) ind.i = BOARD_SIZE - i - 1;
else ind.i = i;
printf("%d|", ind.i + 1);
for (j = 0; j < BOARD_SIZE; j++) {
if (color == BLACK) ind.j = BOARD_SIZE - j - 1;
else ind.j = j;
if (board[ind.i][ind.j] != EMPTY) {
strcpy(buf, BASE);
buf[2] += board[ind.i][ind.j];
}
else strcpy(buf, " ");
square_color = (ind.i + ind.j) % 2 != 0;
if (selected && ind.i == move->src.i && ind.j == move->src.j) {
printf("%s[", SELECTION_COLOR[square_color]);
printf("%s%s", BACKGROUND_COLOR[square_color], buf);
printf("%s]", SELECTION_COLOR[square_color]);
}
else if (ind.i == move->dst.i && ind.j == move->dst.j) {
printf("%s[", POINTER_COLOR[square_color]);
printf("%s%s", BACKGROUND_COLOR[square_color], buf);
printf("%s]", POINTER_COLOR[square_color]);
}
else if (move_counter > color && ind.i == op_move->src.i && ind.j == op_move->src.j) {
printf("%s[", OP_SELECTION_COLOR[square_color]);
printf("%s%s", BACKGROUND_COLOR[square_color], buf);
printf("%s]", OP_SELECTION_COLOR[square_color]);
}
else if (move_counter > color && ind.i == op_move->dst.i && ind.j == op_move->dst.j) {
printf("%s[", OP_POINTER_COLOR[square_color]);
printf("%s%s", BACKGROUND_COLOR[square_color], buf);
printf("%s]", OP_POINTER_COLOR[square_color]);
}
else printf("%s %s ", BACKGROUND_COLOR[square_color], buf);
}
printf("%s\n", BACKGROUND_DEFAULT);
}
if (color == WHITE) {
printf("%s\n", " ______________________");
printf("%s\n", " a b c d e f g h");
}
else {
printf("%s\n", " ______________________");
printf("%s\n", " h g f e d c b a");
}
}
bool threatened(const Pos* pos, const Color color, const Move *op_move) { // Checks whether any piece with color "color" threatens the position "pos"
int i, j;
Piece p;
Move mv = {.src = {0, 0}, .dst = {pos->i, pos->j}};
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
p = pieceatindex(i, j);
mv.src.i = i;
mv.src.j = j;
if (get_color(p) == color) {
if (is_valid_move[p](&mv, op_move)) return true;
}
}
}
return false;
}
bool threatened_index(int pos_i, int pos_j, const Color color, const Move *op_move) { // Checks whether any piece with color "color" threatens the indeces i, j
int i, j;
Piece p;
Move mv = {.src = {0, 0}, .dst = {pos_i, pos_j}};
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
p = pieceatindex(i, j);
mv.src.i = i;
mv.src.j = j;
if (get_color(p) == color) {
if (is_valid_move[p](&mv, op_move)) return true;
}
}
}
return false;
}
static bool checked(const Color color, const Move *op_move) { // Checks whether the player with color "color" is checked
int i, j;
Pos pos;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
pos.i = i;
pos.j = j;
if ((color == WHITE && pieceat(&pos) == KING_W) || (color == BLACK && pieceat(&pos) == KING_B)) {
return threatened(&pos, !color, op_move);
}
}
}
return false;
}
Status move_piece(const Move *move, const Move *op_move, const Color color) {
Piece taker = board[move->src.i][move->src.j];
Piece taken = board[move->dst.i][move->dst.j];
if (get_color(taker) != color || get_color(taken) == color) return BAD_MOVE;
if (!is_valid_move[taker](move, op_move)) return BAD_MOVE;
board[move->dst.i][move->dst.j] = taker;
board[move->src.i][move->src.j] = EMPTY;
if (checked(color, op_move)) {
board[move->src.i][move->src.j] = taker;
board[move->dst.i][move->dst.j] = taken;
return CHECKED;
}
memcpy(move_history + (move_counter++ % HISTSIZE), move, sizeof(Move)); // Store move in the history ring buffer
return 0;
}