Advertisement
redsees

Untitled

Dec 24th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <stdlib.h>
  5. #include <vector>
  6. #include <string.h>
  7. #ifdef OS_WINDOWS
  8. #include <windows.h>
  9. #include <time.h>
  10. #define LET_ME_SLEEP Sleep(7000);
  11. #define LET_ME_CLEAR system("cls");
  12. #else
  13. #include <unistd.h>
  14. #define LET_ME_SLEEP sleep(2);
  15. #define LET_ME_CLEAR system("clear");
  16. #endif
  17. using namespace std;
  18.  
  19.  
  20. void StartScreen();
  21. void SetBoard ();
  22. void DispBoard ();
  23. bool Check(char c);
  24. int  Game (char c);
  25. char SwitchTurn (char c);
  26. bool CanMove(char c);
  27. bool EndGame(char c);
  28.  
  29. class Piece{
  30.  
  31.     protected:
  32.         char color;
  33.  
  34.     public:
  35.         Piece(char c):color(c){}
  36.         virtual ~Piece() {}
  37.         virtual char GetPiece() = 0;
  38.         virtual bool IsLegalMove(int iRow, int iCol, int fRow, int fCol) = 0;
  39.  
  40.         char GetColor()
  41.         {
  42.             return color;
  43.         }
  44.  
  45. };
  46.  
  47. Piece* ChessBoard[8][8];
  48. struct Contain {
  49.     Piece* Board[8][8];
  50. };
  51.  
  52. vector <Contain> MyVector;
  53. class Pawn : public Piece
  54. {
  55.     public:
  56.         Pawn(char c) : Piece(c) {}
  57.         ~Pawn() {}
  58.  
  59.         char GetPiece()
  60.         {
  61.             return 'P';
  62.         }
  63.  
  64.         bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
  65.         {
  66.             Piece* finalPos = ChessBoard[fRow][fCol];
  67.  
  68.             if ((finalPos == 0) || (color != finalPos -> GetColor()))
  69.             {
  70.                 if (finalPos == 0)
  71.                 { // hayt7rak
  72.                     if (iCol == fCol)
  73.                     {
  74.                         if (GetColor() == 'W')
  75.                         {
  76.                             if ((fRow == iRow - 1) || ((fRow == 4) && (iRow == 6) && (ChessBoard[5][iCol] == 0)))
  77.                             {
  78.                                 return true;
  79.                             }
  80.                         }
  81.                         else
  82.                         {
  83.                             if ((fRow == iRow + 1) || ((fRow == 3) && (iRow == 1) && (ChessBoard[2][iCol] == 0)))
  84.                             {
  85.                                 return true;
  86.                             }
  87.                         }
  88.                     }
  89.                 }
  90.                 else
  91.                 {
  92.                 // hayakol
  93.                     if ((iCol == fCol + 1) || (iCol == fCol - 1))
  94.                     {
  95.                         if (GetColor() == 'W')
  96.                         {
  97.                             if (fRow == iRow - 1)
  98.                             {
  99.                                 return true;
  100.                             }
  101.                         }
  102.                         else
  103.                         {
  104.                             if (fRow == iRow + 1)
  105.                             {
  106.                                 return true;
  107.                             }
  108.                         }
  109.                     }
  110.                 }
  111.  
  112.                 return false;
  113.             }
  114.  
  115.             return false;
  116.         }
  117. };
  118.  
  119. class Rook : public Piece
  120. {
  121.     public:
  122.         Rook(char c) : Piece(c) {}
  123.         ~Rook() {}
  124.  
  125.         char GetPiece()
  126.         {
  127.             return 'R';
  128.         }
  129.  
  130.         bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
  131.         {
  132.             Piece* finalPos = ChessBoard[fRow][fCol];
  133.             if ((finalPos == 0) || (color != finalPos -> GetColor()))
  134.             {
  135.                 if (((abs(fRow - iRow) >0) && (abs (fCol - iCol) == 0)) || ((abs(fRow - iRow) == 0) && (abs (fCol - iCol) > 0)) )
  136.                 {
  137.                     if (iRow == fRow) // 3ard
  138.                     {
  139.                         int Check_Number = abs (fCol-iCol) - 1; // kam check hane3mlo
  140.                         if (fCol>iCol) // ymeen
  141.                         {
  142.                             for (int count = 0; count < Check_Number; count++)
  143.                             {
  144.                                 if (ChessBoard[iRow][++iCol] != 0)
  145.                                     return false;
  146.                             }
  147.                         }
  148.  
  149.                             else if (fCol<iCol) // shmal
  150.                         {
  151.                                 for (int count = 0; count < Check_Number; count++)
  152.                                 {
  153.                                     if (ChessBoard[iRow][--iCol] != 0)
  154.                                         return false;
  155.                                 }
  156.                         }
  157.                     }
  158.  
  159.                     else if (iCol == fCol) // tool
  160.                     {
  161.                         int Check_Number = abs (fRow - iRow) - 1; // kam check hane3mlo
  162.                         //cout << "kam marra: " << Check_Number << endl;
  163.  
  164.                         if (fRow>iRow) // ta7t
  165.                         {
  166.                             //cout << "ta7t" << endl;
  167.  
  168.                             for (int count = 0; count < Check_Number; count++)
  169.                             {
  170.                                 if (ChessBoard[++iRow][iCol] != 0)
  171.                                     return false;
  172.                             }
  173.                         }
  174.  
  175.                             else if (fRow<iRow) // foo2
  176.                         {
  177.                             //cout << "foo2" << endl;
  178.                                 for (int count = 0; count < Check_Number; count++)
  179.                                 {
  180.                                     if (ChessBoard[--iRow][iCol] != 0)
  181.                                         return false;
  182.                                 }
  183.                         }
  184.                     }
  185.                     return true;
  186.                 }
  187.                 return false;
  188.  
  189.             }
  190.             return false;
  191.         }
  192. };
  193.  
  194. class Knight : public Piece
  195. {
  196.     public:
  197.         Knight(char c) : Piece(c) {}
  198.         ~Knight() {}
  199.  
  200.         char GetPiece()
  201.         {
  202.             return 'N';
  203.         }
  204.  
  205.         bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
  206.         {
  207.             Piece* finalPos = ChessBoard[fRow][fCol];
  208.             if ((finalPos == 0) || (color != finalPos -> GetColor()))
  209.             {
  210.                 // 5atwa bel tool w 5atweten bel ganb
  211.                 if (abs(fCol - iCol) == 2)
  212.                 {
  213.                     if (abs(fRow - iRow) == 1)
  214.                         return true;
  215.                 }
  216.  
  217.                 // 5atwa bel ganb w 5atweten bel tool
  218.                 if (abs(fCol - iCol) == 1)
  219.                 {
  220.                     if (abs(fRow - iRow) == 2)
  221.                         return true;
  222.                 }
  223.                 return false;
  224.             }
  225.             return false;
  226.         }
  227. };
  228.  
  229. class Bishop : public Piece
  230. {
  231.     public:
  232.         Bishop(char c) : Piece(c) {}
  233.         ~Bishop() {}
  234.  
  235.         char GetPiece()
  236.         {
  237.             return 'B';
  238.         }
  239.  
  240.         bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
  241.         {
  242.             Piece* finalPos = ChessBoard[fRow][fCol];
  243.             if ((finalPos == 0) || (color != finalPos -> GetColor()))
  244.             {
  245.                 if (abs(fRow - iRow) == abs(fCol - iCol) && (abs (fRow - iRow) > 0)) // net2aked eno et7rak aslan w law et7rak net2aked eno diagonally
  246.                 {
  247.                     int Check_Number = abs (fRow-iRow) - 1;// aw fcol-icol mesh far2a // kam check hane3mlo
  248.  
  249.                     //cout << "kam marra: " << Check_Number << endl;
  250.                     if (fRow < iRow) // et7rak foo2
  251.                     {
  252.                         if (fCol>iCol) // et7rak foo2 ymeen
  253.                         {
  254.                             for (int count = 0; count < Check_Number; count++)
  255.                             {
  256.                                 if (ChessBoard[--iRow][++iCol] != 0)
  257.                                     return false;
  258.                             }
  259.                         }
  260.  
  261.                         else if (fCol<iCol) // foo2 shmal
  262.                         {
  263.                             for (int count = 0; count < Check_Number; count++)
  264.                             {
  265.                                 if (ChessBoard[--iRow][--iCol] != 0)
  266.                                     return false;
  267.                             }
  268.                         }
  269.                     }
  270.  
  271.                     else if (fRow>iRow) // et7rak ta7t
  272.                     {
  273.                         if (fCol>iCol) // ta7t ymeen
  274.                         {
  275.                             for (int count = 0; count < Check_Number; count++)
  276.                             {
  277.                                 if (ChessBoard[++iRow][++iCol] != 0)
  278.                                     return false;
  279.                             }
  280.                         }
  281.  
  282.                         else if (fCol<iCol) // t7t shmal
  283.                         {
  284.                             for (int count = 0; count < Check_Number; count++)
  285.                             {
  286.                                 if (ChessBoard[++iRow][--iCol] != 0)
  287.                                     return false;
  288.                             }
  289.                         }
  290.                     }
  291.                     return true;
  292.                 }
  293.  
  294.                 return false;
  295.             }
  296.             return false;
  297.         }
  298. };
  299.  
  300. class Queen : public Piece
  301. {
  302.     public:
  303.         Queen(char c) : Piece(c) {}
  304.         ~Queen() {}
  305.  
  306.         char GetPiece()
  307.         {
  308.             return 'Q';
  309.         }
  310.  
  311.         bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
  312.         {
  313.             Piece* finalPos = ChessBoard[fRow][fCol];
  314.             if ((finalPos == 0) || (color != finalPos -> GetColor()))
  315.             {
  316.                 if (((abs(fRow - iRow) >0) && (abs (fCol - iCol) == 0)) || ((abs(fRow - iRow) == 0) && (abs (fCol - iCol) > 0)) )
  317.                 {
  318.                     if (iRow == fRow) // 3ard
  319.                     {
  320.                         int Check_Number = abs (fCol-iCol) - 1; // kam check hane3mlo
  321.                         if (fCol>iCol) // ymeen
  322.                         {
  323.                             for (int count = 0; count < Check_Number; count++)
  324.                             {
  325.                                 if (ChessBoard[iRow][++iCol] != 0)
  326.                                     return false;
  327.                             }
  328.                         }
  329.  
  330.                             else if (fCol<iCol) // shmal
  331.                         {
  332.                                 for (int count = 0; count < Check_Number; count++)
  333.                                 {
  334.                                     if (ChessBoard[iRow][--iCol] != 0)
  335.                                         return false;
  336.                                 }
  337.                         }
  338.                     }
  339.  
  340.                     else if (iCol == fCol) // tool
  341.                     {
  342.                         int Check_Number = abs (fRow - iRow) - 1; // kam check hane3mlo
  343.                        // cout << "kam marra: " << Check_Number << endl;
  344.  
  345.                         if (fRow>iRow) // ta7t
  346.                         {
  347.                             //cout << "ta7t" << endl;
  348.  
  349.                             for (int count = 0; count < Check_Number; count++)
  350.                             {
  351.                                 if (ChessBoard[++iRow][iCol] != 0)
  352.                                     return false;
  353.                             }
  354.                         }
  355.  
  356.                             else if (fRow<iRow) // foo2
  357.                         {
  358.                            // cout << "foo2" << endl;
  359.                                 for (int count = 0; count < Check_Number; count++)
  360.                                 {
  361.                                     if (ChessBoard[--iRow][iCol] != 0)
  362.                                         return false;
  363.                                 }
  364.                         }
  365.                     }
  366.                     return true;
  367.                 }
  368.  
  369.                 else if (abs(fRow - iRow) == abs(fCol - iCol) && (abs (fRow - iRow) > 0)) // net2aked eno et7rak aslan w law et7rak net2aked eno diagonally
  370.                 {
  371.                     int Check_Number = abs (fRow-iRow) - 1;// aw fcol-icol mesh far2a // kam check hane3mlo
  372.  
  373.                     //cout << "kam marra: " << Check_Number << endl;
  374.                     if (fRow < iRow) // et7rak foo2
  375.                     {
  376.                         if (fCol>iCol) // et7rak foo2 ymeen
  377.                         {
  378.                             for (int count = 0; count < Check_Number; count++)
  379.                             {
  380.                                 if (ChessBoard[--iRow][++iCol] != 0)
  381.                                     return false;
  382.                             }
  383.                         }
  384.  
  385.                         else if (fCol<iCol) // foo2 shmal
  386.                         {
  387.                             for (int count = 0; count < Check_Number; count++)
  388.                             {
  389.                                 if (ChessBoard[--iRow][--iCol] != 0)
  390.                                     return false;
  391.                             }
  392.                         }
  393.                     }
  394.  
  395.                     else if (fRow>iRow) // et7rak ta7t
  396.                     {
  397.                         if (fCol>iCol) // ta7t ymeen
  398.                         {
  399.                             for (int count = 0; count < Check_Number; count++)
  400.                             {
  401.                                 if (ChessBoard[++iRow][++iCol] != 0)
  402.                                     return false;
  403.                             }
  404.                         }
  405.  
  406.                         else if (fCol<iCol) // t7t shmal
  407.                         {
  408.                             for (int count = 0; count < Check_Number; count++)
  409.                             {
  410.                                 if (ChessBoard[++iRow][--iCol] != 0)
  411.                                     return false;
  412.                             }
  413.                         }
  414.                     }
  415.                     return true;
  416.                 }
  417.                 return false;
  418.  
  419.             }
  420.             return false;
  421.         }
  422. };
  423.  
  424. class King : public Piece
  425. {
  426.     public:
  427.         King(char c) : Piece(c) {}
  428.         ~King() {}
  429.  
  430.         char GetPiece()
  431.         {
  432.             return 'K';
  433.         }
  434.  
  435.         bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
  436.         {
  437.             Piece* finalPos = ChessBoard[fRow][fCol];
  438.             if ((finalPos == 0) || (color != finalPos -> GetColor()))
  439.             {
  440.                 if ((((fRow - iRow) >= -1) && ((fRow - iRow) <= 1)) && (((fCol - iCol) >= -1) && ((fCol - iCol) <= 1)))
  441.                 {
  442.                     return true;
  443.                 }
  444.                 return false;
  445.             }
  446.             return false;
  447.         }
  448. };
  449.  
  450. int wScore = 0, bScore = 0;
  451.  
  452. int main()
  453. {
  454.  
  455.     char Turn;
  456.     int i,LOL,x;
  457.     StartScreen();
  458.     here:
  459.     Turn='W';
  460.         SetBoard();
  461.         do
  462.         {
  463.             DispBoard();
  464.             LOL=Game(Turn);
  465.             if(LOL==-1)goto here;
  466.             Contain mytemp;
  467.             for (int myrows=0; myrows<8; myrows++)
  468.             {
  469.                 for (int mycols=0; mycols<8; mycols++)
  470.                 {
  471.                        mytemp.Board[myrows][mycols] = ChessBoard[myrows][mycols];
  472.                 }
  473.             }
  474.             MyVector.push_back(mytemp);
  475.             Turn = SwitchTurn(Turn);
  476.         } while (EndGame(Turn) == false);
  477.     cout<<"\n\nDo you want to play another game ?\n[1]for Yes\n[2]for No\n\n";
  478.     cin>>i;
  479.     if(i==1)goto here;
  480.     return 0;
  481. }
  482.  
  483. void StartScreen(){
  484.     cout<<"#################################################\n";
  485.     cout<<"#################################################\n";
  486.     cout<<"####     BHB Ultimate CLI Chess Game v0.1    ####\n";
  487.     cout<<"#################################################\n\n\n";
  488.     cout<<"You can switch to one of these commands by inserting the command number :\n";
  489.     cout<<"-Reset Game  ==> 100\n";
  490.     cout<<"-Undo  Game  ==> 101\n";
  491.     cout<<"-Exit  Game  ==> 102\n\n";
  492.     LET_ME_SLEEP
  493.     }
  494. void SetBoard ()
  495. {
  496.     int rows,cols;
  497.     for (rows=0; rows<8; rows++)
  498.     {
  499.         for (cols=0; cols<8; cols++)
  500.             ChessBoard[rows][cols] = 0; // Set kolaha b NULL
  501.     }
  502.  
  503.     // Set el black pieces
  504.     cols = 0;
  505.     ChessBoard[0][cols++] = new Rook('B');
  506.     ChessBoard[0][cols++] = new Knight('B');
  507.     ChessBoard[0][cols++] = new Bishop('B');
  508.     ChessBoard[0][cols++] = new Queen('B');
  509.     ChessBoard[0][cols++] = new King('B');
  510.     ChessBoard[0][cols++] = new Bishop('B');
  511.     ChessBoard[0][cols++] = new Knight('B');
  512.     ChessBoard[0][cols] =  new Rook('B');
  513.  
  514.     cols = 0;
  515.     while (cols<8)
  516.         ChessBoard[1][cols++] = new Pawn('B');
  517.  
  518.     // Set el white pieces
  519.     cols = 0;
  520.     ChessBoard[7][cols++] = new Rook('W');
  521.     ChessBoard[7][cols++] = new Knight('W');
  522.     ChessBoard[7][cols++] = new Bishop('W');
  523.     ChessBoard[7][cols++] = new Queen('W');
  524.     ChessBoard[7][cols++] = new King('W');
  525.     ChessBoard[7][cols++] = new Bishop('W');
  526.     ChessBoard[7][cols++] = new Knight('W');
  527.     ChessBoard[7][cols] = new Rook('W');
  528.  
  529.     cols = 0;
  530.     while (cols<8)
  531.         ChessBoard[6][cols++] = new Pawn('W');
  532.     Contain mytemp;
  533.     for (int myrows=0; myrows<8; myrows++)
  534.     {
  535.          for (int mycols=0; mycols<8; mycols++)
  536.          {
  537.             mytemp.Board[myrows][mycols] = ChessBoard[myrows][mycols];
  538.          }
  539.    }
  540.    MyVector.push_back(mytemp);
  541. }
  542.  
  543. void DispBoard ()
  544. {
  545.     LET_ME_CLEAR
  546.  
  547.     for (int rows=0; rows<8; rows++)
  548.     {
  549.         cout << rows+1 << "     ";
  550.         for (int cols=0; cols<8; cols++)
  551.         {
  552.             if (ChessBoard[rows][cols] == 0)
  553.             {
  554.                 if ((rows %2 ==0 && cols %2 == 0) || (rows %2 !=0 && cols %2 != 0))
  555.                     cout << "--";
  556.                 else
  557.                     cout << "++";
  558.             }
  559.             else
  560.                 cout << ChessBoard[rows][cols]->GetColor() << ChessBoard [rows][cols] -> GetPiece();
  561.  
  562.             cout << " ";
  563.         }
  564.  
  565.         if (rows == 4)
  566.         {
  567.             cout << "            White: " << wScore << "    " << "Black: " << bScore;
  568.         }
  569.  
  570.         cout << "\n" << endl;
  571.  
  572.     }
  573.     cout << "     ";
  574.     // bytba3 el arqam ta7t
  575.     for (int x = 1; x<9; x++)
  576.         cout << setw(2) << x << " ";
  577.  
  578.     cout << "\n" << endl;
  579. }
  580.  
  581. int Game (char c)
  582. {
  583.     bool Valid = false;
  584.     do
  585.     {
  586.         cout << c << "'s turn: ";
  587.         int Start;
  588.         cin >> Start;
  589.         if(Start == 100)return -1;
  590.         else if (Start == 101)
  591.         {
  592.                 Piece* TempPiece;
  593.                 Contain TempCon;
  594.                 MyVector.pop_back();
  595.                 TempCon = MyVector.back();
  596.                 MyVector.pop_back();
  597.                 for (int rows=0; rows<8; rows++)
  598.                 {
  599.                     for (int cols=0; cols<8; cols++)
  600.                     {
  601.                         TempPiece = TempCon.Board[rows][cols];
  602.                         ChessBoard [rows][cols] = TempPiece;
  603.                     }
  604.  
  605.                 }
  606.             DispBoard();
  607.             return 1;
  608.         } //CODE FOR UNDO
  609.         else if (Start == 102){cout<<"\n\nExiting BHB Chess ...\n\n";exit(0);}
  610.         int StartRow = (Start / 10) - 1;
  611.         int StartCol = (Start % 10) - 1;
  612.  
  613.         cout << "To: ";
  614.         int End;
  615.         cin >> End;
  616.         int EndRow = (End / 10) - 1;
  617.         int EndCol = (End % 10) - 1;
  618.  
  619.         if ((StartRow >= 0 && StartRow <= 7) && (StartCol >= 0 && StartCol <= 7) &&(EndRow >= 0 && EndRow <= 7) &&(EndCol >= 0 && EndCol <= 7))
  620.         {
  621.             //cout << "Hi 1" << endl;
  622.             Piece * CurrentPiece = ChessBoard [StartRow][StartCol];
  623.             if ((CurrentPiece != 0) && (CurrentPiece -> GetColor() == c))
  624.             {
  625.                 //cout << "Hi 2" << endl;
  626.                 if (CurrentPiece->IsLegalMove(StartRow, StartCol, EndRow, EndCol))
  627.                 {
  628.                     //cout << "Hi 3" << endl;
  629.                     Piece* TempPiece = ChessBoard[EndRow][EndCol];
  630.                     ChessBoard[EndRow][EndCol] = ChessBoard[StartRow][StartCol];
  631.                     ChessBoard[StartRow][StartCol] = 0;
  632.  
  633.                    // cout << c << endl;
  634.                     if (!Check(c))
  635.                     {
  636.                         //cout << "Hi 4" << endl;
  637.                         delete TempPiece;
  638.                         Valid = true;
  639.  
  640.                     }
  641.  
  642.                     else
  643.                     { // Undo the last move
  644.                         ChessBoard[StartRow][StartCol] = ChessBoard[EndRow][EndCol];
  645.                         ChessBoard[EndRow][EndCol]= TempPiece;
  646.                     }
  647.                 }
  648.             }
  649.         }
  650.  
  651.         if (Valid == false)
  652.         {
  653.             cout << "Invalid Move" << endl;
  654.         }
  655.     } while (Valid == false);
  656.     return 0;
  657. }
  658.  
  659. char SwitchTurn (char c)
  660. {
  661.     if (c == 'W')
  662.         return 'B';
  663.     else
  664.         return 'W';
  665. }
  666.  
  667. bool Check(char c)
  668. {
  669.     int KingRow,KingCol;
  670.     for (int Row = 0; Row < 8; Row++)
  671.     {
  672.         for (int Col = 0; Col < 8; Col++)
  673.         {
  674.             if (ChessBoard[Row][Col] != 0)
  675.             {
  676.                 if ((ChessBoard[Row][Col]-> GetColor() == c) && (ChessBoard [Row][Col]->GetPiece() =='K'))
  677.                 {
  678.                     KingRow = Row;
  679.                     KingCol = Col;
  680.  
  681.                     //cout << KingRow << " "  << KingCol << endl;
  682.                 }
  683.             }
  684.         }
  685.     }
  686.  
  687.     for (int Row = 0; Row < 8; Row++)
  688.     {
  689.         for (int Col = 0; Col < 8; Col++)
  690.         {
  691.             if (ChessBoard[Row][Col] != 0)
  692.             {
  693.                 if (ChessBoard[Row][Col]->GetColor() != c)
  694.                 {
  695.                     if (ChessBoard[Row][Col]->IsLegalMove(Row, Col, KingRow, KingCol))
  696.                     {
  697.                         //cout << ChessBoard[Row][Col]->GetColor()  << ChessBoard[Row][Col]->GetPiece() << endl;
  698.                         return true;
  699.                     }
  700.                 }
  701.             }
  702.         }
  703.     }
  704.  
  705.     return false;
  706. }
  707.  
  708. bool CanMove(char c)
  709. {
  710.     // Run through all pieces
  711.     for (int iRow = 0; iRow < 8; iRow++)
  712.     {
  713.         for (int iCol = 0; iCol < 8; iCol++)
  714.         {
  715.             if ((ChessBoard[iRow][iCol] != 0) && (ChessBoard[iRow][iCol]->GetColor() == c))
  716.             {
  717.                 // If it is a piece of the current player, see if it has a legal move
  718.                 for (int fRow = 0; fRow < 8; fRow++)
  719.                 {
  720.                     for (int fCol = 0; fCol < 8; fCol++)
  721.                     {
  722.                         if (ChessBoard[iRow][iCol]->IsLegalMove(iRow, iCol, fRow, fCol))
  723.                         {
  724.                             // Make move and check whether king is in check
  725.                             Piece* Temp = ChessBoard[fRow][fCol];
  726.                             ChessBoard[fRow][fCol] = ChessBoard[iRow][iCol];
  727.                             ChessBoard[iRow][iCol] = 0;
  728.                             bool Move_Bool = !Check(c);
  729.                             // fel 7alteen Undo the move
  730.                             ChessBoard[iRow][iCol] = ChessBoard[fRow][fCol];
  731.                             ChessBoard[fRow][fCol] = Temp;
  732.                             if (Move_Bool)
  733.                             {
  734.                                 return true;
  735.                             }
  736.                         }
  737.                     }
  738.                 }
  739.             }
  740.         }
  741.     }
  742.     return false;
  743. }
  744.  
  745. bool EndGame(char c)
  746. {
  747.     char Turn;
  748.     // Check that the current player can move
  749.     // If not, we have a stalemate or checkmate
  750.     bool Move_Bool(false);
  751.     Move_Bool = CanMove(c);
  752.  
  753.     if (Move_Bool == false)
  754.     {
  755.         if (Check(c))
  756.         {
  757.             Turn = SwitchTurn(c);
  758.             cout << "Checkmate, " << Turn << " Wins!" << endl;
  759.             if (Turn == 'W')
  760.             {
  761.                 wScore++;
  762.             }
  763.  
  764.             else
  765.             {
  766.                 bScore++;
  767.             }
  768.  
  769.         }
  770.  
  771.         else
  772.         {
  773.             cout << "Stalemate!" << endl;
  774.             wScore++;
  775.             bScore++;
  776.         }
  777.     }
  778.     return !Move_Bool;
  779. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement