anas_harby

Untitled

Dec 20th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.50 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.  
  89.     if(turn%2!=0 && (piecesBoard[iCurrent][jCurrent]>='A' && piecesBoard[iCurrent][jCurrent]<='Z'))
  90.     {
  91.         printf("White Pieces Turn!\n");
  92.         userInput();
  93.     }
  94.  
  95.     else if(turn%2==0 && (piecesBoard[iCurrent][jCurrent]>='a' && piecesBoard[iCurrent][jCurrent]<='z'))
  96.     {
  97.         printf("Black Pieces Turn!\n");
  98.         userInput();
  99.     }
  100.  
  101.     else if(validate(iCurrent, iDestination, jCurrent, jDestination)!=0)
  102.     {
  103.         printErrors(validate(iCurrent, iDestination, jCurrent, jDestination), iCurrent, iDestination, jCurrent, jDestination);
  104.         userInput();
  105.  
  106.     }
  107.  
  108.  
  109.  
  110.     else if(validateCheck(iCurrent,iDestination,jCurrent,jDestination)==1)
  111.     {
  112.         if(turn%2==1)
  113.             printf("WHITE KING WILL BE CHECKED!\n");
  114.         else if(turn%2==0)
  115.             printf("BLACK KING WILL BE CHECKED!\n");
  116.         userInput();
  117.     }
  118.  
  119.     else if(validate(iCurrent, iDestination, jCurrent, jDestination)==0)
  120.     {
  121.  
  122.         capture(iCurrent, iDestination, jCurrent, jDestination);
  123.         move(iCurrent, iDestination, jCurrent, jDestination);
  124.         findKings();
  125.         correspondPrototypeBoard();
  126.         checkmate();
  127.  
  128.  
  129.  
  130.     }
  131. }
  132.  
  133.  
  134. void move(int iCurrent, int iDestination, int jCurrent, int jDestination)
  135. {
  136.  
  137.     piecesBoard[iDestination][jDestination] = piecesBoard[iCurrent][jCurrent];
  138.     piecesBoard[iCurrent][jCurrent]='\0';
  139. }
  140.  
  141. void printBoard()
  142. {
  143.     board[0][0] = ' ', board[0][9] = ' ', board[9][0] = ' ', board[9][9] = ' ';
  144.     for(int i=1; i<9; i++)
  145.     {
  146.         board[i][0] = 9-i;
  147.         board[i][9]= 9-i;
  148.         board[0][i] = 'A' +i-1;
  149.         board[9][i] = 'A' +i-1;
  150.  
  151.     }
  152.  
  153.     for(int i=0; i<10; i++)
  154.     {
  155.         for(int j=0; j<10; j++)
  156.         {
  157.             if((board[i][j]>='A' && board[i][j] <= 'Z') || board[i][j]==' ')
  158.                 printf("%c\t", board[i][j]);
  159.             else if((i>=1 && i<=8) && (j>=1 && j<=8))
  160.             {
  161.                 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'))
  162.                     printf("%c\t", piecesBoard[i-1][j-1]);
  163.                 else
  164.                     printf("%c\t", BWBoard[i-1][j-1]);
  165.             }
  166.             else
  167.                 printf("%d\t", (char)board[i][j]);
  168.         }
  169.         if(i==0||i==8)
  170.             printf("\n\n\n");
  171.         else
  172.             printf("\n\n");
  173.     }
  174.     printf("\n\n\n");
  175.  
  176. }
  177.  
  178.  
  179. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination)
  180. {
  181.     if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  182.     {
  183.         return 1;
  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]>='A') && (piecesBoard[iCurrent][jCurrent]<='Z')) && ((piecesBoard[iDestination][jDestination]>='A') && (piecesBoard[iDestination][jDestination]<='Z')))
  193.     {
  194.  
  195.         return 5;
  196.     }
  197.  
  198.     else if(piecesBoard[iCurrent][jCurrent]=='\0')
  199.     {
  200.  
  201.         return 4;
  202.     }
  203.  
  204.     else if(((piecesBoard[iCurrent][jCurrent]=='r')||(piecesBoard[iCurrent][jCurrent]=='R')) && (rook(iCurrent, iDestination, jCurrent, jDestination)==1))
  205.     {
  206.  
  207.         return 5;
  208.     }
  209.  
  210.     else if(((piecesBoard[iCurrent][jCurrent]=='p')||(piecesBoard[iCurrent][jCurrent]=='P')) && (pawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  211.     {
  212.  
  213.         return 5;
  214.     }
  215.  
  216.     else if(((piecesBoard[iCurrent][jCurrent]=='b')||(piecesBoard[iCurrent][jCurrent]=='B')) && (bishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  217.     {
  218.  
  219.         return 5;
  220.     }
  221.  
  222.     else if(((piecesBoard[iCurrent][jCurrent]=='q')||(piecesBoard[iCurrent][jCurrent]=='Q')) && (queen(iCurrent, iDestination, jCurrent, jDestination)==1))
  223.     {
  224.  
  225.         return 5;
  226.     }
  227.  
  228.     else if(((piecesBoard[iCurrent][jCurrent]=='k')||(piecesBoard[iCurrent][jCurrent]=='K')) && (king(iCurrent, iDestination, jCurrent, jDestination)==1))
  229.     {
  230.  
  231.         return 5;
  232.     }
  233.  
  234.     else if(((piecesBoard[iCurrent][jCurrent]=='n')||(piecesBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  235.     {
  236.  
  237.         return 5;
  238.     }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.     else
  245.     {
  246.  
  247.         return 0;
  248.     }
  249.  
  250.  
  251. }
  252.  
  253.  
  254.  
  255.  
  256.  
  257. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination)
  258. {
  259.     if(piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  260.     {
  261.         capturedWhite[countWhite] = piecesBoard[iDestination][jDestination];
  262.         countWhite++;
  263.     }
  264.     else if(piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  265.     {
  266.         capturedBlack[countBlack] = piecesBoard[iDestination][jDestination];
  267.         countBlack++;
  268.     }
  269.     system("cls");
  270.     printf("\nCaptured White Pieces: ");
  271.     for(int i=0; i<countWhite; i++)
  272.         printf("%c ", capturedWhite[i]);
  273.     printf("\nCaptured Black Pieces: ");
  274.     for(int i=0; i<countBlack; i++)
  275.         printf("%c ", capturedBlack[i]);
  276.     printf("\n\n");
  277. }
  278.  
  279.  
  280.  
  281.  
  282. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  283. {
  284.     int count,flag=0;
  285.     if((jCurrent==jDestination) && (iCurrent!= iDestination))
  286.     {
  287.         if (iDestination>iCurrent)
  288.         {
  289.             for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  290.             {
  291.                 if (piecesBoard[iCurrent+count][jCurrent]=='\0')
  292.                 {
  293.                     flag=0 ;
  294.                 }
  295.                 else
  296.                 {
  297.                     flag=1;
  298.                 }
  299.             }
  300.         }
  301.         else
  302.         {
  303.             for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  304.             {
  305.                 if (piecesBoard[iCurrent-count][jCurrent]=='\0')
  306.                 {
  307.                     flag=0;
  308.                 }
  309.                 else
  310.                 {
  311.                     flag=1;
  312.                 }
  313.             }
  314.         }
  315.         if (flag==0)
  316.         {
  317.             return 0;
  318.         }
  319.         else
  320.         {
  321.             return 1;
  322.         }
  323.     }
  324.     else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  325.     {
  326.         if (jDestination>jCurrent)
  327.         {
  328.             for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  329.             {
  330.                 if (piecesBoard[iCurrent][jCurrent+count]=='\0')
  331.                 {
  332.                     flag=0;
  333.                 }
  334.                 else
  335.                 {
  336.                     flag=1;
  337.                 }
  338.             }
  339.         }
  340.         else
  341.         {
  342.             for (count=1; (jCurrent-count)>jDestination; count++)
  343.             {
  344.                 if (piecesBoard[iCurrent][jCurrent-count]=='\0')
  345.                 {
  346.                     flag=0;
  347.                 }
  348.                 else
  349.                 {
  350.                     flag=1;
  351.                 }
  352.             }
  353.         }
  354.         if (flag==0)
  355.         {
  356.             return 0;
  357.         }
  358.         else
  359.         {
  360.             return 1;
  361.         }
  362.     }
  363.     else
  364.     {
  365.         return 1;
  366.     }
  367. }
  368.  
  369.  
  370. int king(int iCurrent,int iDestination,int jCurrent,int jDestination)
  371. {
  372.     int iDiff,jDiff;
  373.     iDiff=iCurrent-iDestination;
  374.     jDiff=jCurrent-jDestination;
  375.     if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  376.     {
  377.             return 0;
  378.  
  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.                 findKings();
  599.                 if(validate(i, iBlackKing, j, jBlackKing)==0)
  600.                 {
  601.                     f=1;
  602.                 }
  603.             }
  604.  
  605.             else if(turn%2==0)
  606.             {
  607.                 findKings();
  608.                 if(validate(i, iWhiteKing, j, jWhiteKing)==0)
  609.                 {
  610.                     f=1;
  611.                 }
  612.             }
  613.         }
  614.     }
  615.     return f;
  616. }
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623. void checkmate()
  624. {
  625.     int f=0;
  626.  
  627.     if(check()==1)
  628.     {
  629.  
  630.         for(int iC=0; iC<8; iC++)
  631.         {
  632.             for(int jC=0; jC<8; jC++)
  633.             {
  634.                 if(turn%2==1)
  635.                 {
  636.                     if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
  637.                     {
  638.                         for(int iD=0; iD<8; iD++)
  639.                         {
  640.                             for(int jD=0; jD<8; jD++)
  641.                             {
  642.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  643.                                 {
  644.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  645.                                     prototypeBoard[iC][jC] = '\0';
  646.                                     if(checkPrototype()==0)
  647.                                     {
  648.                                         f=1;
  649.  
  650.                                     }
  651.                                     correspondPrototypeBoard();
  652.                                 }
  653.                             }
  654.                         }
  655.                     }
  656.                 }
  657.                 else if(turn%2==0)
  658.                 {
  659.                     if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
  660.                     {
  661.                         for(int iD=0; iD<8; iD++)
  662.                         {
  663.                             for(int jD=0; jD<8; jD++)
  664.                             {
  665.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  666.                                 {
  667.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  668.                                     prototypeBoard[iC][jC] = '\0';
  669.                                     if(checkPrototype()==0)
  670.                                     {
  671.                                         f=1;
  672.                                     }
  673.                                     correspondPrototypeBoard();
  674.                                 }
  675.                             }
  676.                         }
  677.                     }
  678.                 }
  679.             }
  680.         }
  681.         if(f==0)
  682.             checkMateFlag=1;
  683.  
  684.  
  685.     }
  686. }
  687.  
  688.  
  689. void printErrors(int f, int iCurrent, int iDestination, int jCurrent, int jDestination)
  690. {
  691.     f = validate(iCurrent, iDestination, jCurrent, jDestination);
  692.     switch(f)
  693.     {
  694.     case 0:
  695.         break;
  696.  
  697.     case 1:
  698.         printf("Invalid Input!\n");
  699.         break;
  700.     case 4:
  701.         printf("Empty Position!\n");
  702.         break;
  703.     case 5:
  704.         printf("Wrong Move!\n");
  705.         break;
  706.     }
  707.  
  708. }
  709.  
  710.  
  711. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
  712. {
  713.     char tempPiece;
  714.  
  715.     int f=0, fC=0;
  716.     if(validatePrototype(iCurrent,iDestination,jCurrent,jDestination)!=0)
  717.         f=1;
  718.     else
  719.     {
  720.         if(prototypeBoard[iCurrent][jCurrent]=='k') {
  721.             iWhiteKing = iDestination;
  722.             jWhiteKing = jDestination;
  723.             fC=1;
  724.         }
  725.         else if(prototypeBoard[iCurrent][jCurrent]=='K') {
  726.             iBlackKing = iDestination;
  727.             jBlackKing = jDestination;
  728.             fC=2;
  729.         }
  730.  
  731.         prototypeBoard[iDestination][jDestination] = prototypeBoard[iCurrent][jCurrent];
  732.         prototypeBoard[iCurrent][jCurrent] = '\0';
  733.  
  734.         turn++;
  735.         if(checkPrototype()==1)
  736.         {
  737.             f=1;
  738.         }
  739.  
  740.         if(fC==1) {
  741.             iWhiteKing = iCurrent;
  742.             jWhiteKing = jCurrent;
  743.         }
  744.         else if(fC==2) {
  745.             iBlackKing = iCurrent;
  746.             jBlackKing = jCurrent;
  747.         }
  748.         correspondPrototypeBoard();
  749.  
  750.         turn--;
  751.     }
  752.  
  753.  
  754.     return f;
  755. }
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762. void correspondPrototypeBoard()
  763. {
  764.     for(int i=0; i<8; i++)
  765.     {
  766.         for(int j=0; j<8; j++)
  767.         {
  768.             prototypeBoard[i][j]=piecesBoard[i][j];
  769.         }
  770.     }
  771. }
  772.  
  773. int checkPrototype()
  774. {
  775.  
  776.     int f=0;
  777.     for(int i=0; i<8; i++)
  778.     {
  779.         for(int j=0; j<8; j++)
  780.         {
  781.             if(turn%2==1)
  782.             {
  783.                 if(validatePrototype(i, iBlackKing, j, jBlackKing)==0)
  784.                 {
  785.                     f=1;
  786.                 }
  787.             }
  788.  
  789.             else if(turn%2==0)
  790.             {
  791.                 if(validatePrototype(i, iWhiteKing, j, jWhiteKing)==0)
  792.                 {
  793.                     f=1;
  794.                 }
  795.             }
  796.         }
  797.     }
  798.  
  799.     return f;
  800. }
  801.  
  802.  
  803. int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination)
  804. {
  805.     if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  806.     {
  807.         return 1;
  808.     }
  809.  
  810.  
  811.     else if (((prototypeBoard[iCurrent][jCurrent]>='a') && (prototypeBoard[iCurrent][jCurrent]<='z')) && ((prototypeBoard[iDestination][jDestination]>='a') && (prototypeBoard[iDestination][jDestination]<='z')))
  812.     {
  813.  
  814.         return 5;
  815.     }
  816.  
  817.     else if (((prototypeBoard[iCurrent][jCurrent]>='A') && (prototypeBoard[iCurrent][jCurrent]<='Z')) && ((prototypeBoard[iDestination][jDestination]>='A') && (prototypeBoard[iDestination][jDestination]<='Z')))
  818.     {
  819.  
  820.         return 5;
  821.     }
  822.  
  823.  
  824.     else if(prototypeBoard[iCurrent][jCurrent]=='\0')
  825.     {
  826.  
  827.         return 4;
  828.     }
  829.  
  830.     else if(((prototypeBoard[iCurrent][jCurrent]=='r')||(prototypeBoard[iCurrent][jCurrent]=='R')) && (prototypeRook(iCurrent, iDestination, jCurrent, jDestination)==1))
  831.     {
  832.  
  833.         return 5;
  834.     }
  835.  
  836.     else if(((prototypeBoard[iCurrent][jCurrent]=='p')||(prototypeBoard[iCurrent][jCurrent]=='P')) && (prototypePawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  837.     {
  838.  
  839.         return 5;
  840.     }
  841.  
  842.     else if(((prototypeBoard[iCurrent][jCurrent]=='b')||(prototypeBoard[iCurrent][jCurrent]=='B')) && (prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  843.     {
  844.  
  845.         return 5;
  846.     }
  847.  
  848.     else if(((prototypeBoard[iCurrent][jCurrent]=='q')||(prototypeBoard[iCurrent][jCurrent]=='Q')) && (prototypeQueen(iCurrent, iDestination, jCurrent, jDestination)==1))
  849.     {
  850.  
  851.         return 5;
  852.     }
  853.  
  854.     else if(((prototypeBoard[iCurrent][jCurrent]=='k')||(prototypeBoard[iCurrent][jCurrent]=='K')) && (prototypeKing(iCurrent, iDestination, jCurrent, jDestination)==1))
  855.     {
  856.  
  857.         return 5;
  858.     }
  859.  
  860.     else if(((prototypeBoard[iCurrent][jCurrent]=='n')||(prototypeBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  861.     {
  862.  
  863.         return 5;
  864.     }
  865.  
  866.  
  867.  
  868.     else
  869.     {
  870.  
  871.         return 0;
  872.     }
  873.  
  874.  
  875. }
  876.  
  877.  
  878.  
  879. int prototypeRook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  880. {
  881.     int count,flag=0;
  882.     if((jCurrent==jDestination) && (iCurrent!= iDestination))
  883.     {
  884.         if (iDestination>iCurrent)
  885.         {
  886.             for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  887.             {
  888.                 if (prototypeBoard[iCurrent+count][jCurrent]=='\0')
  889.                 {
  890.                     flag=0 ;
  891.                 }
  892.                 else
  893.                 {
  894.                     flag=1;
  895.                 }
  896.             }
  897.         }
  898.         else
  899.         {
  900.             for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  901.             {
  902.                 if (prototypeBoard[iCurrent-count][jCurrent]=='\0')
  903.                 {
  904.                     flag=0;
  905.                 }
  906.                 else
  907.                 {
  908.                     flag=1;
  909.                 }
  910.             }
  911.         }
  912.         if (flag==0)
  913.         {
  914.             return 0;
  915.         }
  916.         else
  917.         {
  918.             return 1;
  919.         }
  920.     }
  921.     else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  922.     {
  923.         if (jDestination>jCurrent)
  924.         {
  925.             for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  926.             {
  927.                 if (prototypeBoard[iCurrent][jCurrent+count]=='\0')
  928.                 {
  929.                     flag=0;
  930.                 }
  931.                 else
  932.                 {
  933.                     flag=1;
  934.                 }
  935.             }
  936.         }
  937.         else
  938.         {
  939.             for (count=1; (jCurrent-count)>jDestination; count++)
  940.             {
  941.                 if (prototypeBoard[iCurrent][jCurrent-count]=='\0')
  942.                 {
  943.                     flag=0;
  944.                 }
  945.                 else
  946.                 {
  947.                     flag=1;
  948.                 }
  949.             }
  950.         }
  951.         if (flag==0)
  952.         {
  953.             return 0;
  954.         }
  955.         else
  956.         {
  957.             return 1;
  958.         }
  959.     }
  960.     else
  961.     {
  962.         return 1;
  963.     }
  964. }
  965.  
  966.  
  967. int prototypeKing(int iCurrent,int iDestination,int jCurrent,int jDestination)
  968. {
  969.     int iDiff,jDiff;
  970.     iDiff=iCurrent-iDestination;
  971.     jDiff=jCurrent-jDestination;
  972.     if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  973.     {
  974.  
  975.         return 0;
  976.     }
  977.     else
  978.     {
  979.         return 1;
  980.     }
  981. }
  982.  
  983. int prototypeBishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  984. {
  985.     int iDiff,jDiff;
  986.     int count=1,flag=0;
  987.     iDiff=iDestination-iCurrent;
  988.     jDiff=jDestination-jCurrent;
  989.     int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  990.  
  991.     if (abs(iDiff)==abs(jDiff))
  992.     {
  993.         if (iDestination>iCurrent)
  994.         {
  995.             count=1;
  996.             do
  997.             {
  998.                 DecjCurrent=jCurrent-count;
  999.                 IncjCurrent=jCurrent+count;
  1000.                 InciCurrent=iCurrent+count;
  1001.                 if (InciCurrent<iDestination)
  1002.                 {
  1003.                     if (jDestination<jCurrent)
  1004.                     {
  1005.                         if (prototypeBoard[InciCurrent][DecjCurrent]=='\0')
  1006.                         {
  1007.                             flag=0;
  1008.                         }
  1009.                         else
  1010.                         {
  1011.                             flag=1;
  1012.                         }
  1013.  
  1014.                     }
  1015.                     else if (jDestination>jCurrent)
  1016.                     {
  1017.                         if (prototypeBoard[InciCurrent][IncjCurrent]=='\0')
  1018.                         {
  1019.                             flag=0;
  1020.                         }
  1021.                         else
  1022.                         {
  1023.                             flag=1;
  1024.                         }
  1025.                     }
  1026.                     count++;
  1027.                 }
  1028.             }
  1029.             while ((InciCurrent<iDestination) && (flag==0));
  1030.             if (flag==0)
  1031.             {
  1032.                 return 0;
  1033.             }
  1034.             else
  1035.             {
  1036.                 return 1;
  1037.             }
  1038.         }
  1039.  
  1040.         else
  1041.         {
  1042.             count=1;
  1043.             do
  1044.             {
  1045.                 DeciCurrent=iCurrent-count;
  1046.                 DecjCurrent=jCurrent-count;
  1047.                 IncjCurrent=jCurrent+count;
  1048.                 if (DeciCurrent>iDestination)
  1049.                 {
  1050.                     if (jDestination<jCurrent)
  1051.                     {
  1052.  
  1053.                         if (prototypeBoard[DeciCurrent][DecjCurrent]=='\0')
  1054.                         {
  1055.                             flag=0;
  1056.                         }
  1057.                         else
  1058.                         {
  1059.                             flag=1;
  1060.                         }
  1061.                     }
  1062.                     else if (jDestination>jCurrent)
  1063.                     {
  1064.                         if (prototypeBoard[DeciCurrent][IncjCurrent]=='\0')
  1065.                         {
  1066.                             flag=0;
  1067.                         }
  1068.                         else
  1069.                         {
  1070.                             flag=1;
  1071.                         }
  1072.                     }
  1073.                     count++;
  1074.  
  1075.                 }
  1076.             }
  1077.             while ((DeciCurrent>iDestination) && (flag==0));
  1078.  
  1079.             if (flag==0)
  1080.             {
  1081.                 return 0;
  1082.             }
  1083.             else
  1084.             {
  1085.                 return 1;
  1086.             }
  1087.         }
  1088.  
  1089.  
  1090.     }
  1091.     else
  1092.     {
  1093.         return 1;
  1094.     }
  1095.  
  1096. }
  1097.  
  1098. int prototypePawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1099. {
  1100.  
  1101.     if(prototypeBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  1102.     {
  1103.         if (prototypeBoard[iDestination][jDestination]!='\0')
  1104.         {
  1105.             return 1;
  1106.         }
  1107.         else
  1108.         {
  1109.             return 0;
  1110.         }
  1111.     }
  1112.  
  1113.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  1114.     {
  1115.         if (prototypeBoard[iDestination][jDestination]!='\0')
  1116.         {
  1117.             return 1;
  1118.         }
  1119.         else
  1120.         {
  1121.             return 0;
  1122.         }
  1123.     }
  1124.  
  1125.     else if(prototypeBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='A' && prototypeBoard[iDestination][jDestination]<='Z')
  1126.         return 0;
  1127.  
  1128.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='a' && prototypeBoard[iDestination][jDestination]<='z')
  1129.         return 0;
  1130.  
  1131.     else if(prototypeBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
  1132.         return 0;
  1133.  
  1134.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
  1135.         return 0;
  1136.  
  1137.     else
  1138.         return 1;
  1139.  
  1140. }
  1141.  
  1142.  
  1143.  
  1144. int prototypeQueen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1145. {
  1146.     int iDiff,jDiff;
  1147.     iDiff=iDestination-iCurrent;
  1148.     jDiff=jDestination-jCurrent;
  1149.  
  1150.     if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& prototypeRook(iCurrent,iDestination,jCurrent,jDestination)==0)
  1151.  
  1152.         return 0;
  1153.  
  1154.  
  1155.     else if (abs(iDiff)==abs(jDiff) && prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  1156.  
  1157.         return 0;
  1158.  
  1159.     else
  1160.  
  1161.         return 1;
  1162.  
  1163. }
  1164.  
  1165.  
  1166. void findKings() {
  1167.     for(int i=0; i<8; i++) {
  1168.         for(int j=0; j<8; j++) {
  1169.             if(piecesBoard[i][j]=='k') {
  1170.                 iWhiteKing = i;
  1171.                 jWhiteKing = j;
  1172.             }
  1173.             else if(piecesBoard[i][j]=='K') {
  1174.                 iBlackKing = i;
  1175.                 jWhiteKing = j;
  1176.             }
  1177.         }
  1178.     }
  1179. }
Add Comment
Please, Sign In to add comment