Advertisement
anas_harby

Untitled

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