Advertisement
anas_harby

Untitled

Dec 18th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. char BWBoard[8][8];
  7. char piecesBoard[8][8] = {{'R','N','B','Q','K','B','N','R'}, {'P','P','P','P','P','P','P','P'},{},{},{},{},{'p','p','p','p','p','p','p','p'},{'r','n','b','q','k','b','n','r'}};
  8. char board[10][10];
  9. int turn=0, capturedBlack[16], capturedWhite[16], countBlack=0, countWhite=0;
  10.  
  11. void initializeBW();
  12. void userInput();
  13. void move(int iCurrent, int iDestination, int jCurrent, int jDestination);
  14. void printBoard();
  15. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination);
  16. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination);
  17. void printErrors(int, int, int, int, int);
  18.  
  19. int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination);
  20. int knight(int iCurrent, int iDestination, int jCurrent, int jDestination);
  21. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination);
  22. int bishop(int iCurrent, int iDestination, int jCurrent, int jDestination);
  23. int queen(int iCurrent, int iDestination, int jCurrent, int jDestination);
  24. int king(int iCurrent, int iDestination, int jCurrent, int jDestination);
  25.  
  26. int iBlackKing=0, iWhiteKing=7, jBlackKing=4, jWhiteKing=4;
  27. int checkerPieces[], checksCount;
  28. int check();
  29. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination);
  30. void checkmate();
  31. int checkMateFlag=0;
  32.  
  33. int main()
  34. {
  35. initializeBW();
  36.  
  37. while(!checkMateFlag)
  38. {
  39. turn++;
  40. printBoard();
  41. userInput();
  42. printf("\n");
  43.  
  44.  
  45. }
  46. printf("CHECKMATE!\n");
  47. printf("%d %d", iWhiteKing, jWhiteKing);
  48. }
  49.  
  50.  
  51. void initializeBW ()
  52. {
  53. char c1 = '_', c2= '.';
  54. for(int i=0; i<8; i++)
  55. {
  56. for(int j=0; j<8; j+=2)
  57. {
  58. BWBoard[i][j] = c1;
  59. BWBoard[i][j+1] = c2;
  60. }
  61. char temp = c1;
  62. c1 = c2;
  63. c2 = temp;
  64. }
  65. }
  66.  
  67. void userInput()
  68. {
  69.  
  70. char inp[10];
  71. gets(inp);
  72. while(strlen(inp)!=4)
  73. {
  74. printf("Invalid Input!\n");
  75. gets(inp);
  76. }
  77. int iCurrent, iDestination, jCurrent, jDestination;
  78. jCurrent = (int)inp[0] - (int)'A';
  79. iCurrent = (int)inp[1] - (int)'0';
  80. jDestination = (int)inp[2] - (int)'A';
  81. iDestination = (int)inp[3] - (int)'0';
  82. iDestination = 8 - iDestination;
  83. iCurrent = 8 - iCurrent;
  84. if(validate(iCurrent, iDestination, jCurrent, jDestination)!=0)
  85. {
  86. printErrors(validate(iCurrent, iDestination, jCurrent, jDestination), iCurrent, iDestination, jCurrent, jDestination);
  87. userInput();
  88.  
  89. }
  90.  
  91. else if(turn%2!=0 && (piecesBoard[iCurrent][jCurrent]>='A' && piecesBoard[iCurrent][jCurrent]<='Z'))
  92. {
  93. printf("White Pieces Turn!\n");
  94. userInput();
  95. }
  96.  
  97. else if(turn%2==0 && (piecesBoard[iCurrent][jCurrent]>='a' && piecesBoard[iCurrent][jCurrent]<='z'))
  98. {
  99. printf("Black Pieces Turn!\n");
  100. userInput();
  101. }
  102.  
  103. else if(validateCheck(iCurrent,iDestination,jCurrent,jDestination)==1)
  104. {
  105. if(turn%2==1)
  106. printf("WHITE KING WILL BE CHECKED!\n");
  107. else if(turn%2==0)
  108. printf("BLACK KING WILL BE CHECKED!\n");
  109. userInput();
  110. }
  111.  
  112. else if(validateCheck(iCurrent,iDestination,jCurrent,jDestination)==0 && validate(iCurrent, iDestination, jCurrent, jDestination)==0)
  113. {
  114.  
  115. capture(iCurrent, iDestination, jCurrent, jDestination);
  116. move(iCurrent, iDestination, jCurrent, jDestination);
  117. countChecks();
  118. checkmate();
  119.  
  120. }
  121. }
  122.  
  123.  
  124. void move(int iCurrent, int iDestination, int jCurrent, int jDestination)
  125. {
  126.  
  127. piecesBoard[iDestination][jDestination] = piecesBoard[iCurrent][jCurrent];
  128. piecesBoard[iCurrent][jCurrent]='\0';
  129. }
  130.  
  131. void printBoard()
  132. {
  133. board[0][0] = ' ', board[0][9] = ' ', board[9][0] = ' ', board[9][9] = ' ';
  134. for(int i=1; i<9; i++)
  135. {
  136. board[i][0] = 9-i;
  137. board[i][9]= 9-i;
  138. board[0][i] = 'A' +i-1;
  139. board[9][i] = 'A' +i-1;
  140.  
  141. }
  142.  
  143. for(int i=0; i<10; i++)
  144. {
  145. for(int j=0; j<10; j++)
  146. {
  147. if((board[i][j]>='A' && board[i][j] <= 'Z') || board[i][j]==' ')
  148. printf("%c\t", board[i][j]);
  149. else if((i>=1 && i<=8) && (j>=1 && j<=8))
  150. {
  151. if((piecesBoard[i-1][j-1]>='A' && piecesBoard[i-1][j-1]<='Z') || (piecesBoard[i-1][j-1]>='a' && piecesBoard[i-1][j-1]<='z'))
  152. printf("%c\t", piecesBoard[i-1][j-1]);
  153. else
  154. printf("%c\t", BWBoard[i-1][j-1]);
  155. }
  156. else
  157. printf("%d\t", (char)board[i][j]);
  158. }
  159. if(i==0||i==8)
  160. printf("\n\n\n");
  161. else
  162. printf("\n\n");
  163. }
  164. printf("\n\n\n");
  165. }
  166.  
  167.  
  168. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination)
  169. {
  170. if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  171. {
  172. return 1;
  173. }
  174.  
  175.  
  176.  
  177. else if(piecesBoard[iCurrent][jCurrent]=='\0')
  178. {
  179.  
  180. return 4;
  181. }
  182.  
  183. else if(((piecesBoard[iCurrent][jCurrent]=='r')||(piecesBoard[iCurrent][jCurrent]=='R')) && (rook(iCurrent, iDestination, jCurrent, jDestination)==1))
  184. {
  185.  
  186. return 5;
  187. }
  188.  
  189. else if(((piecesBoard[iCurrent][jCurrent]=='p')||(piecesBoard[iCurrent][jCurrent]=='P')) && (pawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  190. {
  191.  
  192. return 5;
  193. }
  194.  
  195. else if(((piecesBoard[iCurrent][jCurrent]=='b')||(piecesBoard[iCurrent][jCurrent]=='B')) && (bishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  196. {
  197.  
  198. return 5;
  199. }
  200.  
  201. else if(((piecesBoard[iCurrent][jCurrent]=='q')||(piecesBoard[iCurrent][jCurrent]=='Q')) && (queen(iCurrent, iDestination, jCurrent, jDestination)==1))
  202. {
  203.  
  204. return 5;
  205. }
  206.  
  207. else if(((piecesBoard[iCurrent][jCurrent]=='k')||(piecesBoard[iCurrent][jCurrent]=='K')) && (king(iCurrent, iDestination, jCurrent, jDestination)==1))
  208. {
  209.  
  210. return 5;
  211. }
  212.  
  213. else if(((piecesBoard[iCurrent][jCurrent]=='n')||(piecesBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  214. {
  215.  
  216. return 5;
  217. }
  218.  
  219. else if (((piecesBoard[iCurrent][jCurrent]>='a') && (piecesBoard[iCurrent][jCurrent]<='z')) && ((piecesBoard[iDestination][jDestination]>='a') && (piecesBoard[iDestination][jDestination]<='z')))
  220. {
  221.  
  222. return 5;
  223. }
  224.  
  225. else if (((piecesBoard[iCurrent][jCurrent]>='A') && (piecesBoard[iCurrent][jCurrent]<='Z')) && ((piecesBoard[iDestination][jDestination]>='A') && (piecesBoard[iDestination][jDestination]<='Z')))
  226. {
  227.  
  228. return 5;
  229. }
  230.  
  231.  
  232.  
  233. else
  234. {
  235.  
  236. return 0;
  237. }
  238.  
  239.  
  240. }
  241.  
  242.  
  243.  
  244.  
  245.  
  246. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination)
  247. {
  248. if(piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  249. {
  250. capturedWhite[countWhite] = piecesBoard[iDestination][jDestination];
  251. countWhite++;
  252. }
  253. else if(piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  254. {
  255. capturedBlack[countBlack] = piecesBoard[iDestination][jDestination];
  256. countBlack++;
  257. }
  258. system("cls");
  259. printf("\nCaptured White Pieces: ");
  260. for(int i=0; i<countWhite; i++)
  261. printf("%c ", capturedWhite[i]);
  262. printf("\nCaptured Black Pieces: ");
  263. for(int i=0; i<countBlack; i++)
  264. printf("%c ", capturedBlack[i]);
  265. printf("\n\n");
  266. }
  267.  
  268.  
  269.  
  270.  
  271. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  272. {
  273. int count,flag=0;
  274. if((jCurrent==jDestination) && (iCurrent!= iDestination))
  275. {
  276. if (iDestination>iCurrent)
  277. {
  278. for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  279. {
  280. if (piecesBoard[iCurrent+count][jCurrent]=='\0')
  281. {
  282. flag=0 ;
  283. }
  284. else
  285. {
  286. flag=1;
  287. }
  288. }
  289. }
  290. else
  291. {
  292. for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  293. {
  294. if (piecesBoard[iCurrent-count][jCurrent]=='\0')
  295. {
  296. flag=0;
  297. }
  298. else
  299. {
  300. flag=1;
  301. }
  302. }
  303. }
  304. if (flag==0)
  305. {
  306. return 0;
  307. }
  308. else
  309. {
  310. return 1;
  311. }
  312. }
  313. else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  314. {
  315. if (jDestination>jCurrent)
  316. {
  317. for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  318. {
  319. if (piecesBoard[iCurrent][jCurrent+count]=='\0')
  320. {
  321. flag=0;
  322. }
  323. else
  324. {
  325. flag=1;
  326. }
  327. }
  328. }
  329. else
  330. {
  331. for (count=1; (jCurrent-count)>jDestination; count++)
  332. {
  333. if (piecesBoard[iCurrent][jCurrent-count]=='\0')
  334. {
  335. flag=0;
  336. }
  337. else
  338. {
  339. flag=1;
  340. }
  341. }
  342. }
  343. if (flag==0)
  344. {
  345. return 0;
  346. }
  347. else
  348. {
  349. return 1;
  350. }
  351. }
  352. else
  353. {
  354. return 1;
  355. }
  356. }
  357.  
  358.  
  359. int king(int iCurrent,int iDestination,int jCurrent,int jDestination)
  360. {
  361. int iDiff,jDiff;
  362. iDiff=iCurrent-iDestination;
  363. jDiff=jCurrent-jDestination;
  364. if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  365. {
  366. if(piecesBoard[iCurrent][jCurrent]=='k')
  367. {
  368. iWhiteKing = iDestination;
  369. jWhiteKing = jDestination;
  370.  
  371. }
  372. else if(piecesBoard[iCurrent][jCurrent]=='K')
  373. {
  374. iBlackKing = iDestination;
  375. jBlackKing = jDestination;
  376. }
  377. return 0;
  378. }
  379. else
  380. {
  381. return 1;
  382. }
  383. }
  384.  
  385. int bishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  386. {
  387. int iDiff,jDiff;
  388. int count=1,flag=0;
  389. iDiff=iDestination-iCurrent;
  390. jDiff=jDestination-jCurrent;
  391. int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  392.  
  393. if (abs(iDiff)==abs(jDiff))
  394. {
  395. if (iDestination>iCurrent)
  396. {
  397. count=1;
  398. do
  399. {
  400. DecjCurrent=jCurrent-count;
  401. IncjCurrent=jCurrent+count;
  402. InciCurrent=iCurrent+count;
  403. if (InciCurrent<iDestination)
  404. {
  405. if (jDestination<jCurrent)
  406. {
  407. if (piecesBoard[InciCurrent][DecjCurrent]=='\0')
  408. {
  409. flag=0;
  410. }
  411. else
  412. {
  413. flag=1;
  414. }
  415.  
  416. }
  417. else if (jDestination>jCurrent)
  418. {
  419. if (piecesBoard[InciCurrent][IncjCurrent]=='\0')
  420. {
  421. flag=0;
  422. }
  423. else
  424. {
  425. flag=1;
  426. }
  427. }
  428. count++;
  429. }
  430. }
  431. while ((InciCurrent<iDestination) && (flag==0));
  432. if (flag==0)
  433. {
  434. return 0;
  435. }
  436. else
  437. {
  438. return 1;
  439. }
  440. }
  441.  
  442. else
  443. {
  444. count=1;
  445. do
  446. {
  447. DeciCurrent=iCurrent-count;
  448. DecjCurrent=jCurrent-count;
  449. IncjCurrent=jCurrent+count;
  450. if (DeciCurrent>iDestination)
  451. {
  452. if (jDestination<jCurrent)
  453. {
  454.  
  455. if (piecesBoard[DeciCurrent][DecjCurrent]=='\0')
  456. {
  457. flag=0;
  458. }
  459. else
  460. {
  461. flag=1;
  462. }
  463. }
  464. else if (jDestination>jCurrent)
  465. {
  466. if (piecesBoard[DeciCurrent][IncjCurrent]=='\0')
  467. {
  468. flag=0;
  469. }
  470. else
  471. {
  472. flag=1;
  473. }
  474. }
  475. count++;
  476.  
  477. }
  478. }
  479. while ((DeciCurrent>iDestination) && (flag==0));
  480.  
  481. if (flag==0)
  482. {
  483. return 0;
  484. }
  485. else
  486. {
  487. return 1;
  488. }
  489. }
  490.  
  491.  
  492. }
  493. else
  494. {
  495. return 1;
  496. }
  497.  
  498. }
  499.  
  500. int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  501. {
  502.  
  503. if(piecesBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  504. {
  505. if (piecesBoard[iDestination][jDestination]!='\0')
  506. {
  507. return 1;
  508. }
  509. else
  510. {
  511. return 0;
  512. }
  513. }
  514.  
  515. else if(piecesBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  516. {
  517. if (piecesBoard[iDestination][jDestination]!='\0')
  518. {
  519. return 1;
  520. }
  521. else
  522. {
  523. return 0;
  524. }
  525. }
  526.  
  527. else if(piecesBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  528. return 0;
  529.  
  530. else if(piecesBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  531. return 0;
  532.  
  533. else if(piecesBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
  534. return 0;
  535.  
  536. else if(piecesBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
  537. return 0;
  538.  
  539. else
  540. return 1;
  541.  
  542. }
  543.  
  544.  
  545.  
  546. int queen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  547. {
  548. int iDiff,jDiff;
  549. iDiff=iDestination-iCurrent;
  550. jDiff=jDestination-jCurrent;
  551.  
  552. if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& rook(iCurrent,iDestination,jCurrent,jDestination)==0)
  553.  
  554. return 0;
  555.  
  556.  
  557. else if (abs(iDiff)==abs(jDiff) && bishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  558.  
  559. return 0;
  560.  
  561. else
  562.  
  563. return 1;
  564.  
  565. }
  566.  
  567. int knight(int iCurrent,int iDestination,int jCurrent,int jDestination)
  568. {
  569. int iDiff,jDiff;
  570. iDiff=iDestination-iCurrent;
  571. jDiff=jDestination-jCurrent;
  572. if ((abs(iDiff)==2) && (abs(jDiff)==1))
  573.  
  574. return 0;
  575.  
  576. else if ((abs(jDiff)==2) && (abs(iDiff)==1))
  577.  
  578. return 0;
  579.  
  580. else
  581.  
  582. return 1;
  583.  
  584. }
  585.  
  586.  
  587. int check()
  588. {
  589. int f=0;
  590.  
  591. for(int i=0; i<8; i++)
  592. {
  593. for(int j=0; j<8; j++)
  594. {
  595. if(turn%2==1)
  596. {
  597. if(validate(i, iBlackKing, j, jBlackKing)==0)
  598. {
  599. f=1;
  600. }
  601. }
  602.  
  603. else if(turn%2==0)
  604. {
  605. if(validate(i, iWhiteKing, j, jWhiteKing)==0)
  606. {
  607. f=1;
  608. }
  609. }
  610. }
  611. }
  612.  
  613. return f;
  614. }
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621. void checkmate()
  622. {
  623. int f=0;
  624.  
  625. if(check()==1)
  626. {
  627.  
  628.  
  629. if(turn%2==0)
  630. {
  631.  
  632. f = validateCheckmate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing) && validateCheckmate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing) &&
  633. validateCheckmate(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing+1) && validateCheckmate(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing-1) &&
  634. validateCheckmate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing+1) && validateCheckmate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing-1) &&
  635. validateCheckmate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing+1) && validateCheckmate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing-1);
  636. }
  637.  
  638. else if(turn%2==1)
  639. {
  640. f = validateCheckmate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing) && validateCheckmate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing) &&
  641. validateCheckmate(iBlackKing, iBlackKing, jBlackKing, jBlackKing+1) && validateCheckmate(iBlackKing, iBlackKing, jBlackKing, jBlackKing-1) &&
  642. validateCheckmate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing+1) && validateCheckmate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing-1) &&
  643. validateCheckmate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing+1) && validateCheckmate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing-1);
  644. }
  645.  
  646. }
  647.  
  648. if(checksCount==1)
  649. {
  650. for(int i=0; i<8; i++)
  651. {
  652. for(int j=0; j<8; j++)
  653. {
  654. if(turn%2==0) {
  655. if(piecesBoard[i][j]>='a' && piecesBoard[i][j]<='z') {
  656. if(validate(i,checkerPieces[0]/10 ,j, checkerPieces[0]%10)==0)
  657. f=0;
  658. }
  659. }
  660. else if(turn%2==1) {
  661. if(piecesBoard[i][j]>='A' && piecesBoard[i][j]<='Z') {
  662. if(validate(i,checkerPieces[0]/10 ,j, checkerPieces[0]%10)==0)
  663. f=0;
  664. }
  665. }
  666. }
  667. }
  668. }
  669.  
  670. if(f==1)
  671. checkMateFlag = 1;
  672.  
  673.  
  674. }
  675.  
  676.  
  677. void printErrors(int f, int iCurrent, int iDestination, int jCurrent, int jDestination)
  678. {
  679. f = validate(iCurrent, iDestination, jCurrent, jDestination);
  680. switch(f)
  681. {
  682. case 0:
  683. break;
  684.  
  685. case 1:
  686. printf("Invalid Input!\n");
  687. break;
  688. case 4:
  689. printf("Empty Position!\n");
  690. break;
  691. case 5:
  692. printf("Wrong Move!\n");
  693. break;
  694. }
  695.  
  696. }
  697.  
  698.  
  699. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
  700. {
  701. char tempPiece;
  702.  
  703. int f=0, fC=0;
  704. if(piecesBoard[iDestination][jDestination]!='\0') {
  705. tempPiece = piecesBoard[iDestination][jDestination];
  706. fC=1;
  707. }
  708. if(validate(iCurrent,iDestination,jCurrent,jDestination)!=0)
  709. f=1;
  710. else
  711. {
  712. move(iCurrent, iDestination, jCurrent, jDestination);
  713.  
  714. turn++;
  715. if(check()==1)
  716. {
  717. f=1;
  718. }
  719.  
  720. move(iDestination, iCurrent, jDestination, jCurrent);
  721.  
  722. turn--;
  723. }
  724. if(fC==1)
  725. {
  726. piecesBoard[iDestination][jDestination] = tempPiece;
  727. }
  728.  
  729. return f;
  730. }
  731.  
  732.  
  733.  
  734.  
  735.  
  736. int validateCheckmate(int iCurrent, int iDestination, int jCurrent, int jDestination)
  737. {
  738.  
  739.  
  740. int tempiBlackKing = iBlackKing;
  741. int tempjBlackKing = jBlackKing;
  742. int tempiWhiteKing = iWhiteKing;
  743. int tempjWhiteKing = jWhiteKing;
  744.  
  745. char tempPiece;
  746.  
  747.  
  748. int f=0, fC=0;
  749. turn++;
  750. if(piecesBoard[iDestination][jDestination]!='\0') {
  751. tempPiece = piecesBoard[iDestination][jDestination];
  752. fC=1;
  753. }
  754. if(validate(iCurrent,iDestination,jCurrent,jDestination)!=0)
  755. f=1;
  756. else
  757. {
  758.  
  759. move(iCurrent, iDestination, jCurrent, jDestination);
  760.  
  761. if(reverseCheck()==1)
  762. {
  763. f=1;
  764. }
  765.  
  766. move(iDestination, iCurrent, jDestination, jCurrent);
  767.  
  768.  
  769. }
  770. if(fC==1)
  771. {
  772. piecesBoard[iDestination][jDestination] = tempPiece;
  773.  
  774. }
  775. iWhiteKing = tempiWhiteKing;
  776. iBlackKing = tempiBlackKing;
  777. jWhiteKing = tempjWhiteKing;
  778. jBlackKing = tempjBlackKing;
  779. turn--;
  780. return f;
  781. }
  782.  
  783. int reverseCheck()
  784. {
  785. int f=0;
  786.  
  787. for(int i=0; i<8; i++)
  788. {
  789. for(int j=0; j<8; j++)
  790. {
  791. if(turn%2==0)
  792. {
  793. if(validate(i, iBlackKing, j, jBlackKing)==0)
  794. {
  795. f=1;
  796. }
  797. }
  798.  
  799. else if(turn%2==1)
  800. {
  801. if(validate(i, iWhiteKing, j, jWhiteKing)==0)
  802. {
  803. f=1;
  804. }
  805. }
  806. }
  807. }
  808.  
  809. return f;
  810. }
  811.  
  812.  
  813. int countChecks()
  814. {
  815. int f=0;
  816. for(int i=0; i<checksCount; i++)
  817. checkerPieces[i] == 0;
  818.  
  819. checksCount=0;
  820.  
  821. for(int i=0; i<8; i++)
  822. {
  823. for(int j=0; j<8; j++)
  824. {
  825. if(turn%2==1)
  826. {
  827. if(validate(i, iBlackKing, j, jBlackKing)==0)
  828. {
  829. f=1;
  830. checkerPieces[checksCount] = i*10 + j;
  831. checksCount++;
  832. }
  833. }
  834.  
  835. else if(turn%2==0)
  836. {
  837. if(validate(i, iWhiteKing, j, jWhiteKing)==0)
  838. {
  839. f=1;
  840. checkerPieces[checksCount] = i*10 + j;
  841. checksCount++;
  842. }
  843. }
  844. }
  845. }
  846.  
  847. return f;
  848. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement