Advertisement
anas_harby

Untitled

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