Advertisement
anas_harby

Untitled

Dec 23rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 44.41 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, countBlack=0, countWhite=0;
  11. char capturedBlack[16], capturedWhite[16];
  12.  
  13. void initializeBW();
  14. void userInput();
  15. void move(int iCurrent, int iDestination, int jCurrent, int jDestination);
  16. void printBoard();
  17. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination);
  18. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination);
  19. int printErrors(int iCurrent, int iDestination, int jCurrent, int jDestination, char inp[1000]);
  20.  
  21. int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination);
  22. int knight(int iCurrent, int iDestination, int jCurrent, int jDestination);
  23. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination);
  24. int bishop(int iCurrent, int iDestination, int jCurrent, int jDestination);
  25. int queen(int iCurrent, int iDestination, int jCurrent, int jDestination);
  26. int king(int iCurrent, int iDestination, int jCurrent, int jDestination);
  27.  
  28. int iBlackKing=0, iWhiteKing=7, jBlackKing=4, jWhiteKing=4;
  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. void processUndo();
  37. int undo[500] = {0};
  38. int movesCount=0;
  39. int capturingTrack[500], captureTurn=0;
  40. int promotionTrack[500], promoteTurn=0;
  41. char promotionPieces[500];
  42. void getInput();
  43. void processRedo();
  44. int promote(int iCurrent, int jCurrent, char type);
  45. int predictPromotion();
  46. int flag=0;
  47.  
  48. void stalemate();
  49. int checkPrototypeStalemate();
  50.  
  51. FILE*log1;
  52. FILE*log2;
  53. FILE*log3;
  54. FILE*log4;
  55. FILE*log5;
  56. FILE*log6;
  57. void save();
  58. void load();
  59.  
  60.  
  61. int main()
  62. {
  63.     initializeBW();
  64.  
  65.     while(!checkmateFlag)
  66.     {
  67.  
  68.         printf("\nCaptured White Pieces: ");
  69.         for(int i=0; i<countWhite; i++)
  70.             printf("%c ", capturedWhite[i]);
  71.         printf("\nCaptured Black Pieces: ");
  72.         for(int i=0; i<countBlack; i++)
  73.             printf("%c ", capturedBlack[i]);
  74.         printf("\n\n");
  75.         printBoard();
  76.         findKings();
  77.         if(check()==0){
  78.             stalemate();
  79.         }
  80.         turn++;
  81.         userInput();
  82.         system("cls");
  83.     }
  84.  
  85.     printBoard();
  86.     if(checkmateFlag==1)
  87.         printf("CHECKMATE!\n");
  88. //    else if(checkmateFlag==2)
  89. //        printf("STALEMATE!\n");
  90. //
  91. }
  92.  
  93.  
  94. void initializeBW ()
  95. {
  96.     char c1 = '_', c2= '.';
  97.     for(int i=0; i<8; i++)
  98.     {
  99.         for(int j=0; j<8; j+=2)
  100.         {
  101.             BWBoard[i][j] = c1;
  102.             BWBoard[i][j+1] = c2;
  103.         }
  104.         char temp = c1;
  105.         c1 = c2;
  106.         c2 = temp;
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. void userInput()
  113. {
  114.     char inp[1000];
  115.     int iCurrent, iDestination, jCurrent, jDestination;
  116.     int f=0;
  117.     while(!f)
  118.     {
  119.         gets(inp);
  120.  
  121.         if(strcmp("SAVE", inp)==0 || strcmp("save", inp)==0)
  122.         {
  123.             save();
  124.         }
  125.         else if(strcmp("LOAD", inp)==0 || strcmp("load", inp)==0)
  126.         {
  127.             load();
  128.             printBoard();
  129.  
  130.         }
  131.  
  132.         else if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0 || strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
  133.         {
  134.             if(printErrors(iCurrent,iDestination,jCurrent,jDestination, inp)==0)
  135.                 f=1;
  136.         }
  137.         else
  138.         {
  139.             jCurrent = (int)inp[0] - (int)'A';
  140.             iCurrent = (int)inp[1] - (int)'0';
  141.             jDestination = (int)inp[2] - (int)'A';
  142.             iDestination = (int)inp[3] - (int)'0';
  143.             iDestination = 8 - iDestination;
  144.             iCurrent = 8 - iCurrent;
  145.             if(printErrors(iCurrent,iDestination,jCurrent,jDestination, inp)==0)
  146.                 f=1;
  147.         }
  148.     }
  149.  
  150.     if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0)
  151.         processUndo();
  152.     else if(strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
  153.         processRedo();
  154.     else
  155.     {
  156.         capture(iCurrent, iDestination, jCurrent, jDestination);
  157.         trackUndo(iCurrent,iDestination,jCurrent,jDestination);
  158.         if(strlen(inp)==5 && promote(iCurrent,jCurrent,inp[4])==1)
  159.         {
  160.             move(iCurrent, iDestination, jCurrent, jDestination);
  161.             piecesBoard[iDestination][jDestination]= inp[4];
  162.             promotionTrack[promoteTurn] = turn;
  163.             promotionPieces[promoteTurn] = inp[4];
  164.             promoteTurn++;
  165.  
  166.         }
  167.         else
  168.             move(iCurrent, iDestination, jCurrent, jDestination);
  169.         findKings();
  170.         correspondPrototypeBoard();
  171.  
  172.         checkmate();
  173.     }
  174.  
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181. void move(int iCurrent, int iDestination, int jCurrent, int jDestination)
  182. {
  183.  
  184.     piecesBoard[iDestination][jDestination] = piecesBoard[iCurrent][jCurrent];
  185.     piecesBoard[iCurrent][jCurrent]='\0';
  186. }
  187.  
  188. void printBoard()
  189. {
  190.     board[0][0] = ' ', board[0][9] = ' ', board[9][0] = ' ', board[9][9] = ' ';
  191.     for(int i=1; i<9; i++)
  192.     {
  193.         board[i][0] = 9-i;
  194.         board[i][9]= 9-i;
  195.         board[0][i] = 'A' +i-1;
  196.         board[9][i] = 'A' +i-1;
  197.  
  198.     }
  199.  
  200.     for(int i=0; i<10; i++)
  201.     {
  202.         for(int j=0; j<10; j++)
  203.         {
  204.             if((board[i][j]>='A' && board[i][j] <= 'Z') || board[i][j]==' ')
  205.                 printf("%c\t", board[i][j]);
  206.             else if((i>=1 && i<=8) && (j>=1 && j<=8))
  207.             {
  208.                 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'))
  209.                     printf("%c\t", piecesBoard[i-1][j-1]);
  210.                 else
  211.                     printf("%c\t", BWBoard[i-1][j-1]);
  212.             }
  213.             else
  214.                 printf("%d\t", (char)board[i][j]);
  215.         }
  216.         if(i==0||i==8)
  217.             printf("\n\n\n");
  218.         else
  219.             printf("\n\n");
  220.     }
  221.     printf("\n\n\n");
  222.  
  223. }
  224.  
  225.  
  226. int validate(int iCurrent, int iDestination, int jCurrent, int jDestination)
  227. {
  228.     if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  229.     {
  230.         return 1;
  231.     }
  232.  
  233.     else if (((piecesBoard[iCurrent][jCurrent]>='a') && (piecesBoard[iCurrent][jCurrent]<='z')) && ((piecesBoard[iDestination][jDestination]>='a') && (piecesBoard[iDestination][jDestination]<='z')))
  234.     {
  235.  
  236.         return 5;
  237.     }
  238.  
  239.     else if (((piecesBoard[iCurrent][jCurrent]>='A') && (piecesBoard[iCurrent][jCurrent]<='Z')) && ((piecesBoard[iDestination][jDestination]>='A') && (piecesBoard[iDestination][jDestination]<='Z')))
  240.     {
  241.  
  242.         return 5;
  243.     }
  244.  
  245.     else if(piecesBoard[iCurrent][jCurrent]=='\0')
  246.     {
  247.  
  248.         return 4;
  249.     }
  250.  
  251.     else if(((piecesBoard[iCurrent][jCurrent]=='r')||(piecesBoard[iCurrent][jCurrent]=='R')) && (rook(iCurrent, iDestination, jCurrent, jDestination)==1))
  252.     {
  253.  
  254.         return 5;
  255.     }
  256.  
  257.     else if(((piecesBoard[iCurrent][jCurrent]=='p')||(piecesBoard[iCurrent][jCurrent]=='P')) && (pawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  258.     {
  259.  
  260.         return 5;
  261.     }
  262.  
  263.     else if(((piecesBoard[iCurrent][jCurrent]=='b')||(piecesBoard[iCurrent][jCurrent]=='B')) && (bishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  264.     {
  265.  
  266.         return 5;
  267.     }
  268.  
  269.     else if(((piecesBoard[iCurrent][jCurrent]=='q')||(piecesBoard[iCurrent][jCurrent]=='Q')) && (queen(iCurrent, iDestination, jCurrent, jDestination)==1))
  270.     {
  271.  
  272.         return 5;
  273.     }
  274.  
  275.     else if(((piecesBoard[iCurrent][jCurrent]=='k')||(piecesBoard[iCurrent][jCurrent]=='K')) && (king(iCurrent, iDestination, jCurrent, jDestination)==1))
  276.     {
  277.  
  278.         return 5;
  279.     }
  280.  
  281.     else if(((piecesBoard[iCurrent][jCurrent]=='n')||(piecesBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  282.     {
  283.  
  284.         return 5;
  285.     }
  286.  
  287.     else
  288.     {
  289.  
  290.         return 0;
  291.     }
  292.  
  293.  
  294. }
  295.  
  296.  
  297. int validateTurns(int iCurrent, int iDesination, int jCurrent, int jDestination)
  298. {
  299.     if(turn%2!=0 && (piecesBoard[iCurrent][jCurrent]>='A' && piecesBoard[iCurrent][jCurrent]<='Z'))
  300.         return 1;
  301.     else if(turn%2==0 && (piecesBoard[iCurrent][jCurrent]>='a' && piecesBoard[iCurrent][jCurrent]<='z'))
  302.         return 2;
  303.     else
  304.         return 0;
  305. }
  306.  
  307. int validateUndoRedo(char inp[1000])
  308. {
  309.     if(((strcmp("undo",inp) == 0)||(strcmp("UNDO",inp) ==0)) && ((turn==1)||(turn==2 && flag==2)))
  310.         return 1;
  311.     else if(((strcmp("redo",inp)==0)||(strcmp("REDO",inp) ==0)) && undo[movesCount]==0)
  312.         return 2;
  313.     else
  314.         return 0;
  315. }
  316.  
  317. int validatePromotion(char inp[1000])
  318. {
  319.     int f2=0;
  320.     if((predictPromotion()==1 && strlen(inp)==5))
  321.     {
  322.         f2=1;
  323.     }
  324.     if(strlen(inp)!=4 && !f2)
  325.     {
  326.         return 1;
  327.     }
  328.  
  329. }
  330.  
  331. void capture(int iCurrent, int iDestination, int jCurrent, int jDestination)
  332. {
  333.     if(piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  334.     {
  335.         capturedWhite[countWhite] = piecesBoard[iDestination][jDestination];
  336.         countWhite++;
  337.     }
  338.     else if(piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  339.     {
  340.         capturedBlack[countBlack] = piecesBoard[iDestination][jDestination];
  341.         countBlack++;
  342.     }
  343.  
  344.  
  345. }
  346.  
  347.  
  348.  
  349.  
  350. int rook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  351. {
  352.     int count,flag=0;
  353.     if((jCurrent==jDestination) && (iCurrent!= iDestination))
  354.     {
  355.         if (iDestination>iCurrent)
  356.         {
  357.             for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  358.             {
  359.                 if (piecesBoard[iCurrent+count][jCurrent]=='\0')
  360.                 {
  361.                     flag=0 ;
  362.                 }
  363.                 else
  364.                 {
  365.                     flag=1;
  366.                 }
  367.             }
  368.         }
  369.         else
  370.         {
  371.             for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  372.             {
  373.                 if (piecesBoard[iCurrent-count][jCurrent]=='\0')
  374.                 {
  375.                     flag=0;
  376.                 }
  377.                 else
  378.                 {
  379.                     flag=1;
  380.                 }
  381.             }
  382.         }
  383.         if (flag==0)
  384.         {
  385.             return 0;
  386.         }
  387.         else
  388.         {
  389.             return 1;
  390.         }
  391.     }
  392.     else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  393.     {
  394.         if (jDestination>jCurrent)
  395.         {
  396.             for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  397.             {
  398.                 if (piecesBoard[iCurrent][jCurrent+count]=='\0')
  399.                 {
  400.                     flag=0;
  401.                 }
  402.                 else
  403.                 {
  404.                     flag=1;
  405.                 }
  406.             }
  407.         }
  408.         else
  409.         {
  410.             for (count=1; (jCurrent-count)>jDestination; count++)
  411.             {
  412.                 if (piecesBoard[iCurrent][jCurrent-count]=='\0')
  413.                 {
  414.                     flag=0;
  415.                 }
  416.                 else
  417.                 {
  418.                     flag=1;
  419.                 }
  420.             }
  421.         }
  422.         if (flag==0)
  423.         {
  424.             return 0;
  425.         }
  426.         else
  427.         {
  428.             return 1;
  429.         }
  430.     }
  431.     else
  432.     {
  433.         return 1;
  434.     }
  435. }
  436.  
  437.  
  438. int king(int iCurrent,int iDestination,int jCurrent,int jDestination)
  439. {
  440.     int iDiff,jDiff;
  441.     iDiff=iCurrent-iDestination;
  442.     jDiff=jCurrent-jDestination;
  443.     if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  444.     {
  445.         return 0;
  446.  
  447.     }
  448.     else
  449.     {
  450.         return 1;
  451.     }
  452. }
  453.  
  454. int bishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  455. {
  456.     int iDiff,jDiff;
  457.     int count=1,flag=0;
  458.     iDiff=iDestination-iCurrent;
  459.     jDiff=jDestination-jCurrent;
  460.     int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  461.  
  462.     if (abs(iDiff)==abs(jDiff))
  463.     {
  464.         if (iDestination>iCurrent)
  465.         {
  466.             count=1;
  467.             do
  468.             {
  469.                 DecjCurrent=jCurrent-count;
  470.                 IncjCurrent=jCurrent+count;
  471.                 InciCurrent=iCurrent+count;
  472.                 if (InciCurrent<iDestination)
  473.                 {
  474.                     if (jDestination<jCurrent)
  475.                     {
  476.                         if (piecesBoard[InciCurrent][DecjCurrent]=='\0')
  477.                         {
  478.                             flag=0;
  479.                         }
  480.                         else
  481.                         {
  482.                             flag=1;
  483.                         }
  484.  
  485.                     }
  486.                     else if (jDestination>jCurrent)
  487.                     {
  488.                         if (piecesBoard[InciCurrent][IncjCurrent]=='\0')
  489.                         {
  490.                             flag=0;
  491.                         }
  492.                         else
  493.                         {
  494.                             flag=1;
  495.                         }
  496.                     }
  497.                     count++;
  498.                 }
  499.             }
  500.             while ((InciCurrent<iDestination) && (flag==0));
  501.             if (flag==0)
  502.             {
  503.                 return 0;
  504.             }
  505.             else
  506.             {
  507.                 return 1;
  508.             }
  509.         }
  510.  
  511.         else
  512.         {
  513.             count=1;
  514.             do
  515.             {
  516.                 DeciCurrent=iCurrent-count;
  517.                 DecjCurrent=jCurrent-count;
  518.                 IncjCurrent=jCurrent+count;
  519.                 if (DeciCurrent>iDestination)
  520.                 {
  521.                     if (jDestination<jCurrent)
  522.                     {
  523.  
  524.                         if (piecesBoard[DeciCurrent][DecjCurrent]=='\0')
  525.                         {
  526.                             flag=0;
  527.                         }
  528.                         else
  529.                         {
  530.                             flag=1;
  531.                         }
  532.                     }
  533.                     else if (jDestination>jCurrent)
  534.                     {
  535.                         if (piecesBoard[DeciCurrent][IncjCurrent]=='\0')
  536.                         {
  537.                             flag=0;
  538.                         }
  539.                         else
  540.                         {
  541.                             flag=1;
  542.                         }
  543.                     }
  544.                     count++;
  545.  
  546.                 }
  547.             }
  548.             while ((DeciCurrent>iDestination) && (flag==0));
  549.  
  550.             if (flag==0)
  551.             {
  552.                 return 0;
  553.             }
  554.             else
  555.             {
  556.                 return 1;
  557.             }
  558.         }
  559.  
  560.  
  561.     }
  562.     else
  563.     {
  564.         return 1;
  565.     }
  566.  
  567. }
  568.  
  569. int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  570. {
  571.  
  572.     if(piecesBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  573.     {
  574.         if (piecesBoard[iDestination][jDestination]!='\0')
  575.         {
  576.             return 1;
  577.         }
  578.         else
  579.         {
  580.             return 0;
  581.         }
  582.     }
  583.  
  584.     else if(piecesBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  585.     {
  586.         if (piecesBoard[iDestination][jDestination]!='\0')
  587.         {
  588.             return 1;
  589.         }
  590.         else
  591.         {
  592.             return 0;
  593.         }
  594.     }
  595.  
  596.     else if(piecesBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
  597.         return 0;
  598.  
  599.     else if(piecesBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
  600.         return 0;
  601.  
  602.     else if(piecesBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2) && piecesBoard[iDestination][jDestination]=='\0')
  603.         return 0;
  604.  
  605.     else if(piecesBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2) && piecesBoard[iDestination][jDestination]=='\0')
  606.         return 0;
  607.  
  608.     else
  609.         return 1;
  610.  
  611. }
  612.  
  613.  
  614.  
  615. int queen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  616. {
  617.     int iDiff,jDiff;
  618.     iDiff=iDestination-iCurrent;
  619.     jDiff=jDestination-jCurrent;
  620.  
  621.     if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& rook(iCurrent,iDestination,jCurrent,jDestination)==0)
  622.  
  623.         return 0;
  624.  
  625.  
  626.     else if (abs(iDiff)==abs(jDiff) && bishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  627.  
  628.         return 0;
  629.  
  630.     else
  631.  
  632.         return 1;
  633.  
  634. }
  635.  
  636. int knight(int iCurrent,int iDestination,int jCurrent,int jDestination)
  637. {
  638.     int iDiff,jDiff;
  639.     iDiff=iDestination-iCurrent;
  640.     jDiff=jDestination-jCurrent;
  641.     if ((abs(iDiff)==2) && (abs(jDiff)==1))
  642.  
  643.         return 0;
  644.  
  645.     else if ((abs(jDiff)==2) && (abs(iDiff)==1))
  646.  
  647.         return 0;
  648.  
  649.     else
  650.  
  651.         return 1;
  652.  
  653. }
  654.  
  655.  
  656. int check()
  657. {
  658.     int f=0;
  659.  
  660.     for(int i=0; i<8; i++)
  661.     {
  662.         for(int j=0; j<8; j++)
  663.         {
  664.             if(turn%2==1)
  665.             {
  666.                 findKings();
  667.                 if(validate(i, iBlackKing, j, jBlackKing)==0)
  668.                 {
  669.                     f=1;
  670.                 }
  671.             }
  672.  
  673.             else if(turn%2==0)
  674.             {
  675.                 findKings();
  676.                 if(validate(i, iWhiteKing, j, jWhiteKing)==0)
  677.                 {
  678.                     f=1;
  679.                 }
  680.             }
  681.         }
  682.     }
  683.     return f;
  684. }
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691. void checkmate()
  692. {
  693.     int f=0;
  694.  
  695.     if(check()==1)
  696.     {
  697.         for(int iC=0; iC<8; iC++)
  698.         {
  699.             for(int jC=0; jC<8; jC++)
  700.             {
  701.                 if(turn%2==1)
  702.                 {
  703.                     if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
  704.                     {
  705.                         for(int iD=0; iD<8; iD++)
  706.                         {
  707.                             for(int jD=0; jD<8; jD++)
  708.                             {
  709.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  710.                                 {
  711.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  712.                                     prototypeBoard[iC][jC] = '\0';
  713.                                     if(checkPrototype()==0)
  714.                                     {
  715.                                         f=1;
  716.  
  717.                                     }
  718.                                     correspondPrototypeBoard();
  719.                                 }
  720.                             }
  721.                         }
  722.                     }
  723.                 }
  724.                 else if(turn%2==0)
  725.                 {
  726.                     if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
  727.                     {
  728.                         for(int iD=0; iD<8; iD++)
  729.                         {
  730.                             for(int jD=0; jD<8; jD++)
  731.                             {
  732.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  733.                                 {
  734.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  735.                                     prototypeBoard[iC][jC] = '\0';
  736.                                     if(checkPrototype()==0)
  737.                                     {
  738.                                         f=1;
  739.                                     }
  740.                                     correspondPrototypeBoard();
  741.                                 }
  742.                             }
  743.                         }
  744.                     }
  745.                 }
  746.             }
  747.         }
  748.  
  749.         if(f==0)
  750.             checkmateFlag=1;
  751.  
  752.  
  753.     }
  754. }
  755.  
  756.  
  757.  
  758.  
  759. void stalemate()
  760. {
  761.     int b,f=1,Vx,Mx;
  762.     if (turn%2==1)
  763.     {
  764.         findKings();
  765.         b = ((validateStalemate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing-1))&&(validateStalemate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing))&& (validateStalemate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing)) &&
  766.              (validateStalemate(iBlackKing, iBlackKing, jBlackKing, jBlackKing+1)) &&(validateStalemate(iBlackKing, iBlackKing, jBlackKing, jBlackKing-1)) &&
  767.              (validateStalemate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing+1)) && (validateStalemate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing-1)) &&
  768.              (validateStalemate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing+1)));
  769.         correspondPrototypeBoard();
  770.         for (int i=0; i<8; i++)
  771.         {
  772.             for (int j=0; j<8; j++)
  773.             {
  774.                 if ((prototypeBoard[i][j]>='A')&&(prototypeBoard[i][j]<='Z'))
  775.                 {
  776.                     Vx=i;
  777.                     Mx=j;
  778.                     for (int i=0; i<8; i++)
  779.                     {
  780.                         for (int j=0; j<8; j++)
  781.                         {
  782.                             if ((validateStalemate(Vx,i,Mx,j)==0)&&((i!=Vx)&&(j!=Mx)))
  783.                             {
  784.                                 f=0;
  785.                             }
  786.                             correspondPrototypeBoard();
  787.                         }
  788.  
  789.                     }
  790.                 }
  791.  
  792.             }
  793.         }
  794.  
  795.         correspondPrototypeBoard();
  796.  
  797.         if ((b!=0)&&(f!=0))
  798.         {
  799.             printf("stalemate!\n");
  800.         }
  801.     }
  802.     else if (turn%2==0)
  803.     {
  804.         findKings();
  805.         b = ((validateStalemate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing)) && (validateStalemate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing)) &&
  806.              (validateStalemate(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing+1)) && (validateStalemate(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing-1)) &&
  807.              (validateStalemate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing+1)) && (validateStalemate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing-1)) &&(validateStalemate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing+1)) && (validateStalemate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing-1)));
  808.         correspondPrototypeBoard();
  809.         for (int i=0; i<8; i++)
  810.         {
  811.             for (int j=0; j<8; j++)
  812.             {
  813.                 if (prototypeBoard[i][j]=!'\0')
  814.                 {
  815.                     Vx=i;
  816.                     Mx=j;
  817.                     for (int i=0; i<8; i++)
  818.                     {
  819.                         for (int j=0; j<8; j++)
  820.                         {
  821.                             if (validateStalemate(Vx,i,Mx,j)==0)
  822.                             {
  823.                                 f=0;
  824.                             }
  825.                             correspondPrototypeBoard();
  826.                         }
  827.                     }
  828.                 }
  829.  
  830.             }
  831.         }
  832.  
  833.  
  834.         correspondPrototypeBoard();
  835.  
  836.         if ((b!=0)&&(f!=0))
  837.         {
  838.             printf("stalemate!\n");
  839.         }
  840.  
  841.  
  842.  
  843.     }
  844. }
  845.  
  846. int validateStalemate(int iCurrent, int iDestination, int jCurrent, int jDestination)
  847. {
  848.     char tempPiece;
  849.  
  850.     int f=0, fC=0;
  851.     if(validatePrototype(iCurrent,iDestination,jCurrent,jDestination)!=0)
  852.         f=1;
  853.     else
  854.     {
  855.         if(prototypeBoard[iCurrent][jCurrent]=='k') {
  856.             iWhiteKing = iDestination;
  857.             jWhiteKing = jDestination;
  858.             fC=1;
  859.         }
  860.         else if(prototypeBoard[iCurrent][jCurrent]=='K') {
  861.             iBlackKing = iDestination;
  862.             jBlackKing = jDestination;
  863.             fC=2;
  864.         }
  865.  
  866.         prototypeBoard[iDestination][jDestination] = prototypeBoard[iCurrent][jCurrent];
  867.         prototypeBoard[iCurrent][jCurrent] = '\0';
  868.  
  869.         turn++;
  870.         if(checkPrototypeStalemate()==1)
  871.         {
  872.             f=1;
  873.         }
  874.  
  875.         if(fC==1) {
  876.             iWhiteKing = iCurrent;
  877.             jWhiteKing = jCurrent;
  878.         }
  879.         else if(fC==2) {
  880.             iBlackKing = iCurrent;
  881.             jBlackKing = jCurrent;
  882.         }
  883.         correspondPrototypeBoard();
  884.  
  885.         turn--;
  886.     }
  887.  
  888.  
  889.     return f;
  890. }
  891.  
  892. int checkPrototypeStalemate()
  893. {
  894.  
  895.     int f=0;
  896.     for(int i=0; i<8; i++)
  897.     {
  898.         for(int j=0; j<8; j++)
  899.         {
  900.             if(turn%2==0)
  901.             {
  902.                 if(validatePrototype(i, iBlackKing, j, jBlackKing)==0)
  903.                 {
  904.                     f=1;
  905.                 }
  906.             }
  907.  
  908.             else if(turn%2==1)
  909.             {
  910.                 if(validatePrototype(i, iWhiteKing, j, jWhiteKing)==0)
  911.                 {
  912.                     f=1;
  913.                 }
  914.             }
  915.         }
  916.     }
  917.  
  918.     return f;
  919. }
  920.  
  921.  
  922.  
  923. int printErrors(int iCurrent, int iDestination, int jCurrent, int jDestination, char inp[1000])
  924. {
  925.     int f=0;
  926.     f = validateUndoRedo(inp);
  927.     switch(f)
  928.     {
  929.     case 0:
  930.         break;
  931.     case 1:
  932.         printf("You Can't Undo More!\n");
  933.         return 1;
  934.     case 2:
  935.         printf("You Can't Redo More!\n");
  936.         return 1;
  937.     }
  938.     if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0 || strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
  939.         return;
  940.  
  941.     f = validatePromotion(inp);
  942.     switch(f)
  943.     {
  944.     case 0:
  945.         break;
  946.     case 1:
  947.         printf("Invalid Input!\n");
  948.         return 1;
  949.     }
  950.  
  951.     f = validateTurns(iCurrent, iDestination, jCurrent, jDestination);
  952.     switch(f)
  953.     {
  954.     case 0:
  955.         break;
  956.     case 1:
  957.         printf("White Pieces Turn!\n");
  958.         return 1;
  959.     case 2:
  960.         printf("Black Pieces Turn!\n");
  961.         return 1;
  962.     }
  963.  
  964.     f = validate(iCurrent, iDestination, jCurrent, jDestination);
  965.     switch(f)
  966.     {
  967.     case 0:
  968.         break;
  969.     case 1:
  970.         printf("Invalid Input!\n");
  971.         return 1;
  972.     case 4:
  973.         printf("Empty Position!\n");
  974.         return 1;
  975.     case 5:
  976.         printf("Wrong Move!\n");
  977.         return 1;
  978.     }
  979.  
  980.  
  981.     f = validateCheck(iCurrent,iDestination,jCurrent,jDestination);
  982.     switch(f)
  983.     {
  984.     case 0:
  985.         break;
  986.     case 1:
  987.         if(turn%2==1)
  988.             printf("WHITE KING WILL BE CHECKED!\n");
  989.         else if(turn%2==0)
  990.             printf("BLACK KING WILL BE CHECKED!\n");
  991.         return 1;
  992.  
  993.     }
  994.  
  995.     if(strlen(inp)==4 && (((piecesBoard[iCurrent][jCurrent]=='p' && iDestination==0)) || ((piecesBoard[iCurrent][jCurrent]=='P' && iDestination==7))))
  996.     {
  997.         printf("You Must Enter A Valid Type For The Promotion!\n");
  998.         return 1;
  999.     }
  1000.  
  1001.  
  1002.     if(strlen(inp)==5 && promote(iCurrent,jCurrent, inp[4])==0)
  1003.     {
  1004.         printf("Invalid!\n");
  1005.         return 1;
  1006.     }
  1007.  
  1008.     return f;
  1009.  
  1010. }
  1011.  
  1012.  
  1013.  
  1014.  
  1015. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1016. {
  1017.     char tempPiece;
  1018.  
  1019.     int f=0, fC=0;
  1020.     if(validatePrototype(iCurrent,iDestination,jCurrent,jDestination)!=0)
  1021.         f=1;
  1022.     else
  1023.     {
  1024.         if(prototypeBoard[iCurrent][jCurrent]=='k')
  1025.         {
  1026.             iWhiteKing = iDestination;
  1027.             jWhiteKing = jDestination;
  1028.             fC=1;
  1029.         }
  1030.         else if(prototypeBoard[iCurrent][jCurrent]=='K')
  1031.         {
  1032.             iBlackKing = iDestination;
  1033.             jBlackKing = jDestination;
  1034.             fC=2;
  1035.         }
  1036.  
  1037.         prototypeBoard[iDestination][jDestination] = prototypeBoard[iCurrent][jCurrent];
  1038.         prototypeBoard[iCurrent][jCurrent] = '\0';
  1039.  
  1040.         turn++;
  1041.         if(checkPrototype()==1)
  1042.         {
  1043.             f=1;
  1044.         }
  1045.  
  1046.         if(fC==1)
  1047.         {
  1048.             iWhiteKing = iCurrent;
  1049.             jWhiteKing = jCurrent;
  1050.         }
  1051.         else if(fC==2)
  1052.         {
  1053.             iBlackKing = iCurrent;
  1054.             jBlackKing = jCurrent;
  1055.         }
  1056.         correspondPrototypeBoard();
  1057.  
  1058.         turn--;
  1059.     }
  1060.  
  1061.  
  1062.     return f;
  1063. }
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070. void correspondPrototypeBoard()
  1071. {
  1072.     for(int i=0; i<8; i++)
  1073.     {
  1074.         for(int j=0; j<8; j++)
  1075.         {
  1076.             prototypeBoard[i][j]=piecesBoard[i][j];
  1077.         }
  1078.     }
  1079. }
  1080.  
  1081. int checkPrototype()
  1082. {
  1083.  
  1084.     int f=0;
  1085.     for(int i=0; i<8; i++)
  1086.     {
  1087.         for(int j=0; j<8; j++)
  1088.         {
  1089.             if(turn%2==1)
  1090.             {
  1091.                 if(validatePrototype(i, iBlackKing, j, jBlackKing)==0)
  1092.                 {
  1093.                     f=1;
  1094.                 }
  1095.             }
  1096.  
  1097.             else if(turn%2==0)
  1098.             {
  1099.                 if(validatePrototype(i, iWhiteKing, j, jWhiteKing)==0)
  1100.                 {
  1101.                     f=1;
  1102.                 }
  1103.             }
  1104.         }
  1105.     }
  1106.  
  1107.     return f;
  1108. }
  1109.  
  1110.  
  1111. int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1112. {
  1113.     if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  1114.     {
  1115.         return 1;
  1116.     }
  1117.  
  1118.  
  1119.     else if (((prototypeBoard[iCurrent][jCurrent]>='a') && (prototypeBoard[iCurrent][jCurrent]<='z')) && ((prototypeBoard[iDestination][jDestination]>='a') && (prototypeBoard[iDestination][jDestination]<='z')))
  1120.     {
  1121.  
  1122.         return 5;
  1123.     }
  1124.  
  1125.     else if (((prototypeBoard[iCurrent][jCurrent]>='A') && (prototypeBoard[iCurrent][jCurrent]<='Z')) && ((prototypeBoard[iDestination][jDestination]>='A') && (prototypeBoard[iDestination][jDestination]<='Z')))
  1126.     {
  1127.  
  1128.         return 5;
  1129.     }
  1130.  
  1131.  
  1132.     else if(prototypeBoard[iCurrent][jCurrent]=='\0')
  1133.     {
  1134.  
  1135.         return 4;
  1136.     }
  1137.  
  1138.     else if(((prototypeBoard[iCurrent][jCurrent]=='r')||(prototypeBoard[iCurrent][jCurrent]=='R')) && (prototypeRook(iCurrent, iDestination, jCurrent, jDestination)==1))
  1139.     {
  1140.  
  1141.         return 5;
  1142.     }
  1143.  
  1144.     else if(((prototypeBoard[iCurrent][jCurrent]=='p')||(prototypeBoard[iCurrent][jCurrent]=='P')) && (prototypePawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  1145.     {
  1146.  
  1147.         return 5;
  1148.     }
  1149.  
  1150.     else if(((prototypeBoard[iCurrent][jCurrent]=='b')||(prototypeBoard[iCurrent][jCurrent]=='B')) && (prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  1151.     {
  1152.  
  1153.         return 5;
  1154.     }
  1155.  
  1156.     else if(((prototypeBoard[iCurrent][jCurrent]=='q')||(prototypeBoard[iCurrent][jCurrent]=='Q')) && (prototypeQueen(iCurrent, iDestination, jCurrent, jDestination)==1))
  1157.     {
  1158.  
  1159.         return 5;
  1160.     }
  1161.  
  1162.     else if(((prototypeBoard[iCurrent][jCurrent]=='k')||(prototypeBoard[iCurrent][jCurrent]=='K')) && (prototypeKing(iCurrent, iDestination, jCurrent, jDestination)==1))
  1163.     {
  1164.  
  1165.         return 5;
  1166.     }
  1167.  
  1168.     else if(((prototypeBoard[iCurrent][jCurrent]=='n')||(prototypeBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  1169.     {
  1170.  
  1171.         return 5;
  1172.     }
  1173.  
  1174.  
  1175.  
  1176.     else
  1177.     {
  1178.  
  1179.         return 0;
  1180.     }
  1181.  
  1182.  
  1183. }
  1184.  
  1185.  
  1186.  
  1187. int prototypeRook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1188. {
  1189.     int count,flag=0;
  1190.     if((jCurrent==jDestination) && (iCurrent!= iDestination))
  1191.     {
  1192.         if (iDestination>iCurrent)
  1193.         {
  1194.             for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  1195.             {
  1196.                 if (prototypeBoard[iCurrent+count][jCurrent]=='\0')
  1197.                 {
  1198.                     flag=0 ;
  1199.                 }
  1200.                 else
  1201.                 {
  1202.                     flag=1;
  1203.                 }
  1204.             }
  1205.         }
  1206.         else
  1207.         {
  1208.             for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  1209.             {
  1210.                 if (prototypeBoard[iCurrent-count][jCurrent]=='\0')
  1211.                 {
  1212.                     flag=0;
  1213.                 }
  1214.                 else
  1215.                 {
  1216.                     flag=1;
  1217.                 }
  1218.             }
  1219.         }
  1220.         if (flag==0)
  1221.         {
  1222.             return 0;
  1223.         }
  1224.         else
  1225.         {
  1226.             return 1;
  1227.         }
  1228.     }
  1229.     else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  1230.     {
  1231.         if (jDestination>jCurrent)
  1232.         {
  1233.             for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  1234.             {
  1235.                 if (prototypeBoard[iCurrent][jCurrent+count]=='\0')
  1236.                 {
  1237.                     flag=0;
  1238.                 }
  1239.                 else
  1240.                 {
  1241.                     flag=1;
  1242.                 }
  1243.             }
  1244.         }
  1245.         else
  1246.         {
  1247.             for (count=1; (jCurrent-count)>jDestination; count++)
  1248.             {
  1249.                 if (prototypeBoard[iCurrent][jCurrent-count]=='\0')
  1250.                 {
  1251.                     flag=0;
  1252.                 }
  1253.                 else
  1254.                 {
  1255.                     flag=1;
  1256.                 }
  1257.             }
  1258.         }
  1259.         if (flag==0)
  1260.         {
  1261.             return 0;
  1262.         }
  1263.         else
  1264.         {
  1265.             return 1;
  1266.         }
  1267.     }
  1268.     else
  1269.     {
  1270.         return 1;
  1271.     }
  1272. }
  1273.  
  1274.  
  1275. int prototypeKing(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1276. {
  1277.     int iDiff,jDiff;
  1278.     iDiff=iCurrent-iDestination;
  1279.     jDiff=jCurrent-jDestination;
  1280.     if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  1281.     {
  1282.  
  1283.         return 0;
  1284.     }
  1285.     else
  1286.     {
  1287.         return 1;
  1288.     }
  1289. }
  1290.  
  1291. int prototypeBishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1292. {
  1293.     int iDiff,jDiff;
  1294.     int count=1,flag=0;
  1295.     iDiff=iDestination-iCurrent;
  1296.     jDiff=jDestination-jCurrent;
  1297.     int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  1298.  
  1299.     if (abs(iDiff)==abs(jDiff))
  1300.     {
  1301.         if (iDestination>iCurrent)
  1302.         {
  1303.             count=1;
  1304.             do
  1305.             {
  1306.                 DecjCurrent=jCurrent-count;
  1307.                 IncjCurrent=jCurrent+count;
  1308.                 InciCurrent=iCurrent+count;
  1309.                 if (InciCurrent<iDestination)
  1310.                 {
  1311.                     if (jDestination<jCurrent)
  1312.                     {
  1313.                         if (prototypeBoard[InciCurrent][DecjCurrent]=='\0')
  1314.                         {
  1315.                             flag=0;
  1316.                         }
  1317.                         else
  1318.                         {
  1319.                             flag=1;
  1320.                         }
  1321.  
  1322.                     }
  1323.                     else if (jDestination>jCurrent)
  1324.                     {
  1325.                         if (prototypeBoard[InciCurrent][IncjCurrent]=='\0')
  1326.                         {
  1327.                             flag=0;
  1328.                         }
  1329.                         else
  1330.                         {
  1331.                             flag=1;
  1332.                         }
  1333.                     }
  1334.                     count++;
  1335.                 }
  1336.             }
  1337.             while ((InciCurrent<iDestination) && (flag==0));
  1338.             if (flag==0)
  1339.             {
  1340.                 return 0;
  1341.             }
  1342.             else
  1343.             {
  1344.                 return 1;
  1345.             }
  1346.         }
  1347.  
  1348.         else
  1349.         {
  1350.             count=1;
  1351.             do
  1352.             {
  1353.                 DeciCurrent=iCurrent-count;
  1354.                 DecjCurrent=jCurrent-count;
  1355.                 IncjCurrent=jCurrent+count;
  1356.                 if (DeciCurrent>iDestination)
  1357.                 {
  1358.                     if (jDestination<jCurrent)
  1359.                     {
  1360.  
  1361.                         if (prototypeBoard[DeciCurrent][DecjCurrent]=='\0')
  1362.                         {
  1363.                             flag=0;
  1364.                         }
  1365.                         else
  1366.                         {
  1367.                             flag=1;
  1368.                         }
  1369.                     }
  1370.                     else if (jDestination>jCurrent)
  1371.                     {
  1372.                         if (prototypeBoard[DeciCurrent][IncjCurrent]=='\0')
  1373.                         {
  1374.                             flag=0;
  1375.                         }
  1376.                         else
  1377.                         {
  1378.                             flag=1;
  1379.                         }
  1380.                     }
  1381.                     count++;
  1382.  
  1383.                 }
  1384.             }
  1385.             while ((DeciCurrent>iDestination) && (flag==0));
  1386.  
  1387.             if (flag==0)
  1388.             {
  1389.                 return 0;
  1390.             }
  1391.             else
  1392.             {
  1393.                 return 1;
  1394.             }
  1395.         }
  1396.  
  1397.  
  1398.     }
  1399.     else
  1400.     {
  1401.         return 1;
  1402.     }
  1403.  
  1404. }
  1405.  
  1406. int prototypePawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1407. {
  1408.  
  1409.     if(prototypeBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  1410.     {
  1411.         if (prototypeBoard[iDestination][jDestination]!='\0')
  1412.         {
  1413.             return 1;
  1414.         }
  1415.         else
  1416.         {
  1417.             return 0;
  1418.         }
  1419.     }
  1420.  
  1421.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  1422.     {
  1423.         if (prototypeBoard[iDestination][jDestination]!='\0')
  1424.         {
  1425.             return 1;
  1426.         }
  1427.         else
  1428.         {
  1429.             return 0;
  1430.         }
  1431.     }
  1432.  
  1433.     else if(prototypeBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='A' && prototypeBoard[iDestination][jDestination]<='Z')
  1434.         return 0;
  1435.  
  1436.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='a' && prototypeBoard[iDestination][jDestination]<='z')
  1437.         return 0;
  1438.  
  1439.     else if(prototypeBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2) && prototypeBoard[iDestination][jDestination]=='\0')
  1440.         return 0;
  1441.  
  1442.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2 && prototypeBoard[iDestination][jDestination]=='\0'))
  1443.         return 0;
  1444.  
  1445.     else
  1446.         return 1;
  1447.  
  1448. }
  1449.  
  1450.  
  1451.  
  1452. int prototypeQueen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1453. {
  1454.     int iDiff,jDiff;
  1455.     iDiff=iDestination-iCurrent;
  1456.     jDiff=jDestination-jCurrent;
  1457.  
  1458.     if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& prototypeRook(iCurrent,iDestination,jCurrent,jDestination)==0)
  1459.  
  1460.         return 0;
  1461.  
  1462.  
  1463.     else if (abs(iDiff)==abs(jDiff) && prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  1464.  
  1465.         return 0;
  1466.  
  1467.     else
  1468.  
  1469.         return 1;
  1470.  
  1471. }
  1472.  
  1473.  
  1474. void findKings()
  1475. {
  1476.     for(int i=0; i<8; i++)
  1477.     {
  1478.         for(int j=0; j<8; j++)
  1479.         {
  1480.             if(piecesBoard[i][j]=='k')
  1481.             {
  1482.                 iWhiteKing = i;
  1483.                 jWhiteKing = j;
  1484.             }
  1485.             else if(piecesBoard[i][j]=='K')
  1486.             {
  1487.                 iBlackKing = i;
  1488.                 jBlackKing = j;
  1489.             }
  1490.         }
  1491.     }
  1492. }
  1493.  
  1494.  
  1495. void trackUndo(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1496. {
  1497.     undo[movesCount] = iCurrent*1000 + iDestination*100 + jCurrent*10 + jDestination;
  1498.     movesCount++;
  1499.     if(piecesBoard[iDestination][jDestination] != '\0')
  1500.     {
  1501.         capturingTrack[captureTurn] = turn;
  1502.         captureTurn++;
  1503.     }
  1504. }
  1505.  
  1506.  
  1507.  
  1508. void processUndo()
  1509. {
  1510.     int iC,iD,jC,jD;
  1511.  
  1512.     movesCount--;
  1513.     iC = undo[movesCount]/1000;
  1514.     iD = undo[movesCount]/100%10;
  1515.     jC = undo[movesCount]/10%10;
  1516.     jD = undo[movesCount]%10;
  1517.     int f=0;
  1518.     if((turn-1)==capturingTrack[captureTurn-1])
  1519.     {
  1520.         move(iD,iC,jD,jC);
  1521.         if(turn%2==1)
  1522.         {
  1523.             piecesBoard[iD][jD] = capturedWhite[countWhite-1];
  1524.             countWhite--;
  1525.         }
  1526.         else if(turn%2==0)
  1527.         {
  1528.             piecesBoard[iD][jD] = capturedBlack[countBlack-1];
  1529.             countBlack--;
  1530.         }
  1531.         captureTurn--;
  1532.         f=1;
  1533.  
  1534.  
  1535.         if((turn-1)==promotionTrack[promoteTurn-1] && promoteTurn!=0)
  1536.         {
  1537.             if(f==0)
  1538.                 move(iD,iC,jD,jC);
  1539.             promoteTurn--;
  1540.             if(turn%2==1)
  1541.                 piecesBoard[iC][jC] = 'P';
  1542.             else if(turn%2==0)
  1543.                 piecesBoard[iC][jC] = 'p';
  1544.  
  1545.         }
  1546.     }
  1547.     else
  1548.     {
  1549.         move(iD, iC, jD, jC);
  1550.     }
  1551.     findKings();
  1552.     correspondPrototypeBoard();
  1553.     turn-=2;
  1554. }
  1555.  
  1556. void processRedo()
  1557. {
  1558.     int iC, iD, jC, jD;
  1559.     iC = undo[movesCount]/1000;
  1560.     iD = undo[movesCount]/100%10;
  1561.     jC = undo[movesCount]/10%10;
  1562.     jD = undo[movesCount]%10;
  1563.     trackUndo(iC,iD,jC,jD);
  1564.     move(iC,iD,jC,jD);
  1565.     findKings();
  1566.     correspondPrototypeBoard();
  1567.  
  1568.     if((turn)==capturingTrack[captureTurn-1])
  1569.     {
  1570.         if(turn%2==0)
  1571.         {
  1572.             countWhite++;
  1573.         }
  1574.         else if(turn%2==1)
  1575.         {
  1576.             countBlack++;
  1577.         }
  1578.         if((turn)== promotionTrack[promoteTurn])
  1579.         {
  1580.             piecesBoard[iD][jD] = promotionPieces[promoteTurn];
  1581.             promoteTurn++;
  1582.         }
  1583.     }
  1584. }
  1585.  
  1586. int predictPromotion()
  1587. {
  1588.     int f=0;
  1589.     for(int j=0; j<8; j++)
  1590.     {
  1591.         if(turn%2==1)
  1592.         {
  1593.             if(piecesBoard[1][j]=='p')
  1594.             {
  1595.                 f=1;
  1596.                 break;
  1597.             }
  1598.         }
  1599.         else if(turn%2==0)
  1600.         {
  1601.             if(piecesBoard[1][j]=='P')
  1602.             {
  1603.                 f=1;
  1604.                 break;
  1605.             }
  1606.         }
  1607.     }
  1608.     return f;
  1609. }
  1610.  
  1611. int promote(int i, int j, char type)
  1612. {
  1613.     if(predictPromotion()==1)
  1614.     {
  1615.         if(piecesBoard[i][j]=='p' && (type=='q' || type=='b' || type=='n') && turn%2==1)
  1616.             return 1;
  1617.         else if(piecesBoard[i][j]=='P' && (type=='Q' || type=='B' || type=='N') && turn%2==0)
  1618.             return 1;
  1619.     }
  1620.     return 0;
  1621. }
  1622.  
  1623.  
  1624. void save()
  1625. {
  1626.     char z,v,x[1],n[2];
  1627.     log1 = fopen("PiecesBoard.txt", "w");
  1628.  
  1629.     for (int i=0; i<8; i++)
  1630.     {
  1631.         for (int j=0; j<8; j++)
  1632.         {
  1633.             z=piecesBoard[i][j];
  1634.             fputc(z,log1);
  1635.         }
  1636.     }
  1637.     log2 = fopen("CapturedBlack.txt","w");
  1638.     for(int i=0; i<countBlack; i++)
  1639.     {
  1640.         z=capturedBlack[i];
  1641.         fputc(z,log2);
  1642.     }
  1643.     log3 = fopen("CapturedWhite.txt","w");
  1644.     for (int i=0; i<countWhite; i++)
  1645.     {
  1646.         z=capturedWhite[i];
  1647.         fputc(z,log3);
  1648.     }
  1649.  
  1650.     fclose(log1);
  1651.     fclose(log2);
  1652.     fclose(log3);
  1653.     log4 = fopen ("turn.txt","w");
  1654.     v=(char)turn%2+'0';
  1655.     fputc(v,log4);
  1656.     log5 = fopen("BlackCounter.txt","w");
  1657.     if (countBlack>=10)
  1658.     {
  1659.         n[0]=countBlack/10;
  1660.         n[1]=countBlack%10;
  1661.         fputs(n,log5);
  1662.         fclose(log5);
  1663.     }
  1664.     else if (countBlack<10)
  1665.     {
  1666.         x[0]=countBlack;
  1667.         fputs(x,log5);
  1668.         fclose(log5);
  1669.     }
  1670.     if (countBlack>=10)
  1671.     {
  1672.         n[0]=countBlack/10;
  1673.         n[1]=countBlack%10;
  1674.         fputs(n,log5);
  1675.         fclose(log5);
  1676.     }
  1677.     else if (countBlack<10)
  1678.     {
  1679.         x[0]=countBlack;
  1680.         fputs(x,log5);
  1681.         fclose(log5);
  1682.     }
  1683.     log6 = fopen ("WhiteCounter.txt","w");
  1684.     if (countWhite>=10)
  1685.     {
  1686.         n[0]=countWhite/10;
  1687.         n[1]=countWhite%10;
  1688.         fputs(n,log6);
  1689.         fclose(log6);
  1690.     }
  1691.     else if (countWhite<10)
  1692.     {
  1693.         x[0]=countWhite;
  1694.         fputs(x,log6);
  1695.         fclose(log6);
  1696.     }
  1697.  
  1698.  
  1699.  
  1700.     fclose(log4);
  1701.     system("cls");
  1702.     flag=2;
  1703. }
  1704.  
  1705. void load()
  1706. {
  1707.     char z,v;
  1708.     log1 = fopen("PiecesBoard.txt", "r");
  1709.     if (log1==NULL)
  1710.     {
  1711.         printf("ERROR: Could Not Find The File!\n");
  1712.     }
  1713.     else
  1714.     {
  1715.  
  1716.         for(int i=0; i<8; i++)
  1717.         {
  1718.             for(int j=0; j<8; j++)
  1719.             {
  1720.                 z = fgetc(log1);
  1721.                 piecesBoard[i][j] = z;
  1722.  
  1723.             }
  1724.         }
  1725.  
  1726.     }
  1727.     log2=fopen("CapturedBlack.txt","r");
  1728.     for(int i=0; i<16; i++)
  1729.     {
  1730.         z=fgetc(log2);
  1731.         capturedBlack[i]=z;
  1732.  
  1733.     }
  1734.     log3=fopen("CapturedWhite.txt","r");
  1735.     for (int i=0; i<16; i++)
  1736.     {
  1737.         z= fgetc(log3);
  1738.         capturedWhite[i]=z;
  1739.     }
  1740.     log4=fopen("turn.txt","r");
  1741.  
  1742.     turn=(int)fgetc(log4)- (int)'0';
  1743.     if(turn==0)
  1744.         turn=2;
  1745.  
  1746.  
  1747.     fclose(log1);
  1748.     fclose(log2);
  1749.     fclose(log3);
  1750.     fclose(log4);
  1751.  
  1752.     movesCount=0;
  1753.     for(int i=0; undo[i]!='\0'; i++)
  1754.     {
  1755.         undo[i]=0;
  1756.     }
  1757.  
  1758.     system("cls");
  1759.     printf("\nCaptured White Pieces: ");
  1760.     for(int i=0; i<countWhite; i++)
  1761.         printf("%c ", capturedWhite[i]);
  1762.     printf("\nCaptured Black Pieces: ");
  1763.     for(int i=0; i<countBlack; i++)
  1764.         printf("%c ", capturedBlack[i]);
  1765.     printf("\n\n");
  1766. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement