-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
432 lines (424 loc) · 13.1 KB
/
Copy pathmain.c
File metadata and controls
432 lines (424 loc) · 13.1 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
typedef struct pieces {
char id;
char owner;// w for white, b for black, s for space
wchar_t* symw;//symbol for white
wchar_t* symb;//symbol for black
char * name;//name of the piece
}piece;
static piece wk = {'k', 'w', L"♔", L"♚", "king"};
static piece wp = {'h', 'w', L"♙", L"♟", "pawn"};//the h is for wHite pawn
static piece wb = {'b', 'w', L"♗", L"♝", "bishop"};
static piece wn = {'n', 'w', L"♘", L"♞", "knight"};
static piece wr = {'r', 'w', L"♖", L"♜", "rook"};
static piece wq = {'q', 'w', L"♕", L"♛", "queen"};
static piece bk = {'k', 'b', L"♔", L"♚", "king"};
static piece bp = {'l', 'b', L"♙", L"♟", "pawn"};//the l is for bLack pawn
static piece bb = {'b', 'b', L"♗", L"♝", "bishop"};
static piece bn = {'n', 'b', L"♘", L"♞", "knight"};//the n is for kNight
static piece br = {'r', 'b', L"♖", L"♜", "rook"};
static piece bq = {'q', 'b', L"♕", L"♛", "queen"};
static piece es = {'s', 's', L" ", L" " , "blank space"};//the s is for blank Space
/*piece * board[8][8] = {
{&wr,&wn,&wb,&wk,&wq,&wb,&wn,&wr},
{&wp,&wp,&wp,&wp,&wp,&wp,&wp,&wp},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&bp,&bp,&bp,&bp,&bp,&bp,&bp,&bp},
{&br,&bn,&bb,&bk,&bq,&bb,&bn,&br}
};*/
piece * board[8][8] = {
{&es,&es,&es,&es,&wk,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&wp,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&bk,&es,&es}
};
piece * calc_board[8][8] = {
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es},
{&es,&es,&es,&es,&es,&es,&es,&es}
};
void copy_on(piece* boardtc[8][8], piece* boardon[8][8]){
for (int i=0; i<8;i++){
for (int j=0;j<8;j++){
boardon[i][j]=boardtc[i][j];
}
}
}
int is_in_board(int x,int y){
return (x>=0&&x<8&&y>=0&&y<8);
}
int in_the_way(piece* board[8][8], int x1, int y1, int x2, int y2){
//return 1 if something is in the way between the 2 square, or else 0, raise an error if not in the board
assert((is_in_board(x1,y1)) && (is_in_board(x2,y2)));
if ((abs(x1-x2)==1&&abs(y1-y2)==1)||((x1!=x2&&y1!=y2)&&(abs(x2-x1)!=abs(y2-y1)))){
return 0;
}
if (x1==x2){
if (y1<y2){
for (int y=y1+1;y<y2;y++){
if (board[x1][y]->id!='s'){
return 1;
}
}
return 0;
}
if (y1>y2){
for (int y=y1-1;y>y2;y--){
if (board[x1][y]->id!='s'){
return 1;
}
}
return 0;
}
}
if (y1==y2){
if (x2>x1){
for (int x=x1+1;x<x2;x++){
if (board[x][y1]->id!='s'){
return 1;
}
}
return 0;
}
if (x1>x2){
for (int x=x1-1;x>x2;x--){
if (board[x][y1]->id!='s'){
return 1;
}
}
return 0;
}
}
if (abs(x2-x1)==abs(y2-y1)){
if (x2>x1&&y2>y1){
for (int i=1; i<abs(x2-x1); i++){
assert((is_in_board(x1+i,y1+i)));
if (board[x1+i][y1+i]->id!='s'){
return 1;
}
}
return 0;
}
else if (x2<x1&&y2>y1){
for (int i=1; i<abs(x2-x1); i++){
assert((is_in_board(x1-i,y1+i)));
if (board[x1-i][y1+i]->id!='s'){
return 1;
}
}
return 0;
}
else if (x2>x1&&y2<y1){
for (int i=1; i<abs(x2-x1); i++){
assert((is_in_board(x1+i,y1-i)));
if (board[x1+i][y1-i]->id!='s'){
return 1;
}
}
return 0;
}
if (x2<x1&&y2<y1){
for (int i=1; i<abs(x2-x1); i++){
assert((is_in_board(x1-i,y1-i)));
if (board[x1-i][y1-i]->id!='s'){
return 1;
}
}
}
return 0;
}
return 0;
}
int is_valid(piece * board[8][8], int x1, int y1, int x2, int y2){
//return 1 if move possible without consideration of checks, else return 0
if ( !( (is_in_board(x1,y1)) && (is_in_board(x2,y2)) ) ){
return 0;
}
if (in_the_way(board, x1, y1, x2, y2)){
return 0;
}
piece * s_piece = board[x1][y1];//the selected piece
piece * desti = board[x2][y2];//the destination of the piece
if (x1==x2&&y1==y2){
return 0;
}
if (s_piece->owner==desti->owner){
return 0;
}
switch (s_piece->id) {
case 's':
return 0;
break;
case 'k':
if (abs(x1-x2)<=1 && abs(y1-y2)<=1){
return 1;
}
else {
return 0;
}
break;
case 'h':
if ((x2-x1==1&&y1==y2&&desti->owner=='s')
||(x1==1&&x2==3&&y1==y2&&desti->owner=='s')
||(x2-x1==1&&abs(y1-y2)==1&&desti->owner=='b')){
return 1;
}
else {
return 0;
}
break;
case 'l':
if ((x2-x1==-1&&y1==y2&&desti->owner=='s')
||(x1==6&&x2==4&&y1==y2&&desti->owner=='s')
||(x2-x1==-1&&abs(y1-y2)==1&&desti->owner=='w')){
return 1;
}
else {
return 0;
}
break;
case 'b':
if (abs(x2-x1)==abs(y2-y1)){
return 1;
}
else{
return 0;
}
break;
case 'n':
if ((abs(x2-x1)==1&&abs(y2-y1)==2)
||(abs(x2-x1)==2&&abs(y2-y1)==1)){
return 1;
}
else{
return 0;
}
break;
case 'r':
if (x1==x2||y1==y2){
return 1;
}
else {
return 0;
}
break;
case 'q':
if ((x1==x2||y1==y2)||(abs(x2-x1)==abs(y2-y1))){
return 1;
}
else return 0;
}
return 0;
}
int naive_move(piece * board[8][8],int x1, int y1, int x2, int y2){
//move a piece and return 1 if successful, 0 otherwise
if (is_valid(board, x1, y1, x2, y2)){
board[x2][y2]=board[x1][y1];
board[x1][y1]=&es;
return 1;
}
else{
return 0;
}
}
int promote(piece* board[8][8], int x,int y){
if ((board[x][y]->id=='h'&&x==7)||(board[x][y]->id=='l'&&x==0)){
wprintf(L"To which piece would you like to promote ?");
scanf(" %c",&board[x][y]->id);
if (board[x][y]->id!='q'&&board[x][y]->id!='r'&&board[x][y]->id!='n'&&board[x][y]->id!='b'
&&(board[x][y]->id!='l'&&board[x][y]->owner=='b')
&&(board[x][y]->id!='h'&&board[x][y]->owner=='w')){
printf("Please choose a valid piece.");
board[x][y]->id=(board[x][y]->owner=='w')?'h':'l';
promote(board, x, y);
return 1;
}
}
return 1;
}
int find_piece(piece * board[8][8], char ide, char powner){
//return 88 if not found else return a string in the form of a number with digits xy with xy coordinate of the piece from the powner
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
if (board[i][j]->id==ide&&board[i][j]->owner==powner){
int x=i;
int y=j*10;
return x+y;
}
}
}
return 88;
}
int is_attacked(piece* board[8][8], int x, int y){
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
if (is_valid(board, i,j, x, y)){
return 1;
}
}
}
return 0;
}
int is_king_check(piece * board[8][8], char pcp){
//pcp is for potentially checked player; 0 if no kings; 0 if king not checked; 1 if king checked
int posintking = find_piece(board, 'k', pcp);
if (posintking==88){
return 0;
}
int xk=posintking%10;
int yk=posintking/10;
if (is_attacked(board, xk, yk)){
return 1;
}
return 0;
}
int is_player_checkmated(piece* board[8][8], piece * calc_board[8][8], char pcp){
//check if pcp is checkmated and if it is the case return 1, else return 0
if (!(is_king_check(board, pcp))){
return 0;
}
int posintking=find_piece(board,'k', pcp);
int xk=posintking%10;
int yk=posintking/10;
int x; int y;
for (int i=0; i<9; i++){
x=i%3+xk-1;
y=i/3+yk-1;
copy_on(board,calc_board);
if (naive_move(calc_board, xk, yk, x, y) && !(is_attacked(calc_board, x,y))){
return 0;
}
}
return 1;
}
int is_legal(piece* board[8][8], piece * calc_board[8][8], char pcp, int x1, int y1, int x2, int y2){
if (!(is_valid(board, x1, y1, x2,y2))) return 0;
copy_on(board,calc_board);
naive_move(calc_board, x1, y1, x2, y2);
if (is_king_check(calc_board, pcp)) return 0;
return 1;
}
int is_stalemated(piece* board[8][8],piece * calc_board[8][8], char ppp){
// check if ppp is pat
if (is_king_check(board, ppp)){
return 0;
}
int pflip = 1; //flip to 0 to piece that are not kings
int lflip = 0; //flip to 1 if find a legal move
for (int x1=0; x1<8; x1++){
for (int y1=0; y1<8; y1++){
if (board[x1][y1]->id!='s'&&board[x1][y1]->id!='k') {pflip=0;}
if (board[x1][y1]->owner==ppp){
for (int x2=0; x2<8; x2++){
for (int y2=0; y2<8; y2++){
if (is_legal(board, calc_board, ppp, x1, y1, x2, y2)){
lflip= 0;
}
}
}
}
if (pflip&&lflip) return 1;
}
}
return pflip||lflip;
}
int move(piece* board[8][8],piece * calc_board[8][8], char pcp, int x1, int y1, int x2, int y2){
if (is_legal(board, calc_board, pcp, x1, y1, x2, y2)) {
naive_move(board, x1, y1, x2, y2);
return 1;
}
else {
return 0;
}
}
void fancy_show_board(piece* board[8][8]){
setlocale(LC_ALL, "C.UTF-8");
for (int i=0; i<8; i++){
for (int j=0; j<8; j++){
if (board[i][j]->owner=='b') wprintf(L"| %ls ",board[i][j]->symb);
else wprintf(L"| %ls ",board[i][j]->symw);
}
wprintf(L"| \n");
for (int k=0; k<11; k++){wprintf(L"---");}
wprintf(L"\n");
}
}
void ascii_show_board(piece* board[8][8]){
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++){
if (board[i][j]->id!='s')wprintf(L"%c%c ",board[i][j]->owner,board[i][j]->id);else wprintf(L"es ");
}
wprintf(L"\n");
}
}
int launch_game(piece* board[8][8], piece * calc_board[8][8]){
char active_player='w';
char passive_player='b';
int turn_num=0;
char winner='y';//w is for no winner Yet, w is for white, b is for black, n is for no winner (stalemate)
int last_move = 0; //move must be formatted as an integer with 4 decimals, each decimal represent a coordinate starting from 1 to 8 : x1y1x2y2
int x1;int y1;int x2;int y2;
ascii_show_board(board);
while (last_move!=9999&&winner=='y'){
if (is_stalemated(board, calc_board, active_player)) {
winner='n';
break;
}
if (is_player_checkmated(board, calc_board, active_player)){
winner=passive_player;
ascii_show_board(board);
break;
}
wprintf(L"%c to play the move ! ",active_player);
scanf("%d",&last_move);
x1=last_move/1000 -1;
y1=(last_move%1000)/100 -1;
x2=(last_move%100)/10 -1;
y2=last_move%10 -1;
if (move(board, calc_board, active_player, x1, y1, x2, y2)){
if (promote(board, x2, y2)){
ascii_show_board(board);}
if (is_player_checkmated(board, calc_board, passive_player)){
winner=active_player;
break;
}
char temp=active_player;
active_player=passive_player;
passive_player=temp;
turn_num++;
}
else {
wprintf(L"Illegal move, please try again.\n");
ascii_show_board(board);
continue;
}
}
switch (winner) {
case 'n' : wprintf(L"That's a stalemate"); break;
case 'w' : wprintf(L"The whites win"); break;
case 'b' : wprintf(L"The blacks win"); break;
}
return 0;
}
int main (void){
launch_game(board, calc_board);
printf("a");
return 0;
}