Advertisement
anas_harby

Untitled

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