├── COPYING
├── README
└── src
├── AI.h
└── chess3.c
/COPYING:
--------------------------------------------------------------------------------
1 | This program is free software: you can redistribute it and/or modify
2 | it under the terms of the GNU General Public License as published by
3 | the Free Software Foundation, either version 3 of the License, or
4 | (at your option) any later version.
5 |
6 | This program is distributed in the hope that it will be useful,
7 | but WITHOUT ANY WARRANTY; without even the implied warranty of
8 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 | GNU General Public License for more details.
10 |
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see .
13 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | This is a chess program completely written in C Programming Language (C99 standard).It is without GUI, so it can be played on linux terminal.The chess grid is made using Unicode and accepts input in the form :-
2 |
3 | Type in your move{prompt}: 1 b1 c1
4 | The above command moves pawn(1) from it's initial position 'b1' to final position 'c1'.
5 |
6 | The Artificial Intelligence code is written in the file under the name AI.c, which is capable of noticing check conditions and avoiding them too. Game Tree concept has been implemented which gives a lot of room for development as in here the evaluation is being done only for a single future instance i.e. it predicts the human output for the next immediate move only and then from there ,it evaluates the best possible move for itself(computer).So there is enough space to increase the accuracy of the game by evaluating the outcomes for more future instances
7 |
8 | This code is capable of noticing and rejecting "illegal" moves typed-in by a human player, so it goes without saying that with little editing the game can work for human v/s human matches.
9 | A lot many things are not implemented in this program for example, the code is strictly an "intelligent" code as there is no previous memory or experience, many algorithms have not been implemented and people may want to write code for GUI interface since it has none.
10 |
11 | INTENDED AUDIENCE
12 | Any person having basic knowledge of C Programming Language and willing to spend a few hours reading and understanding the code.
13 |
14 | COMPILATION
15 | Type-in the following command in the directory containing the files chess3.c and AI.h:-
16 | gcc -std="c99" chess3.c AI.h -o chess
17 | which will create an executable file under the name chess, open this file and the game will be up and running.
18 |
19 | FOR WINDOWS USERS
20 | You may download many of the gcc ide's or softwares available for free on the net.Some of the Ide's are given here:
21 |
22 | 1)Eclipse CDT (you can download bundle from http://eclipse.org)
23 |
24 | 2)Cygwin (http://www.cygwin.com/)
25 | gives a linux like environment on windows
26 |
27 | 3)Code::Blocks IDE (http://www.codeblocks.org/)
28 |
29 | 4)CodeLite opensource cross platform IDE(http://codelite.org/)
30 | It works great on almost all Operating Systems: Windows XP/7 and 8, Debian / Ubuntu, Fedora / OpenSUSE, Mac OSX 10.5.8
31 |
32 | If you wish to proceed in the direction of software development, using a good shell won't hurt!
33 |
34 | Have Fun!!
35 |
36 | CREDITS
37 | Sharat Shankar
38 | email Id:sharat.shankar15@gmail.com
39 |
40 | Parigyan Chandra Goyal
41 | email Id:panzer.surfer@gmail.com
42 |
43 |
--------------------------------------------------------------------------------
/src/AI.h:
--------------------------------------------------------------------------------
1 | /*Copyright 2012 Parigyan Chandra Goyal & Sharat Shankar*/
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #define mark 1000
9 |
10 | typedef struct {
11 | int i, j, k, l, eval;
12 | char side;
13 | char ent1;
14 | } list_elm;
15 |
16 | typedef struct node {
17 | struct node *head;
18 | list_elm* elist;
19 | struct node* array[100];
20 | int dcntr;/*to know how many times our king can be killed{ d = defeat}*/
21 | int vcntr;
22 | int max_val;
23 | int min_val;
24 | } node;
25 |
26 | int compute(char ent1,int i1,int j1,int k1,int l1,int cond,char chess[2][8][8]);
27 |
28 | void printing(char chp[2][8][8]);
29 |
30 | void PawnAI(char swap[2][8][8], char side, int i,int j, node* root);
31 | void KnightAI(char swap[2][8][8], char side, int i, int j, node* root);
32 | void BishopAI(char swap[2][8][8],char side, int i, int j, node* root);
33 | void RookAI(char swap[2][8][8], char side, int i, int j, node* root);
34 | void QueenAI(char swap[2][8][8], char side, int i, int j, node* root);
35 | void KingAI(char swap[2][8][8], char side, int i, int j, node* root);
36 | void common_init(int i1, int j1, int k1, int l1, char swap[2][8][8], node *root,char play_side);
37 |
38 |
39 | void assign_ent(char side, node* branch, char swap[2][8][8])
40 | {
41 | int i,j=0;
42 |
43 | /* printf("\n\n\n\n");
44 | printing(swap);*/
45 |
46 | for(i;i<=7;i++)
47 | {
48 | j=0;
49 | for(j;j<=7;j++)
50 | {
51 | if(swap[1][i][j] == side)
52 | {
53 | char ent = swap[0][i][j];
54 | switch(ent)
55 | {
56 | case '1':
57 | PawnAI(swap,side,i,j,branch);
58 | break;
59 | case '2':
60 | KnightAI(swap,side,i,j,branch);
61 | break;
62 | case '4':
63 | BishopAI(swap,side,i,j,branch);
64 | break;
65 | case '5':
66 | RookAI(swap,side,i,j,branch);
67 | break;
68 | case '7':
69 | QueenAI(swap,side,i,j,branch);
70 | break;
71 | case '9':
72 | KingAI(swap,side,i,j,branch);
73 | break;
74 |
75 | default: break;
76 | };
77 | }
78 | };
79 | };
80 | }
81 |
82 | void PawnAI(char swap[2][8][8], char side, int i,int j, node* root)
83 | {
84 | printf("pawn %d %d %c\n",i,j,side);
85 | int outer;
86 | int inner;
87 | if(side== '1')
88 | {
89 | int moves[8]={i+1,j,i+2,j,i+1,j+1,i+1,j-1};
90 | outer=0;
91 | inner=1;
92 | for(outer;outer<=6; outer+=2)
93 | {
94 | if(moves[outer]>7 || moves[outer]<0)
95 | {
96 | moves[outer] = mark;
97 | moves[outer+1] = mark;
98 | }
99 |
100 | if(moves[inner]>7 || moves[inner]<0)
101 | {
102 | moves[outer] = mark;
103 | moves[inner] = mark;
104 | }
105 |
106 | if(moves[outer]!= mark)
107 | {
108 | int k,l;
109 | k = moves[outer];
110 | l = moves[inner];
111 | /* printf("human pawn common_init k=%d l=%d\n",k,l);*/
112 | common_init(i,j,k,l,swap,root,side);
113 | }
114 |
115 | inner+=2;
116 | };
117 |
118 |
119 | }
120 |
121 | if(side == '2')
122 | {
123 | int moves[8] = {i-1,j,i-2,j,i-1,j+1,i-1,j-1};
124 | outer =0;
125 | inner =1;
126 | for(outer;outer<=6; outer+=2)
127 | {
128 | if(moves[outer]>7 || moves[outer]<0)
129 | {
130 | moves[outer] = mark;
131 | moves[outer+1] = mark;
132 | }
133 |
134 | if(moves[inner]>7 || moves[inner]<0)
135 | {
136 | moves[outer] = mark;
137 | moves[inner] = mark;
138 | }
139 |
140 | if(moves[outer]!= mark)
141 | {
142 | int k,l;
143 | k = moves[outer];
144 | l = moves[inner];
145 | /* printf("computer pawn common_init k=%d l=%d\n",k,l);*/
146 | common_init(i,j,k,l,swap,root,side);
147 | }
148 |
149 | inner+=2;
150 | };
151 |
152 | }
153 | }
154 |
155 | void KnightAI(char swap[2][8][8], char side, int i, int j, node* root)
156 | {
157 | printf("knight %d %d %c\n",i,j,side);
158 | int moves[16] = {i+2,j-1,i+2,j+1,i-2,j-1,i-2,j+1,i+1,j+2,i+1,j-2,i-1,j+2,i-1,j-2};
159 | int outer = 0;
160 | int inner =1;
161 |
162 | for(outer;outer<=14; outer+=2)
163 | {
164 | if(moves[outer]>7 || moves[outer]<0)
165 | {
166 | moves[outer] = mark;
167 | moves[outer+1] = mark;
168 | }
169 |
170 | if(moves[inner]>7 || moves[inner]<0)
171 | {
172 | moves[outer] = mark;
173 | moves[inner] = mark;
174 | }
175 |
176 | if(moves[outer]!= mark)
177 | {
178 | int k,l;
179 | k = moves[outer];
180 | l = moves[inner];
181 | common_init(i,j,k,l,swap,root,side);
182 | }
183 |
184 | inner+=2;
185 | };
186 | }
187 |
188 | void BishopAI(char swap[2][8][8],char side, int i, int j, node* root)
189 | {
190 | printf("bishop %d %d %c\n",i,j,side);
191 | int moves[56];
192 | int i1=i;
193 | int j1=j;
194 | int outer =0;
195 | int inner =1;
196 |
197 | for(outer; outer<=12; outer+=2)
198 | {
199 | moves[outer] = i1+1;
200 | moves[inner] = j1+1;
201 | i1++;
202 | j1++;
203 | inner+=2;
204 | };
205 |
206 | for(outer; outer<=26; outer+=2)
207 | {
208 | moves[outer] = i1+1;
209 | moves[inner] = j1-1;
210 | i1++;
211 | j1--;
212 | inner+=2;
213 | };
214 |
215 | for(outer;outer<=40; outer+=2)
216 | {
217 | moves[outer] = i1-1;
218 | moves[inner] = j1+1;
219 | i1--;
220 | j1++;
221 | inner+=2;
222 | };
223 |
224 | for(outer; outer<=54; outer+=2)
225 | {
226 | moves[outer] = i1-1;
227 | moves[inner] = j1-1;
228 | i1--;
229 | j1--;
230 | inner+=2;
231 | };
232 |
233 | outer=0;
234 | inner=1;
235 | for(outer;outer<=54; outer+=2)
236 | {
237 | if(moves[outer]>7 || moves[outer]<0)
238 | {
239 | moves[outer] = mark;
240 | moves[outer+1] = mark;
241 | }
242 |
243 | if(moves[inner]>7 || moves[inner]<0)
244 | {
245 | moves[outer] = mark;
246 | moves[inner] = mark;
247 | }
248 |
249 | if(moves[outer]!= mark)
250 | {
251 | int k,l;
252 | k = moves[outer];
253 | l = moves[inner];
254 | common_init(i,j,k,l,swap,root,side);
255 | }
256 |
257 | inner+=2;
258 | };
259 |
260 | }
261 |
262 |
263 | void RookAI(char swap[2][8][8], char side, int i, int j, node* root)
264 | {
265 | printf("rook %d %d %c\n",i,j,side);
266 | int moves[56];
267 | int i1,i2,i3,i4 = i;
268 | int j1,j2,j3,j4 = j;
269 | int outer = 0;
270 | int inner =1;
271 |
272 | for(outer; outer<=54; outer+=8)
273 | {
274 | moves[outer] = i1+1;
275 | moves[inner] = j1+1;
276 | i1++;
277 | j1++;
278 |
279 | moves[outer+2] = i2+1;
280 | moves[inner+2] = j2-1;
281 | i2++;
282 | j2--;
283 |
284 | moves[outer+4] = i3-1;
285 | moves[inner+4] = j3+1;
286 | i3--;
287 | j3++;
288 |
289 | moves[outer+6] = i4-1;
290 | moves[inner+6] = j4-1;
291 | i4--;
292 | j4--;
293 |
294 | inner+=8;
295 | };
296 |
297 | outer = 0;
298 | inner = 1;
299 | for(outer;outer<=54; outer+=2)
300 | {
301 | if(moves[outer]>7 || moves[outer]<0)
302 | {
303 | moves[outer] = mark;
304 | moves[outer+1] = mark;
305 | }
306 |
307 | if(moves[inner]>7 || moves[inner]<0)
308 | {
309 | moves[outer] = mark;
310 | moves[inner] = mark;
311 | }
312 |
313 | if(moves[outer]!= mark)
314 | {
315 | int k,l;
316 | k = moves[outer];
317 | l = moves[inner];
318 | common_init(i,j,k,l,swap,root,side);
319 | }
320 |
321 | inner+=2;
322 | };
323 | }
324 |
325 | void QueenAI(char swap[2][8][8], char side, int i, int j, node *root)
326 | {
327 | printf("queen %d %d %c\n",i,j,side);
328 | BishopAI(swap,side,i,j,root);
329 | RookAI(swap,side,i,j,root);
330 | printf("queen ends %d %d %c\n",i,j,side);
331 | }
332 |
333 | void KingAI(char swap[2][8][8],char side, int i, int j,node *root)
334 | {
335 | printf("king %d %d %c\n",i,j,side);
336 | int moves[16] = {i+1,j,i+1,j+1,i+1,j-1,i,j-1,i,j+1,i-1,j,i-1,j-1,i-1,j+1};
337 | int outer = 0;
338 | int inner =1;
339 |
340 | for(outer;outer<=14; outer+=2)
341 | {
342 | if(moves[outer]>7 || moves[outer]<0)
343 | {
344 | moves[outer] = mark;
345 | moves[outer+1] = mark;
346 | }
347 |
348 | if(moves[inner]>7 || moves[inner]<0)
349 | {
350 | moves[outer] = mark;
351 | moves[inner] = mark;
352 | }
353 |
354 | if(moves[outer]!= mark)
355 | {
356 | int k,l;
357 | k = moves[outer];
358 | l = moves[inner];
359 | common_init(i,j,k,l,swap,root,side);
360 | }
361 |
362 | inner+=2;
363 | };
364 |
365 | }
366 |
367 | void common_init(int i1, int j1, int k1, int l1, char swap[2][8][8], node *root,char play_side)
368 | {
369 | char init_side = swap[1][i1][j1];
370 | char init_ent = swap[0][i1][j1];
371 | char finl_side = swap[1][k1][l1];
372 | char finl_ent = swap[0][k1][l1];
373 |
374 | int error = compute(init_ent, i1, j1, k1, l1, 1, swap);
375 | printf("error %d ",error);
376 |
377 | if(error == 0)
378 | {
379 | list_elm* list = (list_elm*)malloc(sizeof(list_elm));
380 | list->i = i1;
381 | list->j = j1;
382 | list->k = k1;
383 | list->l = l1;
384 |
385 | node* branch = (node*)malloc(sizeof(node));
386 | branch->head = root;
387 | branch->elist = list;
388 | branch->dcntr = 0;
389 | branch->vcntr = 0;
390 | branch->max_val = 0;
391 | branch->min_val = 0;
392 | int z =0; int c= 0;
393 | for(z; z<=99; z++)
394 | {
395 | branch->array[z] =NULL;
396 | if(root->array[z] == NULL && c==0)
397 | {
398 | root->array[z] = branch; /*this is because we want the first NULL entry */
399 | c =1;
400 | }
401 | };
402 |
403 | int defeat =0;
404 | int victory=0;
405 | int val =0;
406 | char ent = '\0';
407 | char side ='\0';
408 | z=0 ;c= 0;
409 | for(c; c<=7; c++)
410 | {
411 | z=0;
412 | for(z;z<=7;z++)
413 | {
414 | ent = swap[0][c][z];
415 | side = swap[1][c][z];
416 |
417 | switch(ent)
418 | {
419 | case'1':
420 | {
421 | if(side == '1')
422 | val = val-1;
423 | else if(side == '2')
424 | val =val+1;
425 | break;
426 | }
427 |
428 | case'2':
429 | {
430 | if(side == '1')
431 | val = val-2;
432 | else if(side == '2')
433 | val =val+2;
434 | break;
435 | }
436 | case'4':
437 | {
438 | if(side == '1')
439 | val = val-4;
440 | else if(side == '2')
441 | val =val+4;
442 | break;
443 | }
444 | case'5':
445 | {
446 | if(side == '1')
447 | val = val-5;
448 | else if(side == '2')
449 | val =val+5;
450 | break;
451 | }
452 | case'7':
453 | {
454 | if(side == '1')
455 | val = val-7;
456 | else if(side == '2')
457 | val =val+7;
458 | break;
459 | }
460 | case'9':
461 | {
462 | if(side == '1')
463 | {
464 | val = val-9;
465 | defeat = 1;
466 | }
467 | else if(side == '2')
468 | {
469 | val =val+9;
470 | victory=1;
471 | }
472 | break;
473 | }
474 | default:
475 | break;
476 |
477 | };/*END OF SWITCH*/
478 | };/*End of FOR INNER LOOP*/
479 | };/*End of FOR OUTER LOOP*/
480 |
481 | if(defeat==0 && victory==1)
482 | {
483 | branch->vcntr+=1;
484 | }
485 |
486 | else if(defeat==1 && victory==0)
487 | {
488 | branch->dcntr+=1;
489 | }
490 |
491 | branch->elist->eval = val;
492 | /* printf("eval=%d\n\n", branch->elist->eval);*/
493 | swap[0][i1][j1] = init_ent; /* this is */
494 | swap[1][i1][j1] = init_side; /* for undoing */
495 | swap[0][k1][l1] = finl_ent; /* the changes */
496 | swap[1][k1][l1] = finl_side; /* in swap */
497 |
498 | };
499 | }
500 |
501 | void FreeAll(node*);
502 |
503 | /*From here begins the AI operation*/
504 | void AIfunc(char origin[2][8][8])
505 | {
506 | /* printf("origin starts\n");
507 | printing(origin);
508 | printf("origin ends\n");*/
509 | char copy[2][8][8];
510 | char swap[2][8][8];
511 |
512 | int a,b =0;
513 | for(a; a<=7; a++)
514 | {
515 | b=0;
516 | for(b; b<=7; b++)
517 | {
518 | copy[0][a][b] = origin[0][a][b];
519 | copy[1][a][b] = origin[1][a][b];
520 |
521 | swap[0][a][b] = origin[0][a][b];
522 | swap[1][a][b] = origin[1][a][b];
523 | };
524 | };
525 | /* printing(swap);*/
526 |
527 | node *root;
528 | root = (node*)malloc(sizeof(node));
529 | root->head = NULL;
530 | root->elist = NULL;
531 | root->dcntr = 0;
532 | root->vcntr = 0;
533 | root->max_val = 0;
534 | root->min_val = 0;
535 |
536 | int z =0;
537 | for(z; z<=99; z++)
538 | {
539 | root->array[z] = NULL;
540 | };
541 |
542 | /* printing(swap);*/
543 | assign_ent('2',root, swap);
544 |
545 | z =0;
546 | int maxim_val = -100;
547 | node *shell = NULL;
548 | for(z; z<=99; z++)
549 | {
550 | shell =root->array[z];
551 | if(shell!=NULL)
552 | if(maxim_val < shell->elist->eval)
553 | maxim_val = shell->elist->eval;
554 | };
555 |
556 | if(maxim_val >= 0)
557 | maxim_val = 0.3*maxim_val;
558 |
559 | else
560 | maxim_val = 3*maxim_val;
561 |
562 | printf("maxim_val %d\n",maxim_val);
563 | z =0;
564 |
565 | for(z;z<=99; z++)
566 | {
567 | shell = root->array[z];
568 | if(shell!=NULL && shell->elist->eval < maxim_val)
569 | {
570 | free(shell->elist);
571 | free(shell);
572 | root->array[z] =NULL;
573 | }
574 | };
575 |
576 | char init_ent = '\0';
577 | char init_side = '\0';
578 | char finl_ent = '\0';
579 | char finl_side = '\0';
580 |
581 | z =0;
582 | int i1,j1,k1,l1;
583 | for(z;z<=99; z++)
584 | {
585 | if(root->array[z]!=NULL)
586 | {
587 | node* branch = root->array[z];
588 | i1 = branch->elist->i;
589 | j1 = branch->elist->j;
590 | k1 = branch->elist->k;
591 | l1 = branch->elist->l;
592 |
593 | init_ent = swap[0][i1][j1];
594 | init_side = swap[1][i1][j1];
595 | finl_ent = swap[0][k1][l1];
596 | finl_side = swap[1][k1][l1];
597 |
598 | int qw = compute(init_ent,i1,j1,k1,l1,1,swap);
599 | assign_ent('1',branch,swap);
600 |
601 | swap[0][i1][j1] = init_ent;
602 | swap[1][i1][j1] = init_side;
603 | swap[0][k1][l1] = finl_ent;
604 | swap[1][k1][l1] = finl_side;
605 | }
606 | };/*till here we have generated all the moves which are possible.*/
607 |
608 | node* subshell;
609 | node* ult_vic_shell ;
610 | node* vic_shell;
611 | ult_vic_shell = NULL;
612 | vic_shell = NULL;
613 | int min_dcntr = 100;
614 |
615 | int threat = 0; /*to know whether there is any imminent threat
616 | *and this signifies a checkmate condition */
617 | z = 0;
618 | for(z ; z<=99;z++)
619 | {
620 | shell = root->array[z];
621 | if(shell!= NULL)
622 | {
623 | int ma_val= -100;
624 | int mi_val = 100;
625 | int c =0;
626 | for(c;c<=99; c++)
627 | {
628 | subshell = shell->array[c];
629 | if(subshell!=NULL)
630 | {
631 | if(ma_val < subshell->elist->eval)
632 | {
633 | ma_val = subshell->elist->eval;
634 | }
635 |
636 | if(mi_val > subshell->elist->eval)
637 | {
638 | mi_val = subshell->elist->eval;
639 | }
640 |
641 | if(subshell->dcntr > 0)
642 | {
643 | shell->dcntr+=1;/*this is to tell us how many times dcntrs are high in subshells */
644 | }
645 | }
646 | };
647 |
648 | subshell = NULL;
649 | shell->max_val = ma_val;
650 | shell->min_val = mi_val;
651 |
652 | if(min_dcntr > shell->dcntr)
653 | min_dcntr = shell->dcntr;
654 |
655 | if(shell->dcntr > 0)
656 | threat =1;
657 | }
658 | };
659 |
660 | printf("min_dcntr = %d\n",min_dcntr);
661 | int ind_array[60];
662 | z =0;
663 | for(z; z<=59; z++)
664 | ind_array[z] = 200; /*this array is for storing the indices of root which have
665 | * lowest dcntr*/
666 |
667 | int c =0;
668 | z = 0;
669 |
670 | int safe_mode = 0;/*this is used so that we know that ult_vic_shell is
671 | *stored with values which destroy the imminent threat
672 | *FOR EXAMPLE: if a knight is going to kill our king,
673 | * the computer will kill the knight if
674 | * possible.*/
675 |
676 | for(z;z<=99;z++)
677 | {
678 | node* subshell = NULL;
679 | shell = root->array[z];
680 | if(shell!=NULL && shell->dcntr > 0 && safe_mode==0)/*dcntr can be > 1*/
681 | {
682 | int r=0;
683 | for(r;r<=99;r++)
684 | {
685 | subshell = shell->array[r];
686 | if(subshell!= NULL && subshell->dcntr > 0 && safe_mode==0)/*dcntr can be 0 or 1*/
687 | {
688 | int i = subshell->elist->i; /* this 'i' & 'j' represent the position of the */
689 | int j = subshell->elist->j; /* player 1 entity which can kill the king of the */
690 | /* so we try to match its position with the */
691 | /* position of a possible case back in the past */
692 | /* where the computer is capable of killing the entity*/
693 | node* inner_shell = NULL;
694 | int c =0;
695 | for(c;c<=99;c++)
696 | {
697 | inner_shell = root->array[c];
698 | if(inner_shell!=NULL && i ==inner_shell->elist->k && j == inner_shell->elist->l && inner_shell->dcntr==0)
699 | {
700 | ult_vic_shell = inner_shell;
701 | safe_mode =1;
702 | }
703 |
704 | };
705 | }
706 |
707 | };
708 | }
709 | };
710 |
711 | if(safe_mode==0 && threat==1)/*we have been unsuccessful in eliminating the entity*/
712 | { /*which is a threat.*/
713 | node* shell=NULL;
714 | int c=0;
715 | for(c;c<=99;c++)
716 | {
717 | shell = root->array[c];
718 | if(shell!=NULL && copy[0][shell->elist->i][shell->elist->j] == '9' && shell->dcntr==0)
719 | {
720 | ult_vic_shell = shell;/*here we seek for a possible move by computer in which */
721 | threat=0; /* the king can change its position leading to no checkmate*/
722 | }
723 | };
724 |
725 | }
726 |
727 | c=0;
728 | for(z=0; z<=99; z++)
729 | {
730 | shell = root->array[z];
731 | if(shell!= NULL)
732 | {
733 | if(shell->vcntr!= 0)
734 | ult_vic_shell = shell; /*this overwrites the value of ulti_vic_shell because*/
735 | /*we have finally got an instance where computer wins*/
736 |
737 | if(min_dcntr == shell->dcntr)
738 | {
739 | ind_array[c] = z;
740 | c++;
741 | }
742 | }
743 | };
744 |
745 | z =0;
746 | int ma_val = -100;
747 | for(z;z<=c;z++)
748 | {
749 | if(ind_array[z]!= 200)
750 | {
751 | shell = root->array[ind_array[z]];
752 | shell->max_val = ((shell->max_val + shell->min_val)*10)/2; /*10 is multiplied so that we have an integer */
753 | shell->max_val = shell->max_val + shell->elist->eval*100; /*shell->elist->eval gives the value of grid of */
754 | /*the node 1 and shell->max_val earlier containes*/
755 | /*the avg of the possible moves by the human player */
756 | /*shell->elist->eval is multiplied by 100 to give it
757 | *a higher edge in the final calculation*/
758 |
759 | if(ma_val < shell->max_val)
760 | ma_val = shell->max_val;
761 | }
762 | };
763 |
764 | int ind_rand[100];/*for storing those moves which have highest value*/
765 | z=0;
766 | for(z; z<100;z++)
767 | {
768 | ind_rand[z]=200;
769 | };
770 |
771 | int flag =0;
772 | int end_array=0;
773 | z =0;
774 | for(z; z<=c; z++)
775 | {
776 | if(ind_array[z]!= 200)
777 | {
778 | shell = root->array[ind_array[z]];
779 | if(ma_val == shell->max_val)
780 | {
781 | ind_rand[end_array] = ind_array[z];
782 | end_array++;
783 | /* int i1 =shell->elist->i;
784 | int j1 =shell->elist->j;
785 | char ent = copy[0][i1][j1];
786 |
787 | if(ent=='1' && flag==0)
788 | {
789 | vic_shell = shell;
790 | flag=1;
791 | }
792 |
793 | if(ent=='2' && flag==0)
794 | {
795 | vic_shell = shell;
796 | flag=1;
797 | }
798 |
799 | if(ent=='4' && flag==0)
800 | {
801 | vic_shell = shell;
802 | flag=1;
803 | }
804 |
805 | if(ent=='5' && flag==0)
806 | {
807 | vic_shell = shell;
808 | flag=1;
809 | }
810 |
811 | if(ent=='7' && flag==0)
812 | {
813 | vic_shell = shell;
814 | flag=1;
815 | }
816 |
817 | if(ent=='9' && flag==0)
818 | {
819 | vic_shell = shell;
820 | flag=1;
821 | }*/
822 | }
823 | }
824 | };
825 |
826 | printf("i am here\n"); /*produces a random number for selecting*/
827 | end_array--; /*a move amongst equiprobable choices*/
828 | srand(time(NULL));
829 | z=0;
830 | z = rand()%(end_array+1);
831 | vic_shell=root->array[ind_rand[z]];
832 | printf("i am there\n");
833 |
834 | if(ult_vic_shell!=NULL)
835 | {
836 | shell = ult_vic_shell;
837 | i1 = shell->elist->i;
838 | j1 = shell->elist->j;
839 | k1 = shell->elist->k;
840 | l1 = shell->elist->l;
841 | }
842 |
843 | else if(ult_vic_shell==NULL && vic_shell!=NULL)
844 | {
845 | shell = vic_shell;
846 | i1 = shell->elist->i;
847 | j1 = shell->elist->j;
848 | k1 = shell->elist->k;
849 | l1 = shell->elist->l;
850 | }
851 |
852 | init_ent = origin[0][i1][j1];
853 | printf("i1%d j1%d k1%d l1%d\n",i1,j1,k1,l1);
854 | z = compute(init_ent, i1, j1 ,k1, l1, 0, origin);
855 |
856 | FreeAll(root);
857 | return ;
858 | }
859 |
860 | void FreeAll(node* root) /*Returning the memory back to the heap*/
861 | { /*so as to avoid an overhead*/
862 | if(root!= NULL)
863 | {
864 | int z,c =0;
865 | for(z; z<=99; z++)
866 | {
867 | if(root->array[z] != NULL)
868 | c++;
869 | };
870 |
871 | if(c!= 0)
872 | {
873 | z =0;
874 | for(z;z<=99;z++)
875 | {
876 | FreeAll(root->array[z]);
877 | root->array[z] = NULL;
878 | };
879 | }
880 |
881 | else if(c == 0)
882 | {
883 | free(root->elist);
884 | free(root);
885 | }
886 | }
887 | }
888 |
--------------------------------------------------------------------------------
/src/chess3.c:
--------------------------------------------------------------------------------
1 | /*Copyright 2012 Parigyan Chandra Goyal & Sharat Shankar*/
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "AI.h"
9 |
10 | /* 1 = PAWN
11 | * 2 = KNIGHT
12 | * 4 = BISHOP
13 | * 5 = ROOK
14 | * 7 = QUEEN
15 | * 9 = KING*/
16 |
17 | int compute(char ent1,int i1,int j1,int k1,int l1, int cond, char chess[2][8][8]);
18 | void printing(char chp [2][8][8]);
19 |
20 |
21 | int main()
22 | {
23 | char chess[2][8][8]={
24 | {
25 | {'5','4','2','9','7','2','4','5'},
26 | {'1','1','1','1','1','1','1','1'},
27 | {' ',' ',' ',' ',' ',' ',' ',' '},
28 | {' ',' ',' ',' ',' ',' ',' ',' '},
29 | {' ',' ',' ',' ',' ',' ',' ',' '},
30 | {' ',' ',' ',' ',' ',' ',' ',' '},
31 | {'1','1','1','1','1','1','1','1'},
32 | {'5','4','2','7','9','2','4','5'},
33 | },
34 |
35 | {
36 | {'1','1','1','1','1','1','1','1'},
37 | {'1','1','1','1','1','1','1','1'},
38 | {' ',' ',' ',' ',' ',' ',' ',' '},
39 | {' ',' ',' ',' ',' ',' ',' ',' '},
40 | {' ',' ',' ',' ',' ',' ',' ',' '},
41 | {' ',' ',' ',' ',' ',' ',' ',' '},
42 | {'2','2','2','2','2','2','2','2'},
43 | {'2','2','2','2','2','2','2','2'},
44 | }
45 | };
46 |
47 |
48 | const char *ind[8][8]=
49 | {
50 | {"A1","A2","A3","A4","A5","A6","A7","A8"},
51 | {"B1","B2","B3","B4","B5","B6","B7","B8"},
52 | {"C1","C2","C3","C4","C5","C6","C7","C8"},
53 | {"D1","D2","D3","D4","D5","D6","D7","D8"},
54 | {"E1","E2","E3","E4","E5","E6","E7","E8"},
55 | {"F1","F2","F3","F4","F5","F6","F7","F8"},
56 | {"G1","G2","G3","G4","G5","G6","G7","G8"},
57 | {"H1","H2","H3","H4","H5","H6","H7","H8"},
58 | };
59 |
60 | int checker=1;
61 | int run = 108;
62 | while(run > 0)
63 | {
64 |
65 | printing(chess);
66 | char ent='0';
67 |
68 |
69 | /***************************************TAKING THE INPUT**********************************************/
70 | if(checker%2 ==0)
71 | {
72 | int loop =0; /********Delay Loop********/
73 | for(loop;loop<=1000000000;loop++) /******To enhance the gameplay and make it look more real******/
74 | {
75 | ;
76 | };
77 | AIfunc(chess);
78 | checker++;
79 | }
80 |
81 | else if(checker%2 !=0)
82 | {
83 | printf("PLAYER 1 TYPE IN YOUR MOVE: ");
84 |
85 |
86 | int whitesp = 0;
87 | int n =0;
88 | int ck =0;
89 | int ik = 0;
90 | int fk =0;
91 | char d = '\0';
92 | char lin1[100];
93 | char initv[100], finlv[100],help[100];
94 |
95 | int par = 0;
96 | for(par = 0; par<100; par++)
97 | {
98 | lin1[par] = '\0';
99 | initv[par] = '\0';
100 | help[par] = '\0';
101 | finlv[par] = '\0';
102 | }
103 |
104 | scanf(" %[^\n]s", lin1);
105 | int low=0;
106 | int len =strlen(lin1) - 1;
107 |
108 | for(low;low<=len;low++)
109 | {
110 | lin1[low]=toupper(lin1[low]);
111 | }
112 |
113 | for(n =0; n <=len ; n++)
114 | {
115 | if(lin1[n] == ' ' && n==0)
116 | continue;
117 |
118 | if(lin1[n] == ' ' && ((lin1[n-1] != ' ') && n!=0)){
119 | whitesp++;
120 | }
121 |
122 | if( whitesp == 0 && (n == 0 || lin1[n-1] == ' ') && lin1[n] != ' ')
123 | d = lin1[n];
124 |
125 | if(whitesp == 0 && lin1[n] != ' '){
126 | help[ck] = lin1[n];
127 | ck++;
128 | }
129 |
130 | if(whitesp == 1 && lin1[n] != ' '){
131 | initv[ik] = lin1[n];
132 | ik++;
133 | }
134 |
135 | if(whitesp == 2 && lin1[n] != ' '){
136 | finlv[fk] = lin1[n];
137 | fk++;
138 | }
139 |
140 | }
141 |
142 | /*
143 | printf("\n"); The comments have been left
144 | printf("%c \n", d); as such because people who are new
145 | printf("%s \n", lin1 ); to the source code can uncomment the
146 | printf("%sa\n", help); statements and understand the significance
147 | printf("%s \n", initv); and working of the variables.
148 | printf("%s \n", finlv);
149 |
150 | */
151 |
152 |
153 |
154 | /*************************************CALCULATING THE INDICES******************************************/
155 | char *quit = "QUIT";
156 | int dep = 30;
157 | dep = strcmp(help,quit);
158 | /* printf("dep%d",dep) */;
159 |
160 | char *display = "HELP";
161 | int ped =40;
162 | ped = strcmp(help, display);
163 |
164 | if(ped == 0)
165 | {
166 | printf("\n\n\n\n\n");
167 | printf("\t1 = PAWN\n\t2 = KNIGHT\n\t4 = BISHOP\n\t5 = ROOK\n\t7 = QUEEN\n\t9 = KING\n\n\n\n\n");
168 |
169 | printf("PRESS ENTER TO ESCAPE HELP:");
170 | char esc;
171 |
172 | if( (esc = getchar() ) == 'h' || (esc = getchar() )== 'H')
173 | continue;
174 |
175 | }
176 |
177 | if(dep == 0)
178 | {
179 | printf("ENTER Y TO QUIT:");
180 | char y;
181 |
182 | if( (y = getchar() ) == 'y' || (y = getchar() )== 'Y')
183 | goto END;
184 | }
185 |
186 |
187 | ent = d;
188 | int error=0;
189 | int i,j,k,l=0;
190 | int check;
191 |
192 | for (i=0;i<=7;i++)
193 | { check = 100;
194 |
195 | for (j=0;j<=7;j++)
196 | { check = strcmp(ind[i][j],initv);
197 | if (check==0)
198 | break;
199 | };
200 |
201 | if(check==0)
202 | break;
203 | }
204 |
205 |
206 | if(check!=0)
207 | {
208 | error++;
209 | printf("THERE IS NO INITIAL POSITION AS \"%s\"\n", initv);/*************ERROR ERROR*************/
210 | }
211 |
212 |
213 | for (k=0;k<=7;k++)
214 | { check = 100;
215 |
216 | for (l=0;l<=7;l++)
217 | { check = strcmp(ind[k][l],finlv);
218 | if (check==0)
219 | break;
220 | };
221 |
222 | if(check==0)
223 | break;
224 | }
225 |
226 | if(check!=0)
227 | {
228 | error++;
229 | printf("THERE IS NO FINAL POSITION AS \"%s\"\n", finlv);/*************ERROR ERROR*************/
230 | }
231 |
232 | char king = chess[0][k][l];
233 |
234 | if(checker%2 !=0 && chess[1][i][j]=='2')
235 | {
236 | error=1;
237 | printf("PLAYER 1 YOU CANNOT ACCESS ARMY OF PLAYER 2\n");
238 | }
239 |
240 | else if(checker%2 ==0 && chess[1][i][j]== '1')
241 | {
242 | error=1;
243 | printf("PLAYER 2 YOU CANNOT ACCESS ARMY OF PLAYER 1\n");
244 | }
245 |
246 |
247 | /* system("clear");*/
248 |
249 | if(error ==0)
250 | {
251 | error = compute(ent,i,j,k,l,0,chess);
252 | }
253 |
254 | if(error==0)
255 | {
256 | checker++;
257 | /* printf("checker:%d error a:%d\n",checker,error);*/
258 | }
259 |
260 | else if( error !=0)
261 | {
262 | /* printf("checker:%d error b:%d\n",checker,error);*/
263 | }
264 |
265 | if(king=='9' && error ==0 && checker%2 == 0)
266 | {printf("PLAYER 1 HAS WON THE GAME\n");
267 | break;
268 | }
269 |
270 | if(king=='9' && error ==0 && checker%2 != 0)
271 | {printf("PLAYER 2 HAS WON THE GAME\n");
272 | break;
273 | }
274 |
275 | /* printf("checker:%d error c:%d\n",checker,error);*/
276 |
277 | }
278 |
279 | }
280 | END:
281 | return 0;
282 | }
283 |
284 |
285 |
286 |
287 |
288 |
289 | int compute(char ent1,int i1,int j1,int k1,int l1, int cond, char chess[2][8][8])
290 | /*cond means condition for stopping printf statements
291 | *when cond ==1 do not print */
292 | {
293 | int q=0;
294 | switch(ent1)
295 | {
296 | case '1':/************************************* RULES FOR PAWN************************************************/
297 | switch(chess[0][i1][j1])
298 | {
299 | case'1': /*CHECKS WHETHER PAWN IS AVAILABLE OR NOT*/
300 | {
301 | if (chess[1][i1][j1]=='1')/*FOR UPPER PLAYER*/
302 | {
303 | if( (chess[1][k1][l1]== '1' && l1==j1) || (i1 == 1 && chess[1][2][j1] == '1' && j1==l1 ) )
304 | {
305 | q=1;
306 | if(cond == 0)
307 | printf("PLAYER 1 YOU CAN NOT OVERRUN YOUR ARMY\n");/************ERROR ERROR***************/
308 | }
309 |
310 | else if (chess[1][2][j1] == '2')
311 | {
312 | q=1;
313 | if(cond == 0)
314 | printf("PLAYER 1 PAWN CAN'T JUMP OVER THE ENEMY\n");/************ERROR ERROR***************/
315 | }
316 |
317 |
318 | else
319 | {
320 | if ( i1==1 && (k1==i1+1 || k1==i1+2) && l1==j1 )/*WHEN THE PAWN IS AT ITS INITIAL POSITION*/
321 | {
322 | if(chess[1][k1][l1] == ' ')
323 | {
324 | chess[0][k1][l1]=chess[0][i1][j1];
325 | chess[1][k1][l1]=chess[1][i1][j1];
326 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
327 | }
328 |
329 | else
330 | {
331 | q=1;
332 | if(cond == 0)
333 | printf("ILLEGAL MOVE\n");/************ERROR ERROR***************/
334 |
335 | }
336 |
337 | }
338 |
339 |
340 | else if (k1==i1+1 && l1==j1)/*WHEN PAWN IS ANYWHERE ELSE*/
341 | {
342 | if(chess[1][k1][l1] == ' ')
343 | {
344 | chess[0][k1][l1]=chess[0][i1][j1];
345 | chess[1][k1][l1]=chess[1][i1][j1];
346 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
347 | }
348 |
349 | else
350 | {
351 | q=1;
352 | if(cond == 0)
353 | printf("ILLEGAL MOVE\n");/************ERROR ERROR***************/
354 | }
355 |
356 | }
357 |
358 | else if (k1 == i1+1 && ( l1==j1-1 || l1==j1+1))/*WHEN PAWN IS ANYWHERE ELSE*/
359 | {
360 | if(chess[1][k1][l1] == '2')
361 | {
362 | chess[0][k1][l1]=chess[0][i1][j1];
363 | chess[1][k1][l1]=chess[1][i1][j1];
364 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
365 | }
366 |
367 | else
368 | {
369 | q=1;
370 | if(cond == 0)
371 | printf("ILLEGAL MOVE\n");/************ERROR ERROR***************/
372 |
373 | }
374 | }
375 |
376 |
377 | else
378 | { q=1;
379 | if(cond == 0)
380 | printf("PLAYER 1 ILLEGAL MOVE\n");/************ERROR ERROR***************/
381 |
382 | }
383 |
384 | }
385 |
386 | }
387 |
388 | else if (chess[1][i1][j1]=='2')/*FOR LOWER PLAYER*/
389 | {
390 | if( (chess[1][k1][l1]== '2' && l1 == j1) || (i1==6 && chess[1][5][j1] == '2' && j1==l1 ) )
391 | {
392 | q=1;
393 | if(cond == 0)
394 | printf("PLAYER 2 YOU CAN NOT OVERRUN YOUR ARMY\n");/************ERROR ERROR***************/
395 | }
396 |
397 | else if (chess[1][5][j1] == '1')
398 | {
399 | q=1;
400 | if(cond == 0)
401 | printf("PLAYER 2 PAWN CAN'T JUMP OVER THE ENEMY\n");/************ERROR ERROR***************/
402 | }
403 |
404 |
405 | else
406 | {
407 | if ( i1==6 && (k1==i1-1 || k1==i1-2) && l1==j1 )/*WHEN THE PAWN IS AT ITS INITIAL POSITION*/
408 | {
409 | if(chess[1][k1][l1] == ' ')
410 | {
411 | chess[0][k1][l1]=chess[0][i1][j1];
412 | chess[1][k1][l1]=chess[1][i1][j1];
413 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
414 | }
415 |
416 | else
417 | {
418 | q=1;
419 | if(cond == 0)
420 | printf("ILLEGAL MOVE\n");/************ERROR ERROR***************/
421 |
422 | }
423 |
424 | }
425 |
426 | else if (k1==i1-1 && l1==j1)/*WHEN PAWN IS ANYWHERE ELSE*/
427 | {
428 | if(chess[1][k1][l1] == ' ')
429 | {
430 | chess[0][k1][l1]=chess[0][i1][j1];
431 | chess[1][k1][l1]=chess[1][i1][j1];
432 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
433 | }
434 |
435 | else
436 | {
437 | q=1;
438 | if(cond == 0)
439 | printf("ILLEGAL MOVE\n");/************ERROR ERROR***************/
440 | }
441 |
442 | }
443 |
444 | else if (k1 == i1-1 && ( l1==j1-1 || l1==j1+1))/*WHEN PAWN IS ANYWHERE ELSE*/
445 | {
446 | if(chess[1][k1][l1] == '1')
447 | {
448 | chess[0][k1][l1]=chess[0][i1][j1];
449 | chess[1][k1][l1]=chess[1][i1][j1];
450 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
451 | }
452 |
453 | else
454 | {
455 | q=1;
456 | if(cond == 0)
457 | printf("ILLEGAL MOVE\n");/************ERROR ERROR***************/
458 | }
459 | }
460 |
461 |
462 | else
463 | { q=1;
464 | if(cond == 0)
465 | printf("YOUR MOVE IS ILLEGAL\n");/************ERROR ERROR***************/
466 | }
467 |
468 | }
469 | }
470 |
471 | break;}/*BREAK OF CASE 1*/
472 |
473 |
474 |
475 | default:
476 | { q=1;
477 | if(cond == 0)
478 | printf("PAWN IS NOT AT THE SPECIFIED POSITION\n");/************ERROR ERROR***************/
479 | }
480 |
481 | }break;/**********************************************END OF PAWN***********************************/
482 |
483 |
484 |
485 | case '2' : /******************************************RULES FOR KNIGHT*********************************/
486 | switch(chess[0][i1][j1])
487 | { case '2':/***CHECKS WHETHER KNIGHT IS THERE OR NOT******/
488 | {
489 | if( (k1 == i1+2 || k1 == i1-2) && (k1>=0 && k1<=7) && (l1 == j1+1 || l1 == j1-1) && (l1>=0 && l1<=7) )
490 | {/*******FOR VERTICAL MOVEMENT*************/
491 | if(chess[1][i1][j1] == chess[1][k1][l1] )
492 | {
493 | q =1;
494 | if(cond == 0)
495 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
496 | }
497 |
498 |
499 | else
500 | {
501 | chess[0][k1][l1]=chess[0][i1][j1];
502 | chess[1][k1][l1]=chess[1][i1][j1];
503 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
504 |
505 | }
506 |
507 | }
508 |
509 |
510 | else if( (k1 == i1+1 || k1 == i1-1) && (k1>=0 && k1<=7) && (l1 == j1+2 || l1== j1-2) && (l1>=0 && l1<=7) )
511 | {/*********FOR HORIZONTAL MOVEMENT***********/
512 | if(chess[1][i1][j1] == chess[1][k1][l1] )
513 | {
514 | q =1;
515 | if(cond == 0)
516 | printf("YOU CANNOT OVERRUN YOUR ARMY\n");/************ERROR ERROR***************/
517 | }
518 |
519 |
520 | else
521 | {
522 | chess[0][k1][l1]=chess[0][i1][j1];
523 | chess[1][k1][l1]=chess[1][i1][j1];
524 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
525 |
526 | }
527 |
528 | }
529 |
530 | else
531 | {
532 | q =1;
533 | if(cond == 0)
534 | printf("YOUR KNIGHT MOVE IS ILLEGAL\n");/************ERROR ERROR***************/
535 | }
536 |
537 | break;}/*BREAK OF CASE 2*/
538 |
539 |
540 | default:
541 | {
542 | q =1;
543 | if(cond == 0)
544 | printf("KNIGHT IS NOT AT THE SPECIFIED POSITION\n");/************ERROR ERROR***************/
545 | }
546 |
547 | }break;/***********************************END OF KNIGHT********************************************/
548 |
549 |
550 |
551 | case'3':/***************************************RULE OF NO-ENTITY 3 ****************************************/
552 | {
553 | q =1;
554 | if(cond == 0)
555 | printf("THERE IS NO ENTITY 3\n");/************ERROR ERROR***************/
556 |
557 | }break;/***********************************END OF NO-ENTITY 3 ****************************************/
558 |
559 |
560 |
561 | case'4':/****************************************RULES OF BISHOP****************************************/
562 | switch(chess[0][i1][j1])
563 | { case '4': /***CHECKS WHETHER BISHOP IS THERE OR NOT******/
564 | { int bis;
565 | bis = k1 - i1;
566 | /* printf("%d\n",bis);*/
567 |
568 | if(l1 == bis + j1 || l1 == (-1)*bis + j1)
569 | { int var = bis - 1;
570 |
571 |
572 | if(bis < 0){
573 | var = (-1)*bis -1;
574 | }
575 |
576 | for(var; var >0; var--)
577 | {
578 | if(bis > 0 && l1 > j1)
579 | {
580 | if(chess[1][i1+var][j1+var] == ' ')
581 | continue;
582 |
583 | else
584 | {
585 | q =1;
586 | if(cond == 0)
587 | printf("BISHOP CAN'T JUMP\n");/*******ERROR ERROR*********/
588 | break;
589 | }
590 | }
591 |
592 | else if(bis > 0 && l1 < j1)
593 | {
594 | if(chess[1][i1+var][j1-var] == ' ')
595 | continue;
596 |
597 | else
598 | {
599 | q =1;
600 | if(cond == 0)
601 | printf("BISHOP CAN'T JUMP\n");/*******ERROR ERROR*********/
602 | break;
603 | }
604 | }
605 |
606 | else if(bis < 0 && l1 > j1)
607 | {
608 | if(chess[1][i1-var][j1+var] == ' ')
609 | continue;
610 |
611 | else
612 | {
613 | q =1;
614 | if(cond == 0)
615 | printf("BISHOP CAN'T JUMP\n");/*******ERROR ERROR*********/
616 | break;
617 |
618 | }
619 | }
620 |
621 | if(bis < 0 && l1 < j1)
622 | {
623 | if(chess[1][i1-var][j1-var] == ' ')
624 | continue;
625 |
626 | else
627 | {
628 | q =1;
629 | if(cond == 0)
630 | printf("BISHOP CAN'T JUMP\n");/*******ERROR ERROR*********/
631 | break;
632 |
633 | }
634 | }
635 |
636 | }
637 |
638 |
639 | if(chess[1][i1][j1] == chess[1][k1][l1])
640 | {
641 | q =1;
642 | var = 1;
643 | if(cond == 0)
644 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
645 | }
646 |
647 |
648 | if(var == 0)
649 | {
650 | chess[0][k1][l1]=chess[0][i1][j1];
651 | chess[1][k1][l1]=chess[1][i1][j1];
652 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
653 |
654 | }
655 |
656 | }
657 |
658 | else
659 | {
660 | q =1;
661 | if(cond == 0)
662 | printf("INVALID MOVE OF BISHOP\n"); /************ERROR ERROR***************/
663 | }
664 |
665 |
666 |
667 |
668 |
669 | break;}/***END OF CASE 4 ***********/
670 |
671 | default:
672 | { q =1;
673 | if(cond == 0)
674 | printf("BISHOP IS NOT AT THE SPECIFIED POSITION\n"); /************ERROR ERROR***************/
675 | }
676 |
677 |
678 | }break; /*********************************END OF BISHOP**************************************/
679 |
680 |
681 |
682 | case '5' : /******************************************RULES FOR ROOK*********************************/
683 | switch(chess[0][i1][j1])
684 | { case '5':/***CHECKS WHETHER ROOK IS THERE OR NOT******/
685 | {
686 | if(l1==j1)
687 | {/*******FOR VERTICAL MOVEMENT*************/
688 | int rip;
689 | rip = k1 - i1;
690 | int vap = rip-1;
691 |
692 | if(rip <0)
693 | {
694 | vap = (-1)*rip-1;
695 | }
696 |
697 | for(vap; vap>0; vap--)
698 | {
699 | if(rip>0)
700 | {
701 | if(chess[1][i1 + vap][j1] == ' ')
702 | continue;
703 |
704 | else
705 | {
706 | q =1;
707 | if(cond == 0)
708 | printf("ROOK CAN'T JUMP\n");/*******ERROR ERROR*********/
709 | break;
710 | }
711 | }
712 |
713 | else if(rip<0)
714 | {
715 | if(chess[1][i1 - vap][j1] == ' ')
716 | continue;
717 |
718 | else
719 | {
720 | q =1;
721 | if(cond == 0)
722 | printf("ROOK CAN'T JUMP\n");/*******ERROR ERROR*********/
723 | break;
724 | }
725 | }
726 |
727 | }
728 |
729 | if(chess[1][i1][j1] == chess[1][k1][l1] )
730 | {
731 | q =1;
732 | vap = 1;
733 | if(cond == 0)
734 | printf("YOU CANNOT OVERRUN YOUR ARMY\n");/************ERROR ERROR***************/
735 | }
736 |
737 |
738 | if(vap ==0)
739 | {
740 | chess[0][k1][l1]=chess[0][i1][j1];
741 | chess[1][k1][l1]=chess[1][i1][j1];
742 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
743 |
744 | }
745 |
746 | }
747 |
748 | else if(k1==i1)
749 | {/******FOR HORIZONTAL MOVEMENT*************/
750 | int rhip;
751 | rhip = l1 - j1;
752 | int vhap = rhip -1 ;
753 |
754 | if(rhip <0)
755 | {
756 | vhap = (-1)*rhip-1;
757 | }
758 |
759 | for(vhap; vhap>0; vhap--)
760 | {
761 | if(rhip>0)
762 | {
763 | if(chess[1][i1][j1 + vhap] == ' ')
764 | continue;
765 |
766 | else
767 | {
768 | q =1;
769 | if(cond == 0)
770 | printf("ROOK CAN'T JUMP\n");/*******ERROR ERROR*********/
771 | break;
772 | }
773 | }
774 |
775 | else if(rhip<0)
776 | {
777 | if(chess[1][i1][j1 - vhap] == ' ')
778 | continue;
779 |
780 | else
781 | {
782 | q =1;
783 | if(cond == 0)
784 | printf("ROOK CAN'T JUMP\n");/*******ERROR ERROR*********/
785 | break;
786 | }
787 | }
788 |
789 |
790 | }
791 |
792 | if(chess[1][i1][j1] == chess[1][k1][l1] )
793 | {
794 | q =1;
795 | vhap = 1;
796 | if(cond == 0)
797 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
798 | }
799 |
800 |
801 | if(vhap ==0)
802 | {
803 | chess[0][k1][l1]=chess[0][i1][j1];
804 | chess[1][k1][l1]=chess[1][i1][j1];
805 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
806 |
807 | }
808 |
809 | }
810 |
811 | else
812 | {
813 | q =1;
814 | if(cond == 0)
815 | printf("INVALID MOVE FOR ROOK\n"); /*******ERROR ERROR*********/
816 |
817 | }
818 |
819 |
820 | break;}/***END OF CASE 5 ***********/
821 |
822 |
823 | default:
824 | {
825 | q =1;
826 | if(cond == 0)
827 | printf("ROOK IS NOT AT THE SPECIFIED POSITION\n");/***********ERROR ERROR**************/
828 | }
829 |
830 | }break; /*******************************END OF ROOK**************************************/
831 |
832 |
833 |
834 |
835 | case'6':/***************************************RULE OF NO-ENTITY 6 ****************************************/
836 | {
837 | q =1;
838 | if(cond == 0)
839 | printf("THERE IS NO ENTITY 6\n");/************ERROR ERROR***************/
840 |
841 | }break;/***********************************END OF NO-ENTITY 6 ****************************************/
842 |
843 |
844 |
845 |
846 |
847 | case'7':/***************************************RULE OF QUEEN ****************************************/
848 | switch(chess[0][i1][j1])/***CHECKS WHETHER QUEEN IS THERE OR NOT******/
849 | { case'7':
850 | {
851 |
852 | if(l1==j1)
853 | {/*******FOR VERTICAL MOVEMENT*************/
854 | int rip;
855 | rip = k1 - i1;
856 | int vap = rip-1;
857 |
858 | if(rip <0)
859 | {
860 | vap = (-1)*rip-1;
861 | }
862 |
863 | for(vap; vap>0; vap--)
864 | {
865 | if(rip>0)
866 | {
867 | if(chess[1][i1 + vap][j1] == ' ')
868 | continue;
869 |
870 | else
871 | {
872 | q =1;
873 | if(cond == 0)
874 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
875 | break;
876 | }
877 | }
878 |
879 | else if(rip<0)
880 | {
881 | if(chess[1][i1 - vap][j1] == ' ')
882 | continue;
883 |
884 | else
885 | {
886 | q =1;
887 | if(cond == 0)
888 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
889 | break;
890 | }
891 | }
892 |
893 | }
894 |
895 | if(chess[1][i1][j1] == chess[1][k1][l1] )
896 | {
897 | q =1;
898 | vap = 1;
899 | if(cond == 0)
900 | printf("YOU CANNOT OVERRUN YOUR ARMY\n");/************ERROR ERROR***************/
901 | }
902 |
903 |
904 | if(vap ==0)
905 | {
906 | chess[0][k1][l1]=chess[0][i1][j1];
907 | chess[1][k1][l1]=chess[1][i1][j1];
908 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
909 |
910 | }
911 |
912 | }
913 |
914 | else if(k1==i1)
915 | {/******FOR HORIZONTAL MOVEMENT*************/
916 | int rhip;
917 | rhip = l1 - j1;
918 | int vhap = rhip -1 ;
919 |
920 | if(rhip <0)
921 | {
922 | vhap = (-1)*rhip-1;
923 | }
924 |
925 | for(vhap; vhap>0; vhap--)
926 | {
927 | if(rhip>0)
928 | {
929 | if(chess[1][i1][j1 + vhap] == ' ')
930 | continue;
931 |
932 | else
933 | {
934 | q =1;
935 | if(cond == 0)
936 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
937 | break;
938 | }
939 | }
940 |
941 | else if(rhip<0)
942 | {
943 | if(chess[1][i1][j1 - vhap] == ' ')
944 | continue;
945 |
946 | else
947 | {
948 | q =1;
949 | if(cond == 0)
950 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
951 | break;
952 | }
953 | }
954 |
955 |
956 | }
957 |
958 | if(chess[1][i1][j1] == chess[1][k1][l1] )
959 | {
960 | q =1;
961 | vhap = 1;
962 | if(cond == 0)
963 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
964 | }
965 |
966 |
967 | if(vhap ==0)
968 | {
969 | chess[0][k1][l1]=chess[0][i1][j1];
970 | chess[1][k1][l1]=chess[1][i1][j1];
971 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
972 |
973 | }
974 |
975 | }
976 |
977 |
978 | else
979 | { int bis;
980 | bis = k1 - i1;
981 | /*printf("%d\n",bis);*/
982 |
983 | if(l1 == bis + j1 || l1 == (-1)*bis + j1)
984 | { int var = bis - 1;
985 |
986 |
987 | if(bis < 0){
988 | var = (-1)*bis -1;
989 | }
990 |
991 | for(var; var >0; var--)
992 | {
993 | if(bis > 0 && l1 > j1)
994 | {
995 | if(chess[1][i1+var][j1+var] == ' ')
996 | continue;
997 |
998 | else
999 | {
1000 | q =1;
1001 | if(cond == 0)
1002 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
1003 | break;
1004 | }
1005 | }
1006 |
1007 | else if(bis > 0 && l1 < j1)
1008 | {
1009 | if(chess[1][i1+var][j1-var] == ' ')
1010 | continue;
1011 |
1012 | else
1013 | {
1014 | q =1;
1015 | if(cond == 0)
1016 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
1017 | break;
1018 | }
1019 | }
1020 |
1021 | else if(bis < 0 && l1 > j1)
1022 | {
1023 | if(chess[1][i1-var][j1+var] == ' ')
1024 | continue;
1025 |
1026 | else
1027 | {
1028 | q =1;
1029 | if(cond == 0)
1030 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
1031 | break;
1032 |
1033 | }
1034 | }
1035 |
1036 | if(bis < 0 && l1 < j1)
1037 | {
1038 | if(chess[1][i1-var][j1-var] == ' ')
1039 | continue;
1040 |
1041 | else
1042 | {
1043 | q =1;
1044 | if(cond == 0)
1045 | printf("QUEEN CAN'T JUMP\n");/*******ERROR ERROR*********/
1046 | break;
1047 |
1048 | }
1049 | }
1050 |
1051 | }
1052 |
1053 |
1054 | if(chess[1][i1][j1] == chess[1][k1][l1])
1055 | {
1056 | q =1;
1057 | var = 1;
1058 | if(cond == 0)
1059 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
1060 | }
1061 |
1062 |
1063 | if(var == 0)
1064 | {
1065 | chess[0][k1][l1]=chess[0][i1][j1];
1066 | chess[1][k1][l1]=chess[1][i1][j1];
1067 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
1068 |
1069 | }
1070 |
1071 | }
1072 |
1073 | else
1074 | {
1075 | q =1;
1076 | if(cond == 0)
1077 | printf("INVALID MOVE FOR QUEEN\n");/*******ERROR ERROR*********/
1078 | }
1079 |
1080 | }
1081 |
1082 |
1083 | break;}/********END OF CASE 7 ************/
1084 |
1085 |
1086 |
1087 | default:
1088 | {
1089 | q =1;
1090 | if(cond == 0)
1091 | printf("QUEEN IS NOT AT THE SPECIFIED POSITION\n");/***********ERROR ERROR**************/
1092 | }
1093 |
1094 | }break;/***********************************END OF QUEEN ****************************************/
1095 |
1096 |
1097 |
1098 |
1099 | case'9':/***************************************RULE OF KING ****************************************/
1100 | switch(chess[0][i1][j1])/***CHECKS WHETHER QUEEN IS THERE OR NOT******/
1101 | {case'9':
1102 | {
1103 |
1104 | if( (k1 == i1 + 1 || k1 == i1-1) && (l1 == j1 || l1==j1-1 || l1 == j1+1) )
1105 | {
1106 |
1107 | if(chess[1][i1][j1] == chess[1][k1][l1])
1108 | {
1109 | q =1;
1110 | if(cond == 0)
1111 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
1112 | }
1113 |
1114 |
1115 | else
1116 | {
1117 | chess[0][k1][l1]=chess[0][i1][j1];
1118 | chess[1][k1][l1]=chess[1][i1][j1];
1119 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
1120 |
1121 | }
1122 |
1123 |
1124 | }
1125 |
1126 | else if( (k1 == i1) && (l1==j1-1 || l1 == j1+1) )
1127 | {
1128 |
1129 | if(chess[1][i1][j1] == chess[1][k1][l1])
1130 | {
1131 | q =1;
1132 | if(cond == 0)
1133 | printf("YOU CANNOT OVERRUN YOUR ARMY\n"); /************ERROR ERROR***************/
1134 | }
1135 |
1136 |
1137 | else
1138 | {
1139 | chess[0][k1][l1]=chess[0][i1][j1];
1140 | chess[1][k1][l1]=chess[1][i1][j1];
1141 | chess[0][i1][j1]=chess[1][i1][j1]=' ';
1142 |
1143 | }
1144 |
1145 |
1146 | }
1147 |
1148 |
1149 | else
1150 | {
1151 | q =1;
1152 | if(cond == 0)
1153 | printf("ILLEGAL MOVE FOR KING\n"); /************ERROR ERROR***************/
1154 | }
1155 |
1156 |
1157 |
1158 | break;}/********END OF CASE 9 ************/
1159 |
1160 |
1161 | default:
1162 | {
1163 | q =1;
1164 | if(cond == 0)
1165 | printf("KING IS NOT AT THE SPECIFIED POSITION\n");/***********ERROR ERROR**************/
1166 | }
1167 |
1168 |
1169 | }break;/***********************************END OF KING **************************************/
1170 |
1171 |
1172 | default:{
1173 | if(cond == 0)
1174 | printf("OTHER THAN THE ALLOWED NUMBERS , CHARACTERS NOT ALLOWED AS ENTITY\nLOOK INTO HELP\n");/************ERROR ERROR***************/
1175 | }
1176 |
1177 | }/*****************END OF SWITCH(ENTITY)************************/
1178 |
1179 | return q;
1180 |
1181 | }/*END OF COMPUTE*/
1182 |
1183 |
1184 |
1185 | void printing(char chp [2][8][8])
1186 | {
1187 | int a,b;
1188 | a =b =0;
1189 | int len, brd, buffer; /*len = length & brd = breadth*/
1190 | brd = len = 8;
1191 | printf("\n\n\n\n\n");
1192 |
1193 | printf(" \x1b[31m PLAYER \x1b[0m \n");
1194 | printf(" 1 2 3 4 5 6 7 8 \n");
1195 | buffer = len;
1196 | printf(" --");
1197 |
1198 | for(len; len>0; len--){
1199 | printf("-------");
1200 | }
1201 |
1202 | printf("\n");
1203 | len = buffer;
1204 |
1205 | int i;
1206 | char alpha = 'A';
1207 | int vert = 0;
1208 | for(brd; brd>0; brd--)
1209 | {
1210 | a = 8 - brd;
1211 | i = 3;
1212 | if(i==3)
1213 | {
1214 | len = buffer;
1215 | int hor =0;
1216 |
1217 | printf(" |");
1218 | for(len; len>0; len--)
1219 | {
1220 | if( (hor+vert)%2 == 0 )
1221 | {
1222 | printf(" ");
1223 | }
1224 |
1225 | else if( (hor+vert)%2 != 0 )
1226 | {
1227 | printf("\u2588\u2588\u2588\u2588\u2588\u2588\u2588"); /* the characters inside the print statement*/
1228 | } /* are unicodes used for printing the chess grid*/
1229 | hor++;
1230 | }
1231 |
1232 | printf("|");
1233 | printf("\n");
1234 | i--;
1235 | }
1236 |
1237 | if(i==2)
1238 | {
1239 | len = buffer;
1240 | int hor =0;
1241 | printf(" %c |",alpha);
1242 |
1243 | for(len; len>0; len--)
1244 | {
1245 | b = 8 - len;
1246 | if( (hor+vert)%2 == 0 )
1247 | {
1248 | if(chp[1][a][b] == '1')
1249 | {
1250 | printf(" \x1b[31m%c\x1b[0m ", chp[0][a][b]); /*unicode*/
1251 | }
1252 |
1253 | if(chp[1][a][b] == ' ')
1254 | {
1255 | printf(" ");
1256 | }
1257 |
1258 | if(chp[1][a][b] == '2')
1259 | {
1260 | printf(" \x1b[32m%c\x1b[0m ", chp[0][a][b]); /*unicode*/
1261 | }
1262 | }
1263 |
1264 | else if( (hor+vert)%2 != 0 )
1265 | {
1266 | if(chp[1][a][b] =='1')
1267 | {
1268 | printf("\u2588\u2588\u258C\x1b[31m%c\x1b[0m\u2590\u2588\u2588", chp[0][a][b]);
1269 | }
1270 |
1271 | if(chp[1][a][b] =='2')
1272 | {
1273 | printf("\u2588\u2588\u258C\x1b[32m%c\x1b[0m\u2590\u2588\u2588", chp[0][a][b]);
1274 | }
1275 |
1276 | if(chp[1][a][b] ==' ')
1277 | {
1278 | printf("\u2588\u2588\u2588\u2588\u2588\u2588\u2588", chp[0][a][b]);
1279 | }
1280 |
1281 | }
1282 | hor++;
1283 |
1284 | }
1285 |
1286 | printf("| %c", alpha);
1287 | printf("\n");
1288 | i--;
1289 | }
1290 |
1291 | if(i==1)
1292 | {
1293 | len = buffer;
1294 | printf(" |");
1295 | int hor =0;
1296 |
1297 | for(len; len>0; len--)
1298 | {
1299 | if( (hor+vert)%2 == 0 )
1300 | {
1301 | printf(" ");
1302 | }
1303 |
1304 | else if( (hor+vert)%2 != 0 )
1305 | {
1306 | printf("\u2588\u2588\u2588\u2588\u2588\u2588\u2588");
1307 | }
1308 | hor++;
1309 |
1310 | }
1311 |
1312 | printf("|");
1313 | printf("\n");
1314 | i--;
1315 | }
1316 |
1317 | alpha++;
1318 | vert++;
1319 | }
1320 |
1321 |
1322 | len = buffer;
1323 | printf(" --");
1324 |
1325 | for(len; len>0; len--){
1326 | printf("-------");
1327 | }
1328 | printf("\n");
1329 |
1330 | printf(" 1 2 3 4 5 6 7 8 \n");
1331 | printf(" \x1b[32mCOMPUTER\x1b[0m \n\n");
1332 | printf(" '1'=>PAWN '2'=>KNIGHT '4'=>BISHOP\n '5'=>ROOK '7'=>QUEEN '9'=>KING");
1333 | printf("\n\n");
1334 |
1335 |
1336 | }
1337 |
--------------------------------------------------------------------------------