anas_harby

Untitled

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