Advertisement
anas_harby

Untitled

Dec 23rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 41.51 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.  
  47. FILE*log1;
  48. FILE*log2;
  49. FILE*log3;
  50. FILE*log4;
  51. FILE*log5;
  52. FILE*log6;
  53. void save();
  54. void load();
  55.  
  56.  
  57. int main()
  58. {
  59.     initializeBW();
  60.  
  61.     while(!checkmateFlag)
  62.     {
  63.         turn++;
  64.         printf("\nCaptured White Pieces: ");
  65.         for(int i=0; i<countWhite; i++)
  66.             printf("%c ", capturedWhite[i]);
  67.         printf("\nCaptured Black Pieces: ");
  68.         for(int i=0; i<countBlack; i++)
  69.             printf("%c ", capturedBlack[i]);
  70.         printf("\n\n");
  71.         printBoard();
  72.         turn++;
  73.         if(check()==1)
  74.             printf("CHECK!\n");
  75.         turn--;
  76.         userInput();
  77.         system("cls");
  78.     }
  79.  
  80.     printBoard();
  81.     if(checkmateFlag==1)
  82.         printf("CHECKMATE!\n");
  83.     else if(checkmateFlag==2)
  84.         printf("STALEMATE!\n");
  85.  
  86. }
  87.  
  88.  
  89. void initializeBW ()
  90. {
  91.     char c1 = '_', c2= '.';
  92.     for(int i=0; i<8; i++)
  93.     {
  94.         for(int j=0; j<8; j+=2)
  95.         {
  96.             BWBoard[i][j] = c1;
  97.             BWBoard[i][j+1] = c2;
  98.         }
  99.         char temp = c1;
  100.         c1 = c2;
  101.         c2 = temp;
  102.     }
  103. }
  104.  
  105. void getInput()
  106. {
  107.     char inp[1000];
  108.     gets(inp);
  109.     userInput(inp);
  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.         stalemate();
  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)
  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))
  603.         return 0;
  604.  
  605.     else if(piecesBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
  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.  
  698.  
  699.  
  700.         for(int iC=0; iC<8; iC++)
  701.         {
  702.             for(int jC=0; jC<8; jC++)
  703.             {
  704.                 if(turn%2==1)
  705.                 {
  706.                     if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
  707.                     {
  708.                         for(int iD=0; iD<8; iD++)
  709.                         {
  710.                             for(int jD=0; jD<8; jD++)
  711.                             {
  712.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  713.                                 {
  714.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  715.                                     prototypeBoard[iC][jC] = '\0';
  716.                                     if(checkPrototype()==0)
  717.                                     {
  718.                                         f=1;
  719.  
  720.                                     }
  721.                                     correspondPrototypeBoard();
  722.                                 }
  723.                             }
  724.                         }
  725.                     }
  726.                 }
  727.                 else if(turn%2==0)
  728.                 {
  729.                     if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
  730.                     {
  731.                         for(int iD=0; iD<8; iD++)
  732.                         {
  733.                             for(int jD=0; jD<8; jD++)
  734.                             {
  735.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  736.                                 {
  737.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  738.                                     prototypeBoard[iC][jC] = '\0';
  739.                                     if(checkPrototype()==0)
  740.                                     {
  741.                                         f=1;
  742.                                     }
  743.                                     correspondPrototypeBoard();
  744.                                 }
  745.                             }
  746.                         }
  747.                     }
  748.                 }
  749.             }
  750.         }
  751.  
  752.         if(f==0)
  753.             checkmateFlag=1;
  754.  
  755.  
  756.     }
  757. }
  758.  
  759. void stalemate()
  760. {
  761.     int f=0;
  762.  
  763.     if(check()==0)
  764.     {
  765.         for(int iC=0; iC<8; iC++)
  766.         {
  767.             for(int jC=0; jC<8; jC++)
  768.             {
  769.                 if(turn%2==1)
  770.                 {
  771.                     if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
  772.                     {
  773.                         for(int iD=0; iD<8; iD++)
  774.                         {
  775.                             for(int jD=0; jD<8; jD++)
  776.                             {
  777.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  778.                                 {
  779.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  780.                                     prototypeBoard[iC][jC] = '\0';
  781.                                     if(checkPrototype()==0)
  782.                                     {
  783.                                         f=1;
  784.  
  785.                                     }
  786.                                     correspondPrototypeBoard();
  787.                                 }
  788.                             }
  789.                         }
  790.                     }
  791.                 }
  792.                 else if(turn%2==0)
  793.                 {
  794.                     if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
  795.                     {
  796.                         for(int iD=0; iD<8; iD++)
  797.                         {
  798.                             for(int jD=0; jD<8; jD++)
  799.                             {
  800.                                 if(validatePrototype(iC,iD,jC,jD)==0)
  801.                                 {
  802.                                     prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
  803.                                     prototypeBoard[iC][jC] = '\0';
  804.                                     if(checkPrototype()==0)
  805.                                     {
  806.                                         f=1;
  807.                                     }
  808.                                     correspondPrototypeBoard();
  809.                                 }
  810.                             }
  811.                         }
  812.                     }
  813.                 }
  814.             }
  815.         }
  816.         if(f==0)
  817.             checkmateFlag=2;
  818.  
  819.  
  820.     }
  821.  
  822. }
  823.  
  824. int printErrors(int iCurrent, int iDestination, int jCurrent, int jDestination, char inp[1000])
  825. {
  826.     int f=0;
  827.     f = validateUndoRedo(inp);
  828.     switch(f)
  829.     {
  830.     case 0:
  831.         break;
  832.     case 1:
  833.         printf("You Can't Undo More!\n");
  834.         return 1;
  835.     case 2:
  836.         printf("You Can't Redo More!\n");
  837.         return 1;
  838.     }
  839.     if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0 || strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
  840.         return;
  841.  
  842.     f = validatePromotion(inp);
  843.     switch(f)
  844.     {
  845.     case 0:
  846.         break;
  847.     case 1:
  848.         printf("Invalid Input!\n");
  849.         return 1;
  850.     }
  851.  
  852.     f = validateTurns(iCurrent, iDestination, jCurrent, jDestination);
  853.     switch(f)
  854.     {
  855.     case 0:
  856.         break;
  857.     case 1:
  858.         printf("White Pieces Turn!\n");
  859.         return 1;
  860.     case 2:
  861.         printf("Black Pieces Turn!\n");
  862.         return 1;
  863.     }
  864.  
  865.     f = validate(iCurrent, iDestination, jCurrent, jDestination);
  866.     switch(f)
  867.     {
  868.     case 0:
  869.         break;
  870.     case 1:
  871.         printf("Invalid Input!\n");
  872.         return 1;
  873.     case 4:
  874.         printf("Empty Position!\n");
  875.         return 1;
  876.     case 5:
  877.         printf("Wrong Move!\n");
  878.         return 1;
  879.     }
  880.  
  881.  
  882.     f = validateCheck(iCurrent,iDestination,jCurrent,jDestination);
  883.     switch(f)
  884.     {
  885.     case 0:
  886.         break;
  887.     case 1:
  888.         if(turn%2==1)
  889.             printf("WHITE KING WILL BE CHECKED!\n");
  890.         else if(turn%2==0)
  891.             printf("BLACK KING WILL BE CHECKED!\n");
  892.         return 1;
  893.  
  894.     }
  895.  
  896.     if(strlen(inp)==4 && (((piecesBoard[iCurrent][jCurrent]=='p' && iDestination==0)) || ((piecesBoard[iCurrent][jCurrent]=='P' && iDestination==7))))
  897.     {
  898.         printf("You Must Enter A Valid Type For The Promotion!\n");
  899.         return 1;
  900.     }
  901.  
  902.  
  903.     if(strlen(inp)==5 && promote(iCurrent,jCurrent, inp[4])==0)
  904.     {
  905.         printf("Invalid!\n");
  906.         return 1;
  907.     }
  908.  
  909.     return f;
  910.  
  911. }
  912.  
  913.  
  914.  
  915.  
  916. int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
  917. {
  918.     char tempPiece;
  919.  
  920.     int f=0, fC=0;
  921.     if(validatePrototype(iCurrent,iDestination,jCurrent,jDestination)!=0)
  922.         f=1;
  923.     else
  924.     {
  925.         if(prototypeBoard[iCurrent][jCurrent]=='k')
  926.         {
  927.             iWhiteKing = iDestination;
  928.             jWhiteKing = jDestination;
  929.             fC=1;
  930.         }
  931.         else if(prototypeBoard[iCurrent][jCurrent]=='K')
  932.         {
  933.             iBlackKing = iDestination;
  934.             jBlackKing = jDestination;
  935.             fC=2;
  936.         }
  937.  
  938.         prototypeBoard[iDestination][jDestination] = prototypeBoard[iCurrent][jCurrent];
  939.         prototypeBoard[iCurrent][jCurrent] = '\0';
  940.  
  941.         turn++;
  942.         if(checkPrototype()==1)
  943.         {
  944.             f=1;
  945.         }
  946.  
  947.         if(fC==1)
  948.         {
  949.             iWhiteKing = iCurrent;
  950.             jWhiteKing = jCurrent;
  951.         }
  952.         else if(fC==2)
  953.         {
  954.             iBlackKing = iCurrent;
  955.             jBlackKing = jCurrent;
  956.         }
  957.         correspondPrototypeBoard();
  958.  
  959.         turn--;
  960.     }
  961.  
  962.  
  963.     return f;
  964. }
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971. void correspondPrototypeBoard()
  972. {
  973.     for(int i=0; i<8; i++)
  974.     {
  975.         for(int j=0; j<8; j++)
  976.         {
  977.             prototypeBoard[i][j]=piecesBoard[i][j];
  978.         }
  979.     }
  980. }
  981.  
  982. int checkPrototype()
  983. {
  984.  
  985.     int f=0;
  986.     for(int i=0; i<8; i++)
  987.     {
  988.         for(int j=0; j<8; j++)
  989.         {
  990.             if(turn%2==1)
  991.             {
  992.                 if(validatePrototype(i, iBlackKing, j, jBlackKing)==0)
  993.                 {
  994.                     f=1;
  995.                 }
  996.             }
  997.  
  998.             else if(turn%2==0)
  999.             {
  1000.                 if(validatePrototype(i, iWhiteKing, j, jWhiteKing)==0)
  1001.                 {
  1002.                     f=1;
  1003.                 }
  1004.             }
  1005.         }
  1006.     }
  1007.  
  1008.     return f;
  1009. }
  1010.  
  1011.  
  1012. int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1013. {
  1014.     if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
  1015.     {
  1016.         return 1;
  1017.     }
  1018.  
  1019.  
  1020.     else if (((prototypeBoard[iCurrent][jCurrent]>='a') && (prototypeBoard[iCurrent][jCurrent]<='z')) && ((prototypeBoard[iDestination][jDestination]>='a') && (prototypeBoard[iDestination][jDestination]<='z')))
  1021.     {
  1022.  
  1023.         return 5;
  1024.     }
  1025.  
  1026.     else if (((prototypeBoard[iCurrent][jCurrent]>='A') && (prototypeBoard[iCurrent][jCurrent]<='Z')) && ((prototypeBoard[iDestination][jDestination]>='A') && (prototypeBoard[iDestination][jDestination]<='Z')))
  1027.     {
  1028.  
  1029.         return 5;
  1030.     }
  1031.  
  1032.  
  1033.     else if(prototypeBoard[iCurrent][jCurrent]=='\0')
  1034.     {
  1035.  
  1036.         return 4;
  1037.     }
  1038.  
  1039.     else if(((prototypeBoard[iCurrent][jCurrent]=='r')||(prototypeBoard[iCurrent][jCurrent]=='R')) && (prototypeRook(iCurrent, iDestination, jCurrent, jDestination)==1))
  1040.     {
  1041.  
  1042.         return 5;
  1043.     }
  1044.  
  1045.     else if(((prototypeBoard[iCurrent][jCurrent]=='p')||(prototypeBoard[iCurrent][jCurrent]=='P')) && (prototypePawn(iCurrent, iDestination, jCurrent, jDestination)==1))
  1046.     {
  1047.  
  1048.         return 5;
  1049.     }
  1050.  
  1051.     else if(((prototypeBoard[iCurrent][jCurrent]=='b')||(prototypeBoard[iCurrent][jCurrent]=='B')) && (prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==1))
  1052.     {
  1053.  
  1054.         return 5;
  1055.     }
  1056.  
  1057.     else if(((prototypeBoard[iCurrent][jCurrent]=='q')||(prototypeBoard[iCurrent][jCurrent]=='Q')) && (prototypeQueen(iCurrent, iDestination, jCurrent, jDestination)==1))
  1058.     {
  1059.  
  1060.         return 5;
  1061.     }
  1062.  
  1063.     else if(((prototypeBoard[iCurrent][jCurrent]=='k')||(prototypeBoard[iCurrent][jCurrent]=='K')) && (prototypeKing(iCurrent, iDestination, jCurrent, jDestination)==1))
  1064.     {
  1065.  
  1066.         return 5;
  1067.     }
  1068.  
  1069.     else if(((prototypeBoard[iCurrent][jCurrent]=='n')||(prototypeBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
  1070.     {
  1071.  
  1072.         return 5;
  1073.     }
  1074.  
  1075.  
  1076.  
  1077.     else
  1078.     {
  1079.  
  1080.         return 0;
  1081.     }
  1082.  
  1083.  
  1084. }
  1085.  
  1086.  
  1087.  
  1088. int prototypeRook(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1089. {
  1090.     int count,flag=0;
  1091.     if((jCurrent==jDestination) && (iCurrent!= iDestination))
  1092.     {
  1093.         if (iDestination>iCurrent)
  1094.         {
  1095.             for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
  1096.             {
  1097.                 if (prototypeBoard[iCurrent+count][jCurrent]=='\0')
  1098.                 {
  1099.                     flag=0 ;
  1100.                 }
  1101.                 else
  1102.                 {
  1103.                     flag=1;
  1104.                 }
  1105.             }
  1106.         }
  1107.         else
  1108.         {
  1109.             for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
  1110.             {
  1111.                 if (prototypeBoard[iCurrent-count][jCurrent]=='\0')
  1112.                 {
  1113.                     flag=0;
  1114.                 }
  1115.                 else
  1116.                 {
  1117.                     flag=1;
  1118.                 }
  1119.             }
  1120.         }
  1121.         if (flag==0)
  1122.         {
  1123.             return 0;
  1124.         }
  1125.         else
  1126.         {
  1127.             return 1;
  1128.         }
  1129.     }
  1130.     else if((jCurrent!=jDestination) && (iCurrent==iDestination))
  1131.     {
  1132.         if (jDestination>jCurrent)
  1133.         {
  1134.             for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
  1135.             {
  1136.                 if (prototypeBoard[iCurrent][jCurrent+count]=='\0')
  1137.                 {
  1138.                     flag=0;
  1139.                 }
  1140.                 else
  1141.                 {
  1142.                     flag=1;
  1143.                 }
  1144.             }
  1145.         }
  1146.         else
  1147.         {
  1148.             for (count=1; (jCurrent-count)>jDestination; count++)
  1149.             {
  1150.                 if (prototypeBoard[iCurrent][jCurrent-count]=='\0')
  1151.                 {
  1152.                     flag=0;
  1153.                 }
  1154.                 else
  1155.                 {
  1156.                     flag=1;
  1157.                 }
  1158.             }
  1159.         }
  1160.         if (flag==0)
  1161.         {
  1162.             return 0;
  1163.         }
  1164.         else
  1165.         {
  1166.             return 1;
  1167.         }
  1168.     }
  1169.     else
  1170.     {
  1171.         return 1;
  1172.     }
  1173. }
  1174.  
  1175.  
  1176. int prototypeKing(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1177. {
  1178.     int iDiff,jDiff;
  1179.     iDiff=iCurrent-iDestination;
  1180.     jDiff=jCurrent-jDestination;
  1181.     if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
  1182.     {
  1183.  
  1184.         return 0;
  1185.     }
  1186.     else
  1187.     {
  1188.         return 1;
  1189.     }
  1190. }
  1191.  
  1192. int prototypeBishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1193. {
  1194.     int iDiff,jDiff;
  1195.     int count=1,flag=0;
  1196.     iDiff=iDestination-iCurrent;
  1197.     jDiff=jDestination-jCurrent;
  1198.     int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
  1199.  
  1200.     if (abs(iDiff)==abs(jDiff))
  1201.     {
  1202.         if (iDestination>iCurrent)
  1203.         {
  1204.             count=1;
  1205.             do
  1206.             {
  1207.                 DecjCurrent=jCurrent-count;
  1208.                 IncjCurrent=jCurrent+count;
  1209.                 InciCurrent=iCurrent+count;
  1210.                 if (InciCurrent<iDestination)
  1211.                 {
  1212.                     if (jDestination<jCurrent)
  1213.                     {
  1214.                         if (prototypeBoard[InciCurrent][DecjCurrent]=='\0')
  1215.                         {
  1216.                             flag=0;
  1217.                         }
  1218.                         else
  1219.                         {
  1220.                             flag=1;
  1221.                         }
  1222.  
  1223.                     }
  1224.                     else if (jDestination>jCurrent)
  1225.                     {
  1226.                         if (prototypeBoard[InciCurrent][IncjCurrent]=='\0')
  1227.                         {
  1228.                             flag=0;
  1229.                         }
  1230.                         else
  1231.                         {
  1232.                             flag=1;
  1233.                         }
  1234.                     }
  1235.                     count++;
  1236.                 }
  1237.             }
  1238.             while ((InciCurrent<iDestination) && (flag==0));
  1239.             if (flag==0)
  1240.             {
  1241.                 return 0;
  1242.             }
  1243.             else
  1244.             {
  1245.                 return 1;
  1246.             }
  1247.         }
  1248.  
  1249.         else
  1250.         {
  1251.             count=1;
  1252.             do
  1253.             {
  1254.                 DeciCurrent=iCurrent-count;
  1255.                 DecjCurrent=jCurrent-count;
  1256.                 IncjCurrent=jCurrent+count;
  1257.                 if (DeciCurrent>iDestination)
  1258.                 {
  1259.                     if (jDestination<jCurrent)
  1260.                     {
  1261.  
  1262.                         if (prototypeBoard[DeciCurrent][DecjCurrent]=='\0')
  1263.                         {
  1264.                             flag=0;
  1265.                         }
  1266.                         else
  1267.                         {
  1268.                             flag=1;
  1269.                         }
  1270.                     }
  1271.                     else if (jDestination>jCurrent)
  1272.                     {
  1273.                         if (prototypeBoard[DeciCurrent][IncjCurrent]=='\0')
  1274.                         {
  1275.                             flag=0;
  1276.                         }
  1277.                         else
  1278.                         {
  1279.                             flag=1;
  1280.                         }
  1281.                     }
  1282.                     count++;
  1283.  
  1284.                 }
  1285.             }
  1286.             while ((DeciCurrent>iDestination) && (flag==0));
  1287.  
  1288.             if (flag==0)
  1289.             {
  1290.                 return 0;
  1291.             }
  1292.             else
  1293.             {
  1294.                 return 1;
  1295.             }
  1296.         }
  1297.  
  1298.  
  1299.     }
  1300.     else
  1301.     {
  1302.         return 1;
  1303.     }
  1304.  
  1305. }
  1306.  
  1307. int prototypePawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1308. {
  1309.  
  1310.     if(prototypeBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
  1311.     {
  1312.         if (prototypeBoard[iDestination][jDestination]!='\0')
  1313.         {
  1314.             return 1;
  1315.         }
  1316.         else
  1317.         {
  1318.             return 0;
  1319.         }
  1320.     }
  1321.  
  1322.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
  1323.     {
  1324.         if (prototypeBoard[iDestination][jDestination]!='\0')
  1325.         {
  1326.             return 1;
  1327.         }
  1328.         else
  1329.         {
  1330.             return 0;
  1331.         }
  1332.     }
  1333.  
  1334.     else if(prototypeBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='A' && prototypeBoard[iDestination][jDestination]<='Z')
  1335.         return 0;
  1336.  
  1337.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='a' && prototypeBoard[iDestination][jDestination]<='z')
  1338.         return 0;
  1339.  
  1340.     else if(prototypeBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
  1341.         return 0;
  1342.  
  1343.     else if(prototypeBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
  1344.         return 0;
  1345.  
  1346.     else
  1347.         return 1;
  1348.  
  1349. }
  1350.  
  1351.  
  1352.  
  1353. int prototypeQueen(int iCurrent,int iDestination,int jCurrent,int jDestination)
  1354. {
  1355.     int iDiff,jDiff;
  1356.     iDiff=iDestination-iCurrent;
  1357.     jDiff=jDestination-jCurrent;
  1358.  
  1359.     if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& prototypeRook(iCurrent,iDestination,jCurrent,jDestination)==0)
  1360.  
  1361.         return 0;
  1362.  
  1363.  
  1364.     else if (abs(iDiff)==abs(jDiff) && prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==0)
  1365.  
  1366.         return 0;
  1367.  
  1368.     else
  1369.  
  1370.         return 1;
  1371.  
  1372. }
  1373.  
  1374.  
  1375. void findKings()
  1376. {
  1377.     for(int i=0; i<8; i++)
  1378.     {
  1379.         for(int j=0; j<8; j++)
  1380.         {
  1381.             if(piecesBoard[i][j]=='k')
  1382.             {
  1383.                 iWhiteKing = i;
  1384.                 jWhiteKing = j;
  1385.             }
  1386.             else if(piecesBoard[i][j]=='K')
  1387.             {
  1388.                 iBlackKing = i;
  1389.                 jBlackKing = j;
  1390.             }
  1391.         }
  1392.     }
  1393. }
  1394.  
  1395.  
  1396. void trackUndo(int iCurrent, int iDestination, int jCurrent, int jDestination)
  1397. {
  1398.     undo[movesCount] = iCurrent*1000 + iDestination*100 + jCurrent*10 + jDestination;
  1399.     movesCount++;
  1400.     if(piecesBoard[iDestination][jDestination] != '\0')
  1401.     {
  1402.         capturingTrack[captureTurn] = turn;
  1403.         captureTurn++;
  1404.     }
  1405. }
  1406.  
  1407.  
  1408.  
  1409. void processUndo()
  1410. {
  1411.     int iC,iD,jC,jD;
  1412.  
  1413.     movesCount--;
  1414.     iC = undo[movesCount]/1000;
  1415.     iD = undo[movesCount]/100%10;
  1416.     jC = undo[movesCount]/10%10;
  1417.     jD = undo[movesCount]%10;
  1418.     int f=0;
  1419.     if((turn-1)==capturingTrack[captureTurn-1])
  1420.     {
  1421.         move(iD,iC,jD,jC);
  1422.         if(turn%2==1)
  1423.         {
  1424.             piecesBoard[iD][jD] = capturedWhite[countWhite-1];
  1425.             countWhite--;
  1426.         }
  1427.         else if(turn%2==0)
  1428.         {
  1429.             piecesBoard[iD][jD] = capturedBlack[countBlack-1];
  1430.             countBlack--;
  1431.         }
  1432.         captureTurn--;
  1433.         f=1;
  1434.  
  1435.  
  1436.         if((turn-1)==promotionTrack[promoteTurn-1] && promoteTurn!=0)
  1437.         {
  1438.             if(f==0)
  1439.                 move(iD,iC,jD,jC);
  1440.             promoteTurn--;
  1441.             if(turn%2==1)
  1442.                 piecesBoard[iC][jC] = 'P';
  1443.             else if(turn%2==0)
  1444.                 piecesBoard[iC][jC] = 'p';
  1445.  
  1446.         }
  1447.     }
  1448.     else
  1449.     {
  1450.         move(iD, iC, jD, jC);
  1451.     }
  1452.     findKings();
  1453.     correspondPrototypeBoard();
  1454.     turn-=2;
  1455. }
  1456.  
  1457. void processRedo()
  1458. {
  1459.     int iC, iD, jC, jD;
  1460.     iC = undo[movesCount]/1000;
  1461.     iD = undo[movesCount]/100%10;
  1462.     jC = undo[movesCount]/10%10;
  1463.     jD = undo[movesCount]%10;
  1464.     trackUndo(iC,iD,jC,jD);
  1465.     move(iC,iD,jC,jD);
  1466.     findKings();
  1467.     correspondPrototypeBoard();
  1468.  
  1469.     if((turn)==capturingTrack[captureTurn-1])
  1470.     {
  1471.         if(turn%2==0)
  1472.         {
  1473.             countWhite++;
  1474.         }
  1475.         else if(turn%2==1)
  1476.         {
  1477.             countBlack++;
  1478.         }
  1479.         if((turn)== promotionTrack[promoteTurn])
  1480.         {
  1481.             piecesBoard[iD][jD] = promotionPieces[promoteTurn];
  1482.             promoteTurn++;
  1483.         }
  1484.     }
  1485. }
  1486.  
  1487. int predictPromotion()
  1488. {
  1489.     int f=0;
  1490.     for(int j=0; j<8; j++)
  1491.     {
  1492.         if(turn%2==1)
  1493.         {
  1494.             if(piecesBoard[1][j]=='p')
  1495.             {
  1496.                 f=1;
  1497.                 break;
  1498.             }
  1499.         }
  1500.         else if(turn%2==0)
  1501.         {
  1502.             if(piecesBoard[1][j]=='P')
  1503.             {
  1504.                 f=1;
  1505.                 break;
  1506.             }
  1507.         }
  1508.     }
  1509.     return f;
  1510. }
  1511.  
  1512. int promote(int i, int j, char type)
  1513. {
  1514.     if(predictPromotion()==1)
  1515.     {
  1516.         if(piecesBoard[i][j]=='p' && (type=='q' || type=='b' || type=='n') && turn%2==1)
  1517.             return 1;
  1518.         else if(piecesBoard[i][j]=='P' && (type=='Q' || type=='B' || type=='N') && turn%2==0)
  1519.             return 1;
  1520.     }
  1521.     return 0;
  1522. }
  1523.  
  1524.  
  1525. void save()
  1526. {    int flag;
  1527.     char z,v,x[1],n[2];
  1528.     log1 = fopen("PiecesBoard.txt", "w");
  1529.  
  1530.     for (int i=0; i<8; i++)
  1531.     {
  1532.         for (int j=0; j<8; j++)
  1533.         {
  1534.             z=piecesBoard[i][j];
  1535.             fputc(z,log1);
  1536.         }
  1537.     }
  1538.     log2 = fopen("CapturedBlack.txt","w");
  1539.     for(int i=0; i<countBlack; i++)
  1540.     {
  1541.         z=capturedBlack[i];
  1542.         fputc(z,log2);
  1543.     }
  1544.     log3 = fopen("CapturedWhite.txt","w");
  1545.     for (int i=0; i<countWhite; i++)
  1546.     {
  1547.         z=capturedWhite[i];
  1548.         fputc(z,log3);
  1549.     }
  1550.  
  1551.     fclose(log1);
  1552.     fclose(log2);
  1553.     fclose(log3);
  1554.     log4 = fopen ("turn.txt","w");
  1555.     v=(char)turn%2+'0';
  1556.     fputc(v,log4);
  1557.     log5 = fopen("BlackCounter.txt","w");
  1558.     if (countBlack>=10){
  1559.         n[0]=countBlack/10;
  1560.         n[1]=countBlack%10;
  1561.         fputs(n,log5);
  1562.         fclose(log5);
  1563.     }
  1564.     else if (countBlack<10){
  1565.         x[0]=countBlack;
  1566.         fputs(x,log5);
  1567.         fclose(log5);
  1568.     }
  1569.     if (countBlack>=10){
  1570.         n[0]=countBlack/10;
  1571.         n[1]=countBlack%10;
  1572.         fputs(n,log5);
  1573.         fclose(log5);
  1574.     }
  1575.     else if (countBlack<10){
  1576.         x[0]=countBlack;
  1577.         fputs(x,log5);
  1578.         fclose(log5);
  1579.     }
  1580.     if (countWhite>=10){
  1581.         n[0]=countWhite/10;
  1582.         n[1]=countWhite%10;
  1583.         fputs(n,log6);
  1584.         fclose(log6);
  1585.     }
  1586.     else if (countWhite<10){
  1587.         x[0]=countWhite;
  1588.         fputs(x,log6);
  1589.         fclose(log6);
  1590.     }
  1591.  
  1592.  
  1593.  
  1594.     fclose(log4);
  1595.     system("cls");
  1596.     flag=2;
  1597. }
  1598.  
  1599. void load()
  1600. {
  1601.     char z,v;
  1602.     log1 = fopen("PiecesBoard.txt", "r");
  1603.     if (log1==NULL)
  1604.     {
  1605.         printf("ERROR: Could Not Find The File!\n");
  1606.     }
  1607.     else
  1608.     {
  1609.  
  1610.         for(int i=0; i<8; i++)
  1611.         {
  1612.             for(int j=0; j<8; j++)
  1613.             {
  1614.                 z = fgetc(log1);
  1615.                 piecesBoard[i][j] = z;
  1616.  
  1617.             }
  1618.         }
  1619.  
  1620.     }
  1621.     log2=fopen("CapturedBlack.txt","r");
  1622.     for(int i=0; i<16; i++)
  1623.     {
  1624.         z=fgetc(log2);
  1625.         capturedBlack[i]=z;
  1626.  
  1627.     }
  1628.     log3=fopen("CapturedWhite.txt","r");
  1629.     for (int i=0; i<16; i++)
  1630.     {
  1631.         z= fgetc(log3);
  1632.         capturedWhite[i]=z;
  1633.     }
  1634.     log4=fopen("turn.txt","r");
  1635.  
  1636.     turn=(int)fgetc(log4)- (int)'0';
  1637.     if(turn==0)
  1638.         turn=2;
  1639.  
  1640.  
  1641.     fclose(log1);
  1642.     fclose(log2);
  1643.     fclose(log3);
  1644.     fclose(log4);
  1645.  
  1646.     movesCount=0;
  1647.     for(int i=0; i!='\0'; i++)
  1648.     {
  1649.         undo[i]=0;
  1650.     }
  1651.  
  1652.     system("cls");
  1653.     printf("\nCaptured White Pieces: ");
  1654.     for(int i=0; i<countWhite; i++)
  1655.         printf("%c ", capturedWhite[i]);
  1656.     printf("\nCaptured Black Pieces: ");
  1657.     for(int i=0; i<countBlack; i++)
  1658.         printf("%c ", capturedBlack[i]);
  1659.     printf("\n\n");
  1660. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement