Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.22 KB | None | 0 0
  1. #include <time.h>
  2. #include <windows.h>
  3. #include <math.h>
  4. #include <conio.h>
  5. /*Used Variables c,k,n,char temp*/
  6. int k=0,n=0,g=2,xk=8,xK=1,r=0;
  7. char board[8][8],yk='E',yK='E',p,pr;
  8. int c=1;
  9. int x1,x2;
  10. char eaten1[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  11. char eaten2[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  12. char index1[8]={'A','B','C','D','E','F','G','H'};
  13. int index2[8]={1,2,3,4,5,6,7,8};
  14. int valid(int x1,char y1,int x2,char y2);
  15. int removecheckwhite (int x1,char y1,int x2,char y2);
  16. int removecheckblack(int x1,char y1,int x2,char y2);
  17. int checkTurns(int x1,char y1);
  18. int check1 (int xk,char yk,int xK,char yK);
  19. int checkwhite (int xk,char yk,int xK,char yK);
  20. int checkblack (int xk,char yk,int xK,char yK);
  21. int stalemateWhite(int);
  22. int stalemateBlack(int);
  23. char u1[100][2];
  24. int u2[100][4];
  25.  
  26.  
  27.  
  28.  
  29. void gotoxy(int x, int y)
  30. {
  31. COORD coord;
  32. coord.X = x;
  33. coord.Y = y;
  34. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  35. }
  36. void delay(unsigned int mseconds)
  37. {
  38. clock_t goal = mseconds + clock();
  39. while (goal > clock());
  40. }
  41. int main()
  42. {
  43. system("COLOR ");
  44. char y1,y2,mode,temp;
  45. /*Initializing the initial board*/
  46. for(int i=0;i<2;i++)
  47. {
  48. if(i==1)
  49. {
  50. for(int j=0;j<8;j++)
  51. {
  52. board[i][j]='P';
  53. }
  54. }
  55. else
  56. {
  57. for(int j=0;j<8;j++)
  58. {
  59. switch (j)
  60. {
  61. case 0: board[i][j]='R'; break;
  62. case 1: board[i][j]='N'; break;
  63. case 2: board[i][j]='B'; break;
  64. case 3: board[i][j]='Q'; break;
  65. case 4: board[i][j]='K'; break;
  66. case 5: board[i][j]='B'; break;
  67. case 6: board[i][j]='N'; break;
  68. case 7: board[i][j]='R'; break;
  69. }
  70. }
  71. }
  72. }
  73. for(int i=6;i<8;i++)
  74. {
  75. if(i==6)
  76. {
  77. for(int j=0;j<8;j++)
  78. {
  79. board[i][j]='p';
  80. }
  81. }
  82. else
  83. {
  84. for(int j=0;j<8;j++)
  85. {
  86. switch (j)
  87. {
  88. case 0: board[i][j]='r'; break;
  89. case 1: board[i][j]='n'; break;
  90. case 2: board[i][j]='b'; break;
  91. case 3: board[i][j]='q'; break;
  92. case 4: board[i][j]='k'; break;
  93. case 5: board[i][j]='b'; break;
  94. case 6: board[i][j]='n'; break;
  95. case 7: board[i][j]='r'; break;
  96. }
  97. }
  98. }
  99. }
  100. for(int i=2;i<6;i++)
  101. {
  102. for(int j=0;j<8;j++)
  103. {
  104. if ((i+j)%2==1)
  105. board[i][j]=' ';
  106. else
  107. board[i][j]=219;
  108. }
  109. }
  110. /*End of Assiging Values*/
  111. /*Printing the board at the begining*/
  112. printf("\n\n");
  113. printf(" Eaten Pieces Of Player (1) ");
  114. for(int i=0;i<16;i++)
  115. {
  116. printf("%c|",eaten1[i]);
  117. }
  118. printf("\n");
  119. printf(" Eaten Pieces Of Player (2) ");
  120. for(int i=0;i<16;i++)
  121. {
  122. printf("%c|",eaten2[i]);
  123. }
  124. printf("\n\n\n\n");
  125. printf(" Player {2} Black\n\n");
  126.  
  127. printf(" ");
  128. for(int i=0;i<8;i++)
  129. {
  130. printf(" .%c.",index1[i]);
  131. }
  132. printf("\n");
  133. for(int row=0;row<8;row++)
  134. {
  135. /* Draw the top line */
  136. printf(" ");
  137. for(int column=0;column<8;column++)
  138. printf(" ---");
  139. putchar('\n');
  140. /* Draw the squares */
  141. printf(" ");
  142. printf("%d",c++);
  143. for(int column=0;column<8;column++)
  144. {
  145. printf("| %c ",board[row][column]);
  146. }
  147. printf("|\n");
  148. }
  149. printf(" ");
  150. for(int column=0;column<8;column++)
  151. printf(" ---");
  152. printf("\n\n");
  153. printf(" Player {1} White ");
  154. printf("\n\n");
  155. /* End of Printing the board before playing*//*Printing the board before playing*//*Printing the board before playing*/
  156. printf("Enter the desired piece\n");
  157. scanf("%d %c",&x1,&y1);
  158. printf("Enter the desired destination\n");
  159. scanf("%d %c",&x2,&y2);
  160.  
  161. while((valid(x1,y1,x2,y2)==0 || checkTurns(x1,y1)==0)){
  162. printf("NOT VALID MOVE\n");
  163. printf("Enter the desired piece\n");
  164. scanf("%d %c",&x1,&y1);
  165. printf("Enter the desired destination\n");
  166. scanf("%d %c",&x2,&y2);}
  167. g++;
  168. x1=x1-1;
  169. x2=x2-1;
  170. y1=y1-65;
  171. y2=y2-65;
  172. u1[r][0]=board[x1][y1];
  173. u1[r][1]=board[x2][y2];
  174. u2[r][0]=x1;
  175. u2[r][1]=y1;
  176. u2[r][2]=x2;
  177. u2[r][3]=y2;
  178. r++;
  179. temp=board[x2][y2];
  180. board[x2][y2]=board[x1][y1];
  181. if((x1+y1)%2==1)
  182. {
  183. board[x1][y1]=' ';
  184. }
  185. else
  186. {
  187. board[x1][y1]=219;
  188. }
  189. /*Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooor*/
  190. for(;;)
  191. {
  192. here:
  193. system("cls");
  194. if(check1(xk,yk,xK,yK)!=0){printf("CHECK!");}
  195.  
  196. int c=1;
  197. /*Printing The Board At Every Move*/
  198. printf("\n\n");
  199. printf(" Eaten Pieces Of Player (1) ");
  200. for(int i=0;i<16;i++)
  201. {
  202. printf("%c|",eaten1[i]);
  203. }
  204. printf("\n");
  205. printf(" Eaten Pieces Of Player (2) ");
  206. for(int i=0;i<16;i++)
  207. {
  208. printf("%c|",eaten2[i]);
  209. }
  210. printf("\n\n\n\n");
  211. printf(" Player {2} Black\n\n");
  212.  
  213. printf(" ");
  214. for(int i=0;i<8;i++)
  215. {
  216. printf(" .%c.",index1[i]);
  217. }
  218. printf("\n");
  219. for(int row=0;row<8;row++)
  220. {
  221. /* Draw the top line */
  222. printf(" ");
  223. for(int column=0;column<8;column++)
  224. printf(" ---");
  225. putchar('\n');
  226. /* Draw the squares */
  227. printf(" ");
  228. printf("%d",c++);
  229. for(int column=0;column<8;column++)
  230. {
  231. printf("| %c ",board[row][column]);
  232. }
  233. printf("|\n");
  234. }
  235. printf(" ");
  236. for(int column=0;column<8;column++)
  237. printf(" ---");
  238. printf("\n\n");
  239. printf(" Player {1} White ");
  240. printf("\n\n");
  241. /*End of Printing The Board At Every Move*/
  242.  
  243.  
  244. if(checkblack(xk,yk,xK,yK)==1 && check1(xk,yk,xK,yK)==1)
  245. {
  246. printf("Player 1 wins\n");
  247. break;
  248.  
  249. }
  250. if(checkwhite(xk,yk,xK,yK)==1 && check1(xk,yk,xK,yK)==2)
  251. {
  252. printf("Player 2 wins\n");
  253. break;
  254. }
  255.  
  256. if(stalemateWhite(1)==1 || stalemateBlack(1)==1)
  257. {
  258. printf("IT'S A STALEMATE");
  259. break;
  260. }
  261. printf("To Move Press (M) , To Undo Press (U), To Redo Press (R)\n");
  262. mode=getch();
  263. if(mode=='U')
  264. {
  265. r--;
  266. x1=u2[r][0];
  267. y1=u2[r][1];
  268. x2=u2[r][2];
  269. y2=u2[r][3];
  270. board[x1][y1]=u1[r][0];
  271. board[x2][y2]=u1[r][1];
  272. if((board[x2][y2]>=97 && board[x2][y2]<=122))
  273. {
  274. k--;
  275. eaten1[k]=' ';
  276.  
  277. }
  278. if((board[x2][y2]>=65 && board[x2][y2]<=90))
  279. {
  280. n--;
  281. eaten2[n]=' ';
  282.  
  283. }
  284. g--;
  285.  
  286. goto here;
  287. }
  288. else if(mode=='R')
  289. {
  290. x1=u2[r][0];
  291. y1=u2[r][1];
  292. x2=u2[r][2];
  293. y2=u2[r][3];
  294. board[x2][y2]=u1[r][0];
  295. if((x1+y1)%2==1)
  296. {
  297. board[x1][y1]=' ';
  298. }
  299. else
  300. {
  301. board[x1][y1]=219;
  302. }
  303.  
  304.  
  305.  
  306. if((board[x2][y2]>=97 && board[x2][y2]<=122))
  307. {
  308.  
  309. eaten2[n]=u1[r][1];
  310. n--;
  311. }
  312. if((board[x2][y2]>=65 && board[x2][y2]<=90))
  313. {
  314.  
  315. eaten1[k]=u1[r][1];
  316. k--;
  317. }
  318.  
  319. r++;
  320.  
  321. goto here;
  322. }
  323. else
  324. {
  325.  
  326. printf("Enter the desired piece\n");
  327. scanf("%d %c",&x1,&y1);
  328. printf("Enter the desired destination\n");
  329. scanf("%d %c",&x2,&y2);
  330.  
  331. while(valid(x1,y1,x2,y2)==0 || checkTurns(x1,y1)==0){
  332. printf("NOT VALID MOVE\n");
  333. printf("Enter the desired piece\n");
  334. scanf("%d %c",&x1,&y1);
  335. printf("Enter the desired destination\n");
  336. scanf("%d %c",&x2,&y2);}
  337.  
  338. if(board[x1-1][y1-65]=='k'){xk=x2;
  339. yk=y2;}
  340. if(board[x1-1][y1-65]=='K'){xK=x2;
  341. yK=y2;}
  342.  
  343. //check(xk,yk,xK,yK);
  344. g++;
  345. x1=x1-1;
  346. x2=x2-1;
  347. y1=y1-65;
  348. y2=y2-65;
  349. u1[r][0]=board[x1][y1];
  350. u1[r][1]=board[x2][y2];
  351. u2[r][0]=x1;
  352. u2[r][1]=y1;
  353. u2[r][2]=x2;
  354. u2[r][3]=y2;
  355. r++;
  356.  
  357. /*Printing Eaten if Exist */
  358. if((board[x2][y2]>=97 && board[x2][y2]<=122))
  359. {
  360. eaten1[k]=board[x2][y2];
  361. k++;
  362. }
  363. if((board[x2][y2]>=65 && board[x2][y2]<=90))
  364. {
  365. eaten2[n]=board[x2][y2];
  366. n++;
  367. }
  368. /*Printing Eaten if Exist */
  369. temp=board[x2][y2];
  370. board[x2][y2]=board[x1][y1];
  371. /*PROMOTION*/
  372. if(board[x2][y2]=='p'&&x2==0){
  373. printf("Enter the first lower letter of the piece\n ");
  374. scanf("%c",&pr);
  375.  
  376. while(pr!='q'&&pr!='r'&&pr!='b'&&pr!='n'){
  377. printf("NOT AVAILABLE!\n");
  378. scanf("%c",&pr);
  379. board[x2][y2]=pr;}
  380. }
  381.  
  382. if(board[x2][y2]=='P'&&x2==7){
  383. printf("tab the first heigher letter of the piece\n ");
  384. scanf("%c",&pr);
  385.  
  386. while(pr!='Q'&&pr!='R'&&pr!='B'&&pr!='N'){
  387. printf("NOT AVAILABLE!\n");
  388. scanf("%c",&pr);
  389. }
  390.  
  391. board[x2][y2]=pr;}
  392.  
  393.  
  394.  
  395. if((x1+y1)%2==1)
  396. {
  397. board[x1][y1]=' ';
  398. }
  399. else
  400. {
  401. board[x1][y1]=219;
  402. }
  403.  
  404. /*black*/
  405. while(check1(xk,yk,xK,yK)==1 && (board[x2][y2]>=65 && board[x2][y2]<=90))
  406. {
  407. board[x1][y1]=board[x2][y2];
  408. board[x2][y2]=temp;
  409. if(board[x1][y1]=='k'){xk=x1+1;
  410. yk=y1+65;}
  411. if(board[x1][y1]=='K'){xK=x1+1;
  412. yK=y1+65;}
  413. printf("NOT VALID ..THE KING WILL DIE!\n");
  414. printf("Enter the desired piece\n");
  415. scanf("%d %c",&x1,&y1);
  416. printf("Enter the desired destination\n");
  417. scanf("%d %c",&x2,&y2);
  418.  
  419. if(board[x1-1][y1-65]=='k'){xk=x2;
  420. yk=y2;}
  421. if(board[x1-1][y1-65]=='K'){xK=x2;
  422. yK=y2;}
  423.  
  424. x1=x1-1;
  425. x2=x2-1;
  426. y1=y1-65;
  427. y2=y2-65;
  428. temp=board[x2][y2];
  429. board[x2][y2]=board[x1][y1];}
  430.  
  431.  
  432. }
  433. while(check1(xk,yk,xK,yK)==2 && (board[x2][y2]>=97 && board[x2][y2]<=122))
  434. {
  435. board[x1][y1]=board[x2][y2];
  436. board[x2][y2]=temp;
  437. if(board[x1][y1]=='k'){xk=x1+1;
  438. yk=y1+65;}
  439. if(board[x1][y1]=='K'){xK=x1+1;
  440. yK=y1+65;}
  441. printf("NOT VALID ..THE KING WILL DIE!\n");
  442. printf("Enter the desired piece\n");
  443. scanf("%d %c",&x1,&y1);
  444. printf("Enter the desired destination\n");
  445. scanf("%d %c",&x2,&y2);
  446.  
  447. if(board[x1-1][y1-65]=='k'){xk=x2;
  448. yk=y2;}
  449. if(board[x1-1][y1-65]=='K'){xK=x2;
  450. yK=y2;}
  451. x1=x1-1;
  452. x2=x2-1;
  453. y1=y1-65;
  454. y2=y2-65;
  455. temp=board[x2][y2];
  456. board[x2][y2]=board[x1][y1];}
  457.  
  458.  
  459.  
  460.  
  461.  
  462. if((x1+y1)%2==1)
  463. {
  464. board[x1][y1]=' ';
  465. }
  466. else
  467. {
  468. board[x1][y1]=219;
  469. }
  470. }
  471. return 0;
  472.  
  473. }
  474.  
  475.  
  476.  
  477.  
  478. int valid(int x1,char y1,int x2,char y2)
  479. {
  480. int condition=1,i,t1=0,t2=0; char tempo;
  481. y1=y1-65;
  482. y2=y2-65;
  483. x1=x1-1;
  484. x2=x2-1;
  485. char w[6]={'p','r','n','b','k','q'};
  486. char b[6]={'P','R','N','B','K','Q'};
  487. char c=219,tempq;
  488. if(board[x1][y1]=='b'||board[x1][y1]=='B'){
  489. if(abs(x2-x1)!=abs(y2-y1)){condition=0;}}
  490. else if(board[x1][y1]=='n'||board[x1][y1]=='N')
  491. {
  492. if(!((abs(x2-x1)==1&&abs(y2-y1)==2)||(abs(y2-y1)==1&&abs(x2-x1)==2))){condition=0;}
  493. }
  494. else if(board[x1][y1]=='r'||board[x1][y1]=='R')
  495. {
  496. if(x1!=x2&&y1!=y2){condition=0;}
  497. }
  498. else if(board[x1][y1]=='K'||board[x1][y1]=='k')
  499. {
  500. if(abs(x2-x1)>=2||abs(y2-y1)>=2){condition=0;}
  501. }
  502. else if(board[x1][y1]=='p'){
  503. if(!(x1-x2==1&&y1==y2&&(board[x2][y2]==' '||board[x2][y2]==c))){condition=0;}
  504. }
  505. else if(board[x1][y1]=='P'){
  506. if(!(x2-x1==1&&y1==y2&&(board[x2][y2]==' '||board[x2][y2]==c))){condition=0;}
  507. }
  508. else if(board[x1][y1]=='q'||board[x1][y1]=='Q')
  509. {
  510. if((abs(x2-x1)!=abs(y2-y1))&&(x1!=x2&&y1!=y2)){condition=0;}
  511. }
  512. for(i=0;i<6;i++){
  513. if(board[x1][y1]==w[i]){t1=1;}}
  514. for(i=0;i<6;i++){
  515. if(board[x2][y2]==w[i]){t2=1;}}
  516. if(t1==1&&t2==1){condition=0;}
  517. t1=0;
  518. t2=0;
  519. for(i=0;i<6;i++){
  520. if(board[x1][y1]==b[i]){t1=1;}}
  521. for(i=0;i<6;i++){
  522. if(board[x2][y2]==b[i]){t2=1;}}
  523. if(t1==1&&t2==1){condition=0;}
  524. if(board[x1][y1]==' '||board[x1][y1]==c){condition=0;}
  525. t1=0;
  526. t2=0;
  527. if(board[x1][y1]=='p'&&(x1-x2)==1&&abs(y2-y1)==1)
  528. {
  529. for(i=0;i<6;i++){
  530. if(board[x2][y2]==b[i]){condition=1;}}}
  531. if(board[x1][y1]=='P'&&(x2-x1)==1&&abs(y2-y1)==1)
  532. {
  533. for(i=0;i<6;i++){
  534. if(board[x2][y2]==w[i]){condition=1;}}}
  535. i=1,t1=0,t2=0;
  536. if((board[x1][y1]=='b'||board[x1][y1]=='B')&&condition==1)
  537. {
  538. if(x2>x1&&y2>y1){
  539. while(x1+i!=x2){
  540. if(board[x1+i][y1+i]!=' '&&board[x1+i][y1+i]!=c){t1=1;}
  541. i++;}
  542. if(t1==1){condition=0;}}
  543.  
  544. else if(x2>x1&&y2<y1){
  545. t1=0,i=1;
  546. while(x1+i!=x2){
  547. if(board[x1+i][y1-i]!=' '&&board[x1+i][y1-i]!=c){t1=1;}
  548. i++;}
  549. if(t1==1){condition=0;}}
  550.  
  551. else if(x2<x1&&y2<y1){
  552. t1=0,i=1;
  553. while(x1-i!=x2){
  554. if(board[x1-i][y1-i]!=' '&&board[x1-i][y1-i]!=c){t1=1;}
  555. i++;}
  556. if(t1==1){condition=0;}}
  557.  
  558. else if(x2<x1&&y2>y1){
  559. t1=0,i=1;
  560. while(x1-i!=x2){
  561. if(board[x1-i][y1+i]!=' '&&board[x1-i][y1+i]!=c){t1=1;}
  562. i++;}
  563. if(t1==1){condition=0;}}
  564. t1=0,i=1;}
  565. if((board[x1][y1]=='r'||board[x1][y1]=='R')&&condition==1){
  566. if(x2>x1){
  567. i=1,t1=0;
  568. while(x1+i!=x2){
  569. if(board[x1+i][y1]!=' '&&board[x1+i][y1]!=c){t1=1;}
  570. i++;}
  571. if(t1==1){condition=0;}}
  572. if(x2<x1){
  573. i=1,t1=0;
  574. while(x1-i!=x2){
  575. if(board[x1-i][y1]!=' '&&board[x1-i][y1]!=c){t1=1;}
  576. i++;}
  577. if(t1==1){condition=0;}}
  578. if(y2>y1){
  579. i=1,t1=0;
  580. while(y1+i!=y2){
  581. if(board[x1][y1+i]!=' '&&board[x1][y1+i]!=c){t1=1;}
  582. i++;}
  583. if(t1==1){condition=0;}}
  584. if(y2<y1){
  585. i=1,t1=0;
  586. while(y1-i!=y2){
  587. if(board[x1][y1-i]!=' '&&board[x1][y1-i]!=c){t1=1;}
  588. i++;}
  589. if(t1==1){condition=0;}}}
  590. if(board[x1][y1]=='q'&&condition==1){
  591. if(abs(x2-x1)==abs(y2-y1)){
  592. tempq=board[x1][y1];
  593. board[x1][y1]='b';
  594. if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
  595. board[x1][y1]=tempq;}
  596. else {
  597. tempq=board[x1][y1];
  598. board[x1][y1]='r';
  599. if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
  600. board[x1][y1]=tempq;}}
  601. if(board[x1][y1]=='Q'&&condition==1){
  602. if(abs(x2-x1)==abs(y2-y1)){
  603. tempq=board[x1][y1];
  604. board[x1][y1]='B';
  605. if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
  606. board[x1][y1]=tempq;}
  607. else {
  608. tempq=board[x1][y1];
  609. board[x1][y1]='R';
  610. if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
  611. board[x1][y1]=tempq;}}
  612.  
  613. if(x1>=8||x1<0||y1>=8||y1<0||x2>=8||x2<0||y2>=8||y2<0){condition=0;}
  614.  
  615. if(board[x1][y1]=='P'&&condition==0){
  616. if((x2-x1)==2&&x1==1&&y1==y2&&(board[x2][y2]==' '||board[x2][y2]==c)&&(board[x1+1][y2]==' '||board[x1+1][y2]==c)){condition=1;}}
  617.  
  618. if(board[x1][y1]=='p'&&condition==0){
  619. if((x1-x2)==2&&x1==6&&y1==y2&&(board[x2][y2]==' '||board[x2][y2]==c)&&(board[x2+1][y2]==' '||board[x2+1][y2]==c)){condition=1;}}
  620. if(x1==x2&&y1==y2){condition=0;}
  621.  
  622.  
  623.  
  624.  
  625. /* if(board[x1][y1]=='P'&&condition==1&&y1==y2)
  626. {
  627. if(((x2-x1)==1)&&(board[x2][y2]!=' '&&board[x2][y2]!=c)){condition=0;}
  628. }
  629.  
  630. if(board[x1][y1]=='p'&&condition==1&&y1==y2)
  631. {
  632. if(((x1-x2)==1)&&(board[x2][y2]!=' '&&board[x2][y2]!=c)){condition=0;}
  633. }*/
  634. if((board[x1][y1]=='k')&& (check1(x2+1,y2+65,xK,yK)==2)){condition=0;}
  635.  
  636. if((board[x1][y1]=='K')&&(check1(xk,yk,x2+1,y2+65)==1)){condition=0;}
  637.  
  638.  
  639.  
  640. return condition;
  641. }
  642. int check1(int xk,char yk,int xK,char yK)
  643. { int i,j,k,check=0,t1,t2;
  644. char temp;
  645. char w[6]={'q','r','n','b','p','k'};
  646. char b[6]={'Q','R','N','B','P','K'};
  647. for(t1=0;t1<5;t1++)
  648. {
  649.  
  650. for(i=0;i<8;i++)
  651. {
  652. for(j=0;j<8;j++)
  653. {
  654. if(w[t1]==board[i][j])
  655. {
  656. if(valid(i+1,j+65,xK,yK)==1)
  657. {
  658. check=1;
  659. break;
  660. }
  661. }
  662. if(b[t1]==board[i][j])
  663. {
  664. if(valid(i+1,j+65,xk,yk)==1)
  665. {
  666. check=2;
  667. break;
  668. }
  669. }
  670. }
  671. }
  672. }
  673. return check;
  674. }
  675.  
  676.  
  677.  
  678. int checkTurns(int x1,char y1)
  679. {
  680. int turn=1;
  681. /*Validity of turns*/
  682. y1=y1-65;
  683. x1=x1-1;
  684. if((board[x1][y1]>=65 && board[x1][y1]<=90) && (g%2)==0){turn=0;}
  685. if((board[x1][y1]>=97 && board[x1][y1]<=122)&&(g%2)==1){turn=0;}
  686.  
  687. return turn;
  688. }
  689.  
  690. int checkblack (int xk,char yk,int xK,char yK)
  691. {
  692. int i,j,t2,mate=1,t1;
  693. char tempz;
  694.  
  695. if(check1(xk,yk,xK,yK)==1)
  696. {
  697.  
  698. for(i=0;i<8;i++) //black//
  699. {
  700. for(j=0;j<8;j++)
  701. {
  702. if(i+1==xK&&j+65==yK){continue;}
  703. if(board[i][j]>=65 && board[i][j]<=90)
  704. {
  705.  
  706. for(t1=0;t1<8;t1++)
  707. {
  708. for(t2=0;t2<8;t2++)
  709. {
  710. if(valid(i+1,j+65,t1+1,t2+65)==1)
  711. {
  712. tempz=board[t1][t2];
  713. board[t1][t2]=board[i][j];
  714. if(check1(xk,yk,xK,yK)==0)
  715. {
  716. mate=0; printf("%d %d \n",i,j);
  717. printf("%d %d\n",t1,t2);
  718.  
  719. }
  720. board[i][j]=board[t1][t2];
  721. board[t1][t2]=tempz;
  722.  
  723.  
  724. }
  725. }
  726. }
  727. }
  728. }
  729. }
  730. for(t1=-1;t1<2;t1++)
  731. {
  732. for(t2=-1;t2<2;t2++)
  733. {
  734. if(valid(xK,yK,xK+t1,yK+t2)==1)
  735. {
  736. tempz=board[xK+t1-1][yK+t2-65];
  737. board[xK+t1-1][yK+t2-65]=board[xK-1][yK-65];
  738. board[xK-1][yK-65]=219;
  739. if(check1(xk,yk,xK+t1,yK+t2)==0)
  740. {
  741. mate=0; //printf("%d %d\n",t1,t2);
  742. }
  743. board[xK-1][yK-65]=board[xK+t1-1][yK+t2-65];
  744. board[xK+t1-1][yK+t2-65]=tempz;
  745.  
  746.  
  747. }
  748.  
  749.  
  750.  
  751.  
  752. }
  753. }
  754. }
  755.  
  756.  
  757.  
  758. return mate;
  759. }
  760.  
  761. int checkwhite(int xk,char yk,int xK,char yK)
  762. {
  763. int i,j,t2,mate=1,t1;
  764. char tempz;
  765.  
  766. if (check1(xk,yk,xK,yK)==2)
  767. {
  768. for(i=0;i<8;i++) //white
  769. {
  770. for(j=0;j<8;j++)
  771. {
  772. if(i+1==xk&&j+65==yk){continue;}
  773. if(board[i][j]>=97 && board[i][j]<=122)
  774. {
  775.  
  776. for(t1=0;t1<8;t1++)
  777. {
  778. for(t2=0;t2<8;t2++)
  779. {
  780. if(valid(i+1,j+65,t1+1,t2+65)==1)
  781. {
  782. tempz=board[t1][t2];
  783. board[t1][t2]=board[i][j];
  784. if(check1(xk,yk,xK,yK)==0)
  785. {
  786. mate=0;// printf("%d %d \n",i,j);
  787. // printf("%d %d\n",t1,t2);
  788.  
  789. }
  790. board[i][j]=board[t1][t2];
  791. board[t1][t2]=tempz;
  792. }
  793. }
  794. }
  795. }
  796. }
  797. }
  798. for(t1=-1;t1<2;t1++)
  799. {
  800. for(t2=-1;t2<2;t2++)
  801. {
  802.  
  803. if(valid(xk,yk,xk+t1,yk+t2)==1)
  804. {
  805. tempz=board[xk+t1-1][yk+t2-65];
  806. board[xk+t1-1][yk+t2-65]=board[xk-1][yk-65];
  807. board[xk-1][yk-65]=219;
  808.  
  809. if(check1(xk+t1,yk+t2,xK,yK)==0)
  810. {
  811. mate=0;
  812. //printf("%d %d\n",t1,t2);
  813. //printf("%c",board[xk+t1-1][yk+t2-65]);
  814. }
  815. board[xk-1][yk-65]=board[xk+t1-1][yk+t2-65];
  816. board[xk+t1-1][yk+t2-65]=tempz;
  817.  
  818.  
  819. }
  820. }
  821. }
  822.  
  823.  
  824. }
  825.  
  826. return mate;
  827.  
  828. }
  829.  
  830. int removecheckwhite (int x1,char y1,int x2,char y2)
  831. {
  832. int condition=1; char tempo;
  833. y1=y1-65;
  834. x1=x1-1;
  835. x2=x2-1;
  836. y2=y2-65;
  837.  
  838. if(board[x1][y1]>=97 && board[x1][y1]<=122)
  839. {
  840. if(board[x1][y1]=='k'){xk=x2+1;
  841. yk=y2+65;}
  842. tempo=board[x2][y2];
  843. board[x2][y2]=board[x1][y1];
  844. board[x1][y1]=' ';
  845. if(check1(xk,yk,xK,yK)==2)
  846. {
  847. condition=0;
  848. }
  849. board[x1][y1]=board[x2][y2];
  850. board[x2][y2]=tempo;
  851. if(board[x1][y1]=='k'){xk=x1+1;
  852. yk=y1+65;}
  853. }
  854. return condition;
  855. }
  856.  
  857. int removecheckblack(int x1,char y1,int x2,char y2)
  858. {
  859.  
  860. int condition=1; char tempo;
  861. y1=y1-65;
  862. x1=x1-1;
  863. x2=x2-1;
  864. y2=y2-65;
  865.  
  866.  
  867. if(board[x1][y1]>=65 && board[x1][y1]<=90)
  868. {
  869. if(board[x1][y1]=='K'){xK=x2+1;
  870. yK=y2+65;}
  871. tempo=board[x2][y2];
  872. board[x2][y2]=board[x1][y1];
  873. board[x1][y1]=' ';
  874. if(check1(xk,yk,xK,yK)==1)
  875. {
  876. condition=0;
  877. }
  878. board[x1][y1]=board[x2][y2];
  879. board[x2][y2]=tempo;
  880. if(board[x1][y1]=='K'){xK=x1+1;
  881. yK=y1+65;}
  882. }
  883. return condition;
  884.  
  885. }
  886.  
  887. int stalemateWhite(int a)
  888. {
  889. int i,j,stale=1,t1,t2;
  890. char w[6]={'q','r','n','b','p','k'};
  891. char b[6]={'Q','R','N','B','P','K'};
  892.  
  893. for(i=0;i<8;i++)
  894. {
  895. for(j=0;j<8;j++)
  896. {
  897. if(board[i][j]>=97 && board[i][j]<=122) //WHITE//
  898. {
  899. for(t1=0;t1<8;t1++)
  900. {
  901. for(t2=0;t2<8;t2++)
  902. {
  903. if(valid(i+1,j+65,t1+1,t2+65)==1&&removecheckwhite(i+1,j+65,t1+1,t2+65)==1 )
  904. {
  905. stale=0;
  906. //printf("%d %c %d %c \n",i+1,j+65,t1+1,t2+65);
  907. }
  908.  
  909. }
  910. }
  911. }
  912. }
  913. }
  914.  
  915. return stale;
  916. }
  917. int stalemateBlack(int a)
  918. {
  919.  
  920. int i,j,stale=1,t1,t2;
  921.  
  922. for(i=0;i<8;i++)
  923. {
  924. for(j=0;j<8;j++)
  925. {
  926. if(board[i][j]>=65 && board[i][j]<=90)
  927. {
  928. for(t1=0;t1<8;t1++)
  929. {
  930. for(t2=0;t2<8;t2++)
  931. {
  932. if(valid(i+1,j+65,t1+1,t2+65)==1&&removecheckblack(i+1,j+65,t1+1,t2+65)==1)
  933. {
  934. stale=0;
  935. //printf("%d %c \n",i+1,j+65);
  936. }
  937.  
  938. }
  939. }
  940. }
  941. }
  942. }
  943.  
  944.  
  945.  
  946. return stale;
  947.  
  948. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement