This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.c
241 lines (191 loc) · 7.14 KB
/
chess.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
#include <ctype.h>
#include <stdlib.h>
#include <chess.h>
#define CHESS_GET_X(idx) ((idx) % 8)
#define CHESS_GET_Y(idx) ((idx) / 8)
#define CHESS_GET_IDX(x, y) ((x) + (y) * 8)
#define ABS(x) ((x) < 0 ? -(x) : (x))
int chess_turn(char piece) {
// Fully complete and working, do not touch
if (piece == ' ') return CHESS_NONE;
else if (isupper(piece)) return CHESS_WHITE;
else return CHESS_BLACK;
}
int chess_ended(char *board) {
int has_white_king = 0, has_black_king = 0;
for (int i = 0; i < 64; i++) {
if (board[i] == 'k') has_black_king = 1;
if (board[i] == 'K') has_white_king = 1;
}
return !(has_black_king && has_white_king);
}
int chess_valid(char *board, int *move, int turn) {
if (move[0] == move[1]) return 0;
else if (chess_turn(board[move[0]]) == chess_turn(board[move[1]])) return 0;
else if (chess_turn(board[move[0]]) != turn) return 0;
// Check if the move from piece at move[0] to index move[1] is valid
// for the specified player(CHESS_WHITE or CHESS_BLACK)
int pos_0[2] = {CHESS_GET_X(move[0]), CHESS_GET_Y(move[0])};
int pos_1[2] = {CHESS_GET_X(move[1]), CHESS_GET_Y(move[1])};
if (board[move[0]] == 'P') {
if (board[move[1]] == ' ' && pos_0[0] == pos_1[0]) {
if (pos_0[1] == pos_1[1] + 1)
return 1;
if (pos_0[1] == 6 && pos_1[1] == 4) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0], 5)]) == CHESS_NONE)
return 1;
}
}
if (pos_0[0] > 0) {
if (pos_1[0] == pos_0[0] - 1 && pos_1[1] == pos_0[1] - 1) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0] - 1, pos_0[1] - 1)]) == CHESS_BLACK)
return 1;
}
}
if (pos_0[0] < 7) {
if (pos_1[0] == pos_0[0] + 1 && pos_1[1] == pos_0[1] - 1) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0] + 1, pos_0[1] - 1)]) == CHESS_BLACK)
return 1;
}
}
}
if (board[move[0]] == 'p') {
if (board[move[1]] == ' ' && pos_0[0] == pos_1[0]) {
if (pos_0[1] == pos_1[1] - 1)
return 1;
if (pos_0[1] == 1 && pos_1[1] == 3) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0], 2)]) == CHESS_NONE)
return 1;
}
}
if (pos_0[0] > 0) {
if (pos_1[0] == pos_0[0] - 1 && pos_1[1] == pos_0[1] + 1) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0] - 1, pos_0[1] + 1)]) == CHESS_WHITE)
return 1;
}
}
if (pos_0[0] < 7) {
if (pos_1[0] == pos_0[0] + 1 && pos_1[1] == pos_0[1] + 1) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0] + 1, pos_0[1] + 1)]) == CHESS_WHITE)
return 1;
}
}
}
if (board[move[0]] == 'R' || board[move[0]] == 'r' || board[move[0]] == 'Q' || board[move[0]] == 'q') {
if (pos_0[0] == pos_1[0]) {
if (pos_0[1] > pos_1[1]) {
for (int i = pos_1[1]; i <= pos_0[1] - 1; i++) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0], i)]) == turn)
return 0;
if (i != pos_1[1] && chess_turn(board[CHESS_GET_IDX(pos_0[0], i)]) != CHESS_NONE && chess_turn(board[CHESS_GET_IDX(pos_0[0], i)]) != turn)
return 0;
}
} else {
for (int i = pos_0[1] + 1; i <= pos_1[1]; i++) {
if (chess_turn(board[CHESS_GET_IDX(pos_0[0], i)]) == turn)
return 0;
if (i != pos_1[1] && chess_turn(board[CHESS_GET_IDX(pos_0[0], i)]) != CHESS_NONE && chess_turn(board[CHESS_GET_IDX(pos_0[0], i)]) != turn)
return 0;
}
}
return 1;
}
if (pos_0[1] == pos_1[1]) {
if (pos_0[0] > pos_1[0]) {
for (int i = pos_1[0]; i <= pos_0[0] - 1; i++) {
if (chess_turn(board[CHESS_GET_IDX(i, pos_0[1])]) == turn)
return 0;
if (i != pos_1[0] && chess_turn(board[CHESS_GET_IDX(i, pos_0[1])]) != CHESS_NONE && chess_turn(board[CHESS_GET_IDX(i, pos_0[1])]) != turn)
return 0;
}
} else {
for (int i = pos_0[0] + 1; i <= pos_1[0]; i++) {
if (chess_turn(board[CHESS_GET_IDX(i, pos_0[1])]) == turn)
return 0;
if (i != pos_1[0] && chess_turn(board[CHESS_GET_IDX(i, pos_0[1])]) != CHESS_NONE && chess_turn(board[CHESS_GET_IDX(i, pos_0[1])]) != turn)
return 0;
}
}
return 1;
}
}
if (board[move[0]] == 'B' || board[move[0]] == 'b' || board[move[0]] == 'Q' || board[move[0]] == 'q') {
if (chess_turn(board[move[1]]) != turn) {
if (ABS(pos_0[0] - pos_1[0]) == ABS(pos_0[1] - pos_1[1])) {
int dir_x = (pos_1[0] - pos_0[0]) / ABS(pos_1[0] - pos_0[0]);
int dir_y = (pos_1[1] - pos_0[1]) / ABS(pos_1[1] - pos_0[1]);
int pos_x = pos_0[0] + dir_x, pos_y = pos_0[1] + dir_y;
while (pos_x != pos_1[0] && pos_y != pos_1[1]) {
if (chess_turn(board[CHESS_GET_IDX(pos_x, pos_y)]) != CHESS_NONE)
return 0;
pos_x += dir_x; pos_y += dir_y;
}
return 1;
}
}
}
if (board[move[0]] == 'N' || board[move[0]] == 'n') {
if (chess_turn(board[move[1]]) != turn) {
int can_move = 0;
can_move |= (pos_1[0] == pos_0[0] - 1 && pos_1[1] == pos_0[1] - 2);
can_move |= (pos_1[0] == pos_0[0] - 1 && pos_1[1] == pos_0[1] + 2);
can_move |= (pos_1[0] == pos_0[0] + 1 && pos_1[1] == pos_0[1] - 2);
can_move |= (pos_1[0] == pos_0[0] + 1 && pos_1[1] == pos_0[1] + 2);
can_move |= (pos_1[0] == pos_0[0] - 2 && pos_1[1] == pos_0[1] - 1);
can_move |= (pos_1[0] == pos_0[0] - 2 && pos_1[1] == pos_0[1] + 1);
can_move |= (pos_1[0] == pos_0[0] + 2 && pos_1[1] == pos_0[1] - 1);
can_move |= (pos_1[0] == pos_0[0] + 2 && pos_1[1] == pos_0[1] + 1);
if (can_move) return 1;
}
}
if (board[move[0]] == 'K' || board[move[0]] == 'k') {
int off_x = ABS(pos_0[0] - pos_1[0]);
int off_y = ABS(pos_0[1] - pos_1[1]);
if (off_x < 2 && off_y < 2 && !(off_x == 0 && off_y == 0)) {
return 1;
}
}
return 0;
}
int **chess_get_moves(char *board, int turn) {
// I mean, it technically works, but it is the fuckin
// slowest way of doing it in the entire universe...
// we never saw the aliens' solution...
int **move_arr = malloc(sizeof(int *));
int move_cnt = 0;
move_arr[move_cnt] = NULL;
if (!board) return move_arr;
if (chess_ended(board)) return move_arr;
for (int i = 0; i < 64; i++) {
if (chess_turn(board[i]) == turn) {
for (int j = 0; j < 64; j++) {
if (i != j && chess_turn(board[j]) != turn) {
int move[2] = {i, j};
if (chess_valid(board, move, turn)) {
move_arr = realloc(move_arr, (move_cnt + 2) * sizeof(int *));
move_arr[move_cnt] = malloc(2 * sizeof(int));
move_arr[move_cnt][0] = i;
move_arr[move_cnt][1] = j;
move_arr[++move_cnt] = NULL;
}
}
}
}
}
return move_arr;
}
void chess_free(int **moves) {
int move_cnt = 0;
while (moves[move_cnt] != NULL)
free(moves[move_cnt++]);
free(moves);
}
void chess_move(char *board, int *move) {
// Fully complete and working, do not touch
if (CHESS_GET_Y(move[1]) == 7 && board[move[0]] == 'p')
board[move[0]] = 'q';
if (CHESS_GET_Y(move[1]) == 0 && board[move[0]] == 'P')
board[move[0]] = 'Q';
board[move[1]] = board[move[0]];
board[move[0]] = ' ';
}