Advertisement
anas_harby

Untitled

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