Advertisement
anas_harby

Untitled

Dec 19th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.53 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 prototypeBoard[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'}};
  9. char board[10][10];
  10. int turn=0, capturedBlack[16], capturedWhite[16], countBlack=0, countWhite=0;
  11.  
  12. void initializeBW();
  13. void userInput();
  14. void move(int iCurrent, int iDestination, int jCurrent, int jDestination);
  15. void printBoard();
  16. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination);
  17. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination);
  18. void printErrors(int, int, int, int, int);
  19.  
  20. int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination);
  21. int knight(int iCurrent, int iDestination, int jCurrent, int jDestination);
  22. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination);
  23. int bishop(int iCurrent, int iDestination, int jCurrent, int jDestination);
  24. int queen(int iCurrent, int iDestination, int jCurrent, int jDestination);
  25. int king(int iCurrent, int iDestination, int jCurrent, int jDestination);
  26.  
  27. int iBlackKing=0, iWhiteKing=7, jBlackKing=4, jWhiteKing=4;
  28. int checkerPieces[], checksCount;
  29. int check();
  30. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination);
  31. void checkmate();
  32. int checkMateFlag=0;
  33. void correspondPrototypeBoard();
  34. int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination);
  35. int checkPrototype();
  36.  
  37. int main()
  38. {
  39. initializeBW();
  40.  
  41. while(!checkMateFlag)
  42. {
  43. turn++;
  44. printBoard();
  45. userInput();
  46. printf("\n");
  47. }
  48. system("cls");
  49. printBoard();
  50. printf("CHECKMATE!\n");
  51.  
  52. }
  53.  
  54.  
  55. void initializeBW ()
  56. {
  57. char c1 = '_', c2= '.';
  58. for(int i=0; i<8; i++)
  59. {
  60. for(int j=0; j<8; j+=2)
  61. {
  62. BWBoard[i][j] = c1;
  63. BWBoard[i][j+1] = c2;
  64. }
  65. char temp = c1;
  66. c1 = c2;
  67. c2 = temp;
  68. }
  69. }
  70.  
  71. void userInput()
  72. {
  73.  
  74. char inp[10];
  75. gets(inp);
  76. while(strlen(inp)!=4)
  77. {
  78. printf("Invalid Input!\n");
  79. gets(inp);
  80. }
  81. int iCurrent, iDestination, jCurrent, jDestination;
  82. jCurrent = (int)inp[0] - (int)'A';
  83. iCurrent = (int)inp[1] - (int)'0';
  84. jDestination = (int)inp[2] - (int)'A';
  85. iDestination = (int)inp[3] - (int)'0';
  86. iDestination = 8 - iDestination;
  87. iCurrent = 8 - iCurrent;
  88. if(validate(iCurrent, iDestination, jCurrent, jDestination)!=0)
  89. {
  90. printErrors(validate(iCurrent, iDestination, jCurrent, jDestination), iCurrent, iDestination, jCurrent, jDestination);
  91. userInput();
  92.  
  93. }
  94.  
  95. else if(turn%2!=0 && (piecesBoard[iCurrent][jCurrent]>='A' && piecesBoard[iCurrent][jCurrent]<='Z'))
  96. {
  97. printf("White Pieces Turn!\n");
  98. userInput();
  99. }
  100.  
  101. else if(turn%2==0 && (piecesBoard[iCurrent][jCurrent]>='a' && piecesBoard[iCurrent][jCurrent]<='z'))
  102. {
  103. printf("Black Pieces Turn!\n");
  104. userInput();
  105. }
  106.  
  107. else if(validateCheck(iCurrent,iDestination,jCurrent,jDestination)==1)
  108. {
  109. if(turn%2==1)
  110. printf("WHITE KING WILL BE CHECKED!\n");
  111. else if(turn%2==0)
  112. printf("BLACK KING WILL BE CHECKED!\n");
  113. userInput();
  114. }
  115.  
  116. else if(validateCheck(iCurrent,iDestination,jCurrent,jDestination)==0 && validate(iCurrent, iDestination, jCurrent, jDestination)==0)
  117. {
  118.  
  119. capture(iCurrent, iDestination, jCurrent, jDestination);
  120. move(iCurrent, iDestination, jCurrent, jDestination);
  121. checkmate();
  122. correspondPrototypeBoard();
  123.  
  124.  
  125. }
  126. }
  127.  
  128.  
  129. void move(int iCurrent, int iDestination, int jCurrent, int jDestination)
  130. {
  131.  
  132. piecesBoard[iDestination][jDestination] = piecesBoard[iCurrent][jCurrent];
  133. piecesBoard[iCurrent][jCurrent]='\0';
  134. }
  135.  
  136. void printBoard()
  137. {
  138. board[0][0] = ' ', board[0][9] = ' ', board[9][0] = ' ', board[9][9] = ' ';
  139. for(int i=1; i<9; i++)
  140. {
  141. board[i][0] = 9-i;
  142. board[i][9]= 9-i;
  143. board[0][i] = 'A' +i-1;
  144. board[9][i] = 'A' +i-1;
  145.  
  146. }
  147.  
  148. for(int i=0; i<10; i++)
  149. {
  150. for(int j=0; j<10; j++)
  151. {
  152. if((board[i][j]>='A' && board[i][j] <= 'Z') || board[i][j]==' ')
  153. printf("%c\t", board[i][j]);
  154. else if((i>=1 && i<=8) && (j>=1 && j<=8))
  155. {
  156. 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'))
  157. printf("%c\t", piecesBoard[i-1][j-1]);
  158. else
  159. printf("%c\t", BWBoard[i-1][j-1]);
  160. }
  161. else
  162. printf("%d\t", (char)board[i][j]);
  163. }
  164. if(i==0||i==8)
  165. printf("\n\n\n");
  166. else
  167. printf("\n\n");
  168. }
  169. printf("\n\n\n");
  170. }
  171.  
  172.  
  173. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination)
  174. {
  175. if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  176. {
  177. return 1;
  178. }
  179.  
  180. else if (((piecesBoard[iCurrent][jCurrent]>='a') && (piecesBoard[iCurrent][jCurrent]<='z')) && ((piecesBoard[iDestination][jDestination]>='a') && (piecesBoard[iDestination][jDestination]<='z')))
  181. {
  182.  
  183. return 5;
  184. }
  185.  
  186. else if (((piecesBoard[iCurrent][jCurrent]>='A') && (piecesBoard[iCurrent][jCurrent]<='Z')) && ((piecesBoard[iDestination][jDestination]>='A') && (piecesBoard[iDestination][jDestination]<='Z')))
  187. {
  188.  
  189. return 5;
  190. }
  191.  
  192. else if(piecesBoard[iCurrent][jCurrent]=='\0')
  193. {
  194.  
  195. return 4;
  196. }
  197.  
  198. else if(((piecesBoard[iCurrent][jCurrent]=='r')||(piecesBoard[iCurrent][jCurrent]=='R')) && (rook(iCurrent, iDestination, jCurrent, jDestination)==1))
  199. {
  200.  
  201. return 5;
  202. }
  203.  
  204. else if(((piecesBoard[iCurrent][jCurrent]=='p')||(piecesBoard[iCurrent][jCurrent]=='P')) && (pawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  205. {
  206.  
  207. return 5;
  208. }
  209.  
  210. else if(((piecesBoard[iCurrent][jCurrent]=='b')||(piecesBoard[iCurrent][jCurrent]=='B')) && (bishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  211. {
  212.  
  213. return 5;
  214. }
  215.  
  216. else if(((piecesBoard[iCurrent][jCurrent]=='q')||(piecesBoard[iCurrent][jCurrent]=='Q')) && (queen(iCurrent, iDestination, jCurrent, jDestination)==1))
  217. {
  218.  
  219. return 5;
  220. }
  221.  
  222. else if(((piecesBoard[iCurrent][jCurrent]=='k')||(piecesBoard[iCurrent][jCurrent]=='K')) && (king(iCurrent, iDestination, jCurrent, jDestination)==1))
  223. {
  224.  
  225. return 5;
  226. }
  227.  
  228. else if(((piecesBoard[iCurrent][jCurrent]=='n')||(piecesBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  229. {
  230.  
  231. return 5;
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. else
  239. {
  240.  
  241. return 0;
  242. }
  243.  
  244.  
  245. }
  246.  
  247.  
  248.  
  249.  
  250.  
  251. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination)
  252. {
  253. if(piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  254. {
  255. capturedWhite[countWhite] = piecesBoard[iDestination][jDestination];
  256. countWhite++;
  257. }
  258. else if(piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  259. {
  260. capturedBlack[countBlack] = piecesBoard[iDestination][jDestination];
  261. countBlack++;
  262. }
  263. system("cls");
  264. printf("\nCaptured White Pieces: ");
  265. for(int i=0; i<countWhite; i++)
  266. printf("%c ", capturedWhite[i]);
  267. printf("\nCaptured Black Pieces: ");
  268. for(int i=0; i<countBlack; i++)
  269. printf("%c ", capturedBlack[i]);
  270. printf("\n\n");
  271. }
  272.  
  273.  
  274.  
  275.  
  276. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  277. {
  278. int count,flag=0;
  279. if((jCurrent==jDestination) && (iCurrent!= iDestination))
  280. {
  281. if (iDestination>iCurrent)
  282. {
  283. for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  284. {
  285. if (piecesBoard[iCurrent+count][jCurrent]=='\0')
  286. {
  287. flag=0 ;
  288. }
  289. else
  290. {
  291. flag=1;
  292. }
  293. }
  294. }
  295. else
  296. {
  297. for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  298. {
  299. if (piecesBoard[iCurrent-count][jCurrent]=='\0')
  300. {
  301. flag=0;
  302. }
  303. else
  304. {
  305. flag=1;
  306. }
  307. }
  308. }
  309. if (flag==0)
  310. {
  311. return 0;
  312. }
  313. else
  314. {
  315. return 1;
  316. }
  317. }
  318. else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  319. {
  320. if (jDestination>jCurrent)
  321. {
  322. for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  323. {
  324. if (piecesBoard[iCurrent][jCurrent+count]=='\0')
  325. {
  326. flag=0;
  327. }
  328. else
  329. {
  330. flag=1;
  331. }
  332. }
  333. }
  334. else
  335. {
  336. for (count=1; (jCurrent-count)>jDestination; count++)
  337. {
  338. if (piecesBoard[iCurrent][jCurrent-count]=='\0')
  339. {
  340. flag=0;
  341. }
  342. else
  343. {
  344. flag=1;
  345. }
  346. }
  347. }
  348. if (flag==0)
  349. {
  350. return 0;
  351. }
  352. else
  353. {
  354. return 1;
  355. }
  356. }
  357. else
  358. {
  359. return 1;
  360. }
  361. }
  362.  
  363.  
  364. int king(int iCurrent,int iDestination,int jCurrent,int jDestination)
  365. {
  366. int iDiff,jDiff;
  367. iDiff=iCurrent-iDestination;
  368. jDiff=jCurrent-jDestination;
  369. if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  370. {
  371. return 0;
  372.  
  373. }
  374. else
  375. {
  376. return 1;
  377. }
  378. }
  379.  
  380. int bishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  381. {
  382. int iDiff,jDiff;
  383. int count=1,flag=0;
  384. iDiff=iDestination-iCurrent;
  385. jDiff=jDestination-jCurrent;
  386. int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  387.  
  388. if (abs(iDiff)==abs(jDiff))
  389. {
  390. if (iDestination>iCurrent)
  391. {
  392. count=1;
  393. do
  394. {
  395. DecjCurrent=jCurrent-count;
  396. IncjCurrent=jCurrent+count;
  397. InciCurrent=iCurrent+count;
  398. if (InciCurrent<iDestination)
  399. {
  400. if (jDestination<jCurrent)
  401. {
  402. if (piecesBoard[InciCurrent][DecjCurrent]=='\0')
  403. {
  404. flag=0;
  405. }
  406. else
  407. {
  408. flag=1;
  409. }
  410.  
  411. }
  412. else if (jDestination>jCurrent)
  413. {
  414. if (piecesBoard[InciCurrent][IncjCurrent]=='\0')
  415. {
  416. flag=0;
  417. }
  418. else
  419. {
  420. flag=1;
  421. }
  422. }
  423. count++;
  424. }
  425. }
  426. while ((InciCurrent<iDestination) && (flag==0));
  427. if (flag==0)
  428. {
  429. return 0;
  430. }
  431. else
  432. {
  433. return 1;
  434. }
  435. }
  436.  
  437. else
  438. {
  439. count=1;
  440. do
  441. {
  442. DeciCurrent=iCurrent-count;
  443. DecjCurrent=jCurrent-count;
  444. IncjCurrent=jCurrent+count;
  445. if (DeciCurrent>iDestination)
  446. {
  447. if (jDestination<jCurrent)
  448. {
  449.  
  450. if (piecesBoard[DeciCurrent][DecjCurrent]=='\0')
  451. {
  452. flag=0;
  453. }
  454. else
  455. {
  456. flag=1;
  457. }
  458. }
  459. else if (jDestination>jCurrent)
  460. {
  461. if (piecesBoard[DeciCurrent][IncjCurrent]=='\0')
  462. {
  463. flag=0;
  464. }
  465. else
  466. {
  467. flag=1;
  468. }
  469. }
  470. count++;
  471.  
  472. }
  473. }
  474. while ((DeciCurrent>iDestination) && (flag==0));
  475.  
  476. if (flag==0)
  477. {
  478. return 0;
  479. }
  480. else
  481. {
  482. return 1;
  483. }
  484. }
  485.  
  486.  
  487. }
  488. else
  489. {
  490. return 1;
  491. }
  492.  
  493. }
  494.  
  495. int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  496. {
  497.  
  498. if(piecesBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  499. {
  500. if (piecesBoard[iDestination][jDestination]!='\0')
  501. {
  502. return 1;
  503. }
  504. else
  505. {
  506. return 0;
  507. }
  508. }
  509.  
  510. else if(piecesBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  511. {
  512. if (piecesBoard[iDestination][jDestination]!='\0')
  513. {
  514. return 1;
  515. }
  516. else
  517. {
  518. return 0;
  519. }
  520. }
  521.  
  522. else if(piecesBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  523. return 0;
  524.  
  525. else if(piecesBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  526. return 0;
  527.  
  528. else if(piecesBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
  529. return 0;
  530.  
  531. else if(piecesBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
  532. return 0;
  533.  
  534. else
  535. return 1;
  536.  
  537. }
  538.  
  539.  
  540.  
  541. int queen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  542. {
  543. int iDiff,jDiff;
  544. iDiff=iDestination-iCurrent;
  545. jDiff=jDestination-jCurrent;
  546.  
  547. if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& rook(iCurrent,iDestination,jCurrent,jDestination)==0)
  548.  
  549. return 0;
  550.  
  551.  
  552. else if (abs(iDiff)==abs(jDiff) && bishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  553.  
  554. return 0;
  555.  
  556. else
  557.  
  558. return 1;
  559.  
  560. }
  561.  
  562. int knight(int iCurrent,int iDestination,int jCurrent,int jDestination)
  563. {
  564. int iDiff,jDiff;
  565. iDiff=iDestination-iCurrent;
  566. jDiff=jDestination-jCurrent;
  567. if ((abs(iDiff)==2) && (abs(jDiff)==1))
  568.  
  569. return 0;
  570.  
  571. else if ((abs(jDiff)==2) && (abs(iDiff)==1))
  572.  
  573. return 0;
  574.  
  575. else
  576.  
  577. return 1;
  578.  
  579. }
  580.  
  581.  
  582. int check()
  583. {
  584. int f=0;
  585.  
  586. for(int i=0; i<8; i++)
  587. {
  588. for(int j=0; j<8; j++)
  589. {
  590. if(turn%2==1)
  591. {
  592. findKings();
  593. if(validate(i, iBlackKing, j, jBlackKing)==0)
  594. {
  595. f=1;
  596. }
  597. }
  598.  
  599. else if(turn%2==0)
  600. {
  601. findKings();
  602. if(validate(i, iWhiteKing, j, jWhiteKing)==0)
  603. {
  604. f=1;
  605. }
  606. }
  607. }
  608. }
  609.  
  610. return f;
  611. }
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618. void checkmate()
  619. {
  620. int f=0;
  621.  
  622. if(check()==1)
  623. {
  624.  
  625. for(int iC=0; iC<8; iC++)
  626. {
  627. for(int jC=0; jC<8; jC++)
  628. {
  629. if(turn%2==1)
  630. {
  631. if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
  632. {
  633. for(int iD=0; iD<8; iD++)
  634. {
  635. for(int jD=0; jD<8; jD++)
  636. {
  637. if(validatePrototype(iC,iD,jC,jD)==0)
  638. {
  639. prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  640. prototypeBoard[iC][jC] = '\0';
  641. if(checkPrototype()==0)
  642. {
  643. f=1;
  644.  
  645. }
  646. correspondPrototypeBoard();
  647. }
  648. }
  649. }
  650. }
  651. }
  652. else if(turn%2==0)
  653. {
  654. if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
  655. {
  656. for(int iD=0; iD<8; iD++)
  657. {
  658. for(int jD=0; jD<8; jD++)
  659. {
  660. if(validatePrototype(iC,iD,jC,jD)==0)
  661. {
  662. prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  663. prototypeBoard[iC][jC] = '\0';
  664. if(checkPrototype()==0)
  665. {
  666. f=1;
  667. }
  668. correspondPrototypeBoard();
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }
  675. }
  676. if(f==0)
  677. checkMateFlag=1;
  678.  
  679.  
  680. }
  681. }
  682.  
  683.  
  684. void printErrors(int f, int iCurrent, int iDestination, int jCurrent, int jDestination)
  685. {
  686. f = validate(iCurrent, iDestination, jCurrent, jDestination);
  687. switch(f)
  688. {
  689. case 0:
  690. break;
  691.  
  692. case 1:
  693. printf("Invalid Input!\n");
  694. break;
  695. case 4:
  696. printf("Empty Position!\n");
  697. break;
  698. case 5:
  699. printf("Wrong Move!\n");
  700. break;
  701. }
  702.  
  703. }
  704.  
  705.  
  706. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
  707. {
  708. char tempPiece;
  709.  
  710. int f=0, fC=0;
  711. if(validatePrototype(iCurrent,iDestination,jCurrent,jDestination)!=0)
  712. f=1;
  713. else
  714. {
  715. if(prototypeBoard[iCurrent][jCurrent]=='k') {
  716. iWhiteKing = iDestination;
  717. jWhiteKing = jDestination;
  718. fC=1;
  719. }
  720. else if(prototypeBoard[iCurrent][jCurrent]=='K') {
  721. iWhiteKing = iDestination;
  722. jWhiteKing = jDestination;
  723. fC=2;
  724. }
  725.  
  726. prototypeBoard[iDestination][jDestination] = prototypeBoard[iCurrent][jCurrent];
  727. prototypeBoard[iCurrent][jCurrent] = '\0';
  728.  
  729. turn++;
  730. if(checkPrototype()==1)
  731. {
  732. f=1;
  733. }
  734.  
  735. if(fC==1) {
  736. iWhiteKing = iCurrent;
  737. jWhiteKing = jCurrent;
  738. }
  739. else if(fC==2) {
  740. iBlackKing = iCurrent;
  741. jBlackKing = jCurrent;
  742. }
  743. correspondPrototypeBoard();
  744.  
  745. turn--;
  746. }
  747.  
  748.  
  749. return f;
  750. }
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757. void correspondPrototypeBoard()
  758. {
  759. for(int i=0; i<8; i++)
  760. {
  761. for(int j=0; j<8; j++)
  762. {
  763. prototypeBoard[i][j]=piecesBoard[i][j];
  764. }
  765. }
  766. }
  767.  
  768. int checkPrototype()
  769. {
  770.  
  771. int f=0;
  772. for(int i=0; i<8; i++)
  773. {
  774. for(int j=0; j<8; j++)
  775. {
  776. if(turn%2==1)
  777. {
  778. if(validatePrototype(i, iBlackKing, j, jBlackKing)==0)
  779. {
  780. f=1;
  781. }
  782. }
  783.  
  784. else if(turn%2==0)
  785. {
  786. if(validatePrototype(i, iWhiteKing, j, jWhiteKing)==0)
  787. {
  788. f=1;
  789. }
  790. }
  791. }
  792. }
  793.  
  794. return f;
  795. }
  796.  
  797.  
  798. int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination)
  799. {
  800. if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  801. {
  802. return 1;
  803. }
  804.  
  805.  
  806. else if (((prototypeBoard[iCurrent][jCurrent]>='a') && (prototypeBoard[iCurrent][jCurrent]<='z')) && ((prototypeBoard[iDestination][jDestination]>='a') && (prototypeBoard[iDestination][jDestination]<='z')))
  807. {
  808.  
  809. return 5;
  810. }
  811.  
  812. else if (((prototypeBoard[iCurrent][jCurrent]>='A') && (prototypeBoard[iCurrent][jCurrent]<='Z')) && ((prototypeBoard[iDestination][jDestination]>='A') && (prototypeBoard[iDestination][jDestination]<='Z')))
  813. {
  814.  
  815. return 5;
  816. }
  817.  
  818.  
  819. else if(prototypeBoard[iCurrent][jCurrent]=='\0')
  820. {
  821.  
  822. return 4;
  823. }
  824.  
  825. else if(((prototypeBoard[iCurrent][jCurrent]=='r')||(prototypeBoard[iCurrent][jCurrent]=='R')) && (prototypeRook(iCurrent, iDestination, jCurrent, jDestination)==1))
  826. {
  827.  
  828. return 5;
  829. }
  830.  
  831. else if(((prototypeBoard[iCurrent][jCurrent]=='p')||(prototypeBoard[iCurrent][jCurrent]=='P')) && (prototypePawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  832. {
  833.  
  834. return 5;
  835. }
  836.  
  837. else if(((prototypeBoard[iCurrent][jCurrent]=='b')||(prototypeBoard[iCurrent][jCurrent]=='B')) && (prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  838. {
  839.  
  840. return 5;
  841. }
  842.  
  843. else if(((prototypeBoard[iCurrent][jCurrent]=='q')||(prototypeBoard[iCurrent][jCurrent]=='Q')) && (prototypeQueen(iCurrent, iDestination, jCurrent, jDestination)==1))
  844. {
  845.  
  846. return 5;
  847. }
  848.  
  849. else if(((prototypeBoard[iCurrent][jCurrent]=='k')||(prototypeBoard[iCurrent][jCurrent]=='K')) && (prototypeKing(iCurrent, iDestination, jCurrent, jDestination)==1))
  850. {
  851.  
  852. return 5;
  853. }
  854.  
  855. else if(((prototypeBoard[iCurrent][jCurrent]=='n')||(prototypeBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  856. {
  857.  
  858. return 5;
  859. }
  860.  
  861.  
  862.  
  863. else
  864. {
  865.  
  866. return 0;
  867. }
  868.  
  869.  
  870. }
  871.  
  872.  
  873.  
  874. int prototypeRook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  875. {
  876. int count,flag=0;
  877. if((jCurrent==jDestination) && (iCurrent!= iDestination))
  878. {
  879. if (iDestination>iCurrent)
  880. {
  881. for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  882. {
  883. if (prototypeBoard[iCurrent+count][jCurrent]=='\0')
  884. {
  885. flag=0 ;
  886. }
  887. else
  888. {
  889. flag=1;
  890. }
  891. }
  892. }
  893. else
  894. {
  895. for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  896. {
  897. if (prototypeBoard[iCurrent-count][jCurrent]=='\0')
  898. {
  899. flag=0;
  900. }
  901. else
  902. {
  903. flag=1;
  904. }
  905. }
  906. }
  907. if (flag==0)
  908. {
  909. return 0;
  910. }
  911. else
  912. {
  913. return 1;
  914. }
  915. }
  916. else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  917. {
  918. if (jDestination>jCurrent)
  919. {
  920. for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  921. {
  922. if (prototypeBoard[iCurrent][jCurrent+count]=='\0')
  923. {
  924. flag=0;
  925. }
  926. else
  927. {
  928. flag=1;
  929. }
  930. }
  931. }
  932. else
  933. {
  934. for (count=1; (jCurrent-count)>jDestination; count++)
  935. {
  936. if (prototypeBoard[iCurrent][jCurrent-count]=='\0')
  937. {
  938. flag=0;
  939. }
  940. else
  941. {
  942. flag=1;
  943. }
  944. }
  945. }
  946. if (flag==0)
  947. {
  948. return 0;
  949. }
  950. else
  951. {
  952. return 1;
  953. }
  954. }
  955. else
  956. {
  957. return 1;
  958. }
  959. }
  960.  
  961.  
  962. int prototypeKing(int iCurrent,int iDestination,int jCurrent,int jDestination)
  963. {
  964. int iDiff,jDiff;
  965. iDiff=iCurrent-iDestination;
  966. jDiff=jCurrent-jDestination;
  967. if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  968. {
  969.  
  970. return 0;
  971. }
  972. else
  973. {
  974. return 1;
  975. }
  976. }
  977.  
  978. int prototypeBishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  979. {
  980. int iDiff,jDiff;
  981. int count=1,flag=0;
  982. iDiff=iDestination-iCurrent;
  983. jDiff=jDestination-jCurrent;
  984. int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  985.  
  986. if (abs(iDiff)==abs(jDiff))
  987. {
  988. if (iDestination>iCurrent)
  989. {
  990. count=1;
  991. do
  992. {
  993. DecjCurrent=jCurrent-count;
  994. IncjCurrent=jCurrent+count;
  995. InciCurrent=iCurrent+count;
  996. if (InciCurrent<iDestination)
  997. {
  998. if (jDestination<jCurrent)
  999. {
  1000. if (prototypeBoard[InciCurrent][DecjCurrent]=='\0')
  1001. {
  1002. flag=0;
  1003. }
  1004. else
  1005. {
  1006. flag=1;
  1007. }
  1008.  
  1009. }
  1010. else if (jDestination>jCurrent)
  1011. {
  1012. if (prototypeBoard[InciCurrent][IncjCurrent]=='\0')
  1013. {
  1014. flag=0;
  1015. }
  1016. else
  1017. {
  1018. flag=1;
  1019. }
  1020. }
  1021. count++;
  1022. }
  1023. }
  1024. while ((InciCurrent<iDestination) && (flag==0));
  1025. if (flag==0)
  1026. {
  1027. return 0;
  1028. }
  1029. else
  1030. {
  1031. return 1;
  1032. }
  1033. }
  1034.  
  1035. else
  1036. {
  1037. count=1;
  1038. do
  1039. {
  1040. DeciCurrent=iCurrent-count;
  1041. DecjCurrent=jCurrent-count;
  1042. IncjCurrent=jCurrent+count;
  1043. if (DeciCurrent>iDestination)
  1044. {
  1045. if (jDestination<jCurrent)
  1046. {
  1047.  
  1048. if (prototypeBoard[DeciCurrent][DecjCurrent]=='\0')
  1049. {
  1050. flag=0;
  1051. }
  1052. else
  1053. {
  1054. flag=1;
  1055. }
  1056. }
  1057. else if (jDestination>jCurrent)
  1058. {
  1059. if (prototypeBoard[DeciCurrent][IncjCurrent]=='\0')
  1060. {
  1061. flag=0;
  1062. }
  1063. else
  1064. {
  1065. flag=1;
  1066. }
  1067. }
  1068. count++;
  1069.  
  1070. }
  1071. }
  1072. while ((DeciCurrent>iDestination) && (flag==0));
  1073.  
  1074. if (flag==0)
  1075. {
  1076. return 0;
  1077. }
  1078. else
  1079. {
  1080. return 1;
  1081. }
  1082. }
  1083.  
  1084.  
  1085. }
  1086. else
  1087. {
  1088. return 1;
  1089. }
  1090.  
  1091. }
  1092.  
  1093. int prototypePawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1094. {
  1095.  
  1096. if(prototypeBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  1097. {
  1098. if (prototypeBoard[iDestination][jDestination]!='\0')
  1099. {
  1100. return 1;
  1101. }
  1102. else
  1103. {
  1104. return 0;
  1105. }
  1106. }
  1107.  
  1108. else if(prototypeBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  1109. {
  1110. if (prototypeBoard[iDestination][jDestination]!='\0')
  1111. {
  1112. return 1;
  1113. }
  1114. else
  1115. {
  1116. return 0;
  1117. }
  1118. }
  1119.  
  1120. else if(prototypeBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='A' && prototypeBoard[iDestination][jDestination]<='Z')
  1121. return 0;
  1122.  
  1123. else if(prototypeBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='a' && prototypeBoard[iDestination][jDestination]<='z')
  1124. return 0;
  1125.  
  1126. else if(prototypeBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
  1127. return 0;
  1128.  
  1129. else if(prototypeBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
  1130. return 0;
  1131.  
  1132. else
  1133. return 1;
  1134.  
  1135. }
  1136.  
  1137.  
  1138.  
  1139. int prototypeQueen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1140. {
  1141. int iDiff,jDiff;
  1142. iDiff=iDestination-iCurrent;
  1143. jDiff=jDestination-jCurrent;
  1144.  
  1145. if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& prototypeRook(iCurrent,iDestination,jCurrent,jDestination)==0)
  1146.  
  1147. return 0;
  1148.  
  1149.  
  1150. else if (abs(iDiff)==abs(jDiff) && bishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  1151.  
  1152. return 0;
  1153.  
  1154. else
  1155.  
  1156. return 1;
  1157.  
  1158. }
  1159.  
  1160.  
  1161. void findKings() {
  1162. for(int i=0; i<8; i++) {
  1163. for(int j=0; j<8; j++) {
  1164. if(piecesBoard[i][j]=='k') {
  1165. iWhiteKing = i;
  1166. jWhiteKing = j;
  1167. }
  1168. else if(piecesBoard[i][j]=='K') {
  1169. iBlackKing = i;
  1170. jWhiteKing = j;
  1171. }
  1172. }
  1173. }
  1174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement