Advertisement
anas_harby

Untitled

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