Advertisement
Guest User

Untitled

a guest
Apr 11th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <vector>
  4. #include <iterator>
  5. class GamePiece
  6. {
  7. public:
  8.     GamePiece(char PieceColor) : mPieceColor(PieceColor) {}
  9.     ~GamePiece() {}
  10.     virtual char GetPiece() = 0;
  11.     char GetColor() {
  12.         return mPieceColor;
  13.     }
  14.     bool IsLegalMove(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  15.         GamePiece* qpDest = GameBoard[iDestRow][iDestCol];
  16.         if ((qpDest == 0) || (mPieceColor != qpDest->GetColor())) {
  17.             return AreSquaresLegal(iSrcRow, iSrcCol, iDestRow, iDestCol, GameBoard);
  18.         }
  19.         return false;
  20.     }
  21. private:
  22.     virtual bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) = 0;
  23.     char mPieceColor;
  24. };
  25.  
  26. class PawnPiece : public GamePiece
  27. {
  28. public:
  29.     PawnPiece(char PieceColor) : GamePiece(PieceColor) {}
  30.     ~PawnPiece() {}
  31. private:
  32.     virtual char GetPiece() {
  33.         return 'P';
  34.     }
  35.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  36.         GamePiece* qpDest = GameBoard[iDestRow][iDestCol];
  37.         if (qpDest == 0) {
  38.             // Destination square is unoccupied
  39.             if (iSrcCol == iDestCol) {
  40.                 if (GetColor() == 'W') {
  41.                     if (iDestRow == iSrcRow + 1) {
  42.                         return true;
  43.                     }
  44.                 } else {
  45.                     if (iDestRow == iSrcRow - 1) {
  46.                         return true;
  47.                     }
  48.                 }
  49.             }
  50.         } else {
  51.             // Dest holds piece of opposite color
  52.             if ((iSrcCol == iDestCol + 1) || (iSrcCol == iDestCol - 1)) {
  53.                 if (GetColor() == 'W') {
  54.                     if (iDestRow == iSrcRow + 1) {
  55.                         return true;
  56.                     }
  57.                 } else {
  58.                     if (iDestRow == iSrcRow - 1) {
  59.                         return true;
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         return false;
  65.     }
  66. };
  67.  
  68. class KnightPiece : public GamePiece
  69. {
  70. public:
  71.     KnightPiece(char PieceColor) : GamePiece(PieceColor) {}
  72.     ~KnightPiece() {}
  73. private:
  74.     virtual char GetPiece() {
  75.         return 'N';
  76.     }
  77.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  78.         // Destination square is unoccupied or occupied by opposite color
  79.         if ((iSrcCol == iDestCol + 1) || (iSrcCol == iDestCol - 1)) {
  80.             if ((iSrcRow == iDestRow + 2) || (iSrcRow == iDestRow - 2)) {
  81.                 return true;
  82.             }
  83.         }
  84.         if ((iSrcCol == iDestCol + 2) || (iSrcCol == iDestCol - 2)) {
  85.             if ((iSrcRow == iDestRow + 1) || (iSrcRow == iDestRow - 1)) {
  86.                 return true;
  87.             }
  88.         }
  89.         return false;
  90.     }
  91. };
  92.  
  93. class BishopPiece : public GamePiece
  94. {
  95. public:
  96.     BishopPiece(char PieceColor) : GamePiece(PieceColor) {}
  97.     ~BishopPiece() {}
  98. private:
  99.     virtual char GetPiece() {
  100.         return 'B';
  101.     }
  102.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  103.         if ((iDestCol - iSrcCol == iDestRow - iSrcRow) || (iDestCol - iSrcCol == iSrcRow - iDestRow)) {
  104.             // Make sure that all invervening squares are empty
  105.             int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  106.             int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  107.             int iCheckRow;
  108.             int iCheckCol;
  109.             for (iCheckRow = iSrcRow + iRowOffset, iCheckCol = iSrcCol + iColOffset;
  110.                 iCheckRow !=  iDestRow;
  111.                 iCheckRow = iCheckRow + iRowOffset, iCheckCol = iCheckCol + iColOffset)
  112.             {
  113.                 if (GameBoard[iCheckRow][iCheckCol] != 0) {
  114.                     return false;
  115.                 }
  116.             }
  117.             return true;
  118.         }
  119.         return false;
  120.     }
  121. };
  122.  
  123. class RookPiece : public GamePiece
  124. {
  125. public:
  126.     RookPiece(char PieceColor) : GamePiece(PieceColor) {}
  127.     ~RookPiece() {}
  128. private:
  129.     virtual char GetPiece() {
  130.         return 'R';
  131.     }
  132.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  133.         if (iSrcRow == iDestRow) {
  134.             // Make sure that all invervening squares are empty
  135.             int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  136.             for (int iCheckCol = iSrcCol + iColOffset; iCheckCol !=  iDestCol; iCheckCol = iCheckCol + iColOffset) {
  137.                 if (GameBoard[iSrcRow][iCheckCol] != 0) {
  138.                     return false;
  139.                 }
  140.             }
  141.             return true;
  142.         } else if (iDestCol == iSrcCol) {
  143.             // Make sure that all invervening squares are empty
  144.             int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  145.             for (int iCheckRow = iSrcRow + iRowOffset; iCheckRow !=  iDestRow; iCheckRow = iCheckRow + iRowOffset) {
  146.                 if (GameBoard[iCheckRow][iSrcCol] != 0) {
  147.                     return false;
  148.                 }
  149.             }
  150.             return true;
  151.         }
  152.         return false;
  153.     }
  154. };
  155.  
  156. class QueenPiece : public GamePiece
  157. {
  158. public:
  159.     QueenPiece(char PieceColor) : GamePiece(PieceColor) {}
  160.     ~QueenPiece() {}
  161. private:
  162.     virtual char GetPiece() {
  163.         return 'Q';
  164.     }
  165.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  166.         if (iSrcRow == iDestRow) {
  167.             // Make sure that all invervening squares are empty
  168.             int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  169.             for (int iCheckCol = iSrcCol + iColOffset; iCheckCol !=  iDestCol; iCheckCol = iCheckCol + iColOffset) {
  170.                 if (GameBoard[iSrcRow][iCheckCol] != 0) {
  171.                     return false;
  172.                 }
  173.             }
  174.             return true;
  175.         } else if (iDestCol == iSrcCol) {
  176.             // Make sure that all invervening squares are empty
  177.             int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  178.             for (int iCheckRow = iSrcRow + iRowOffset; iCheckRow !=  iDestRow; iCheckRow = iCheckRow + iRowOffset) {
  179.                 if (GameBoard[iCheckRow][iSrcCol] != 0) {
  180.                     return false;
  181.                 }
  182.             }
  183.             return true;
  184.         } else if ((iDestCol - iSrcCol == iDestRow - iSrcRow) || (iDestCol - iSrcCol == iSrcRow - iDestRow)) {
  185.             // Make sure that all invervening squares are empty
  186.             int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  187.             int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  188.             int iCheckRow;
  189.             int iCheckCol;
  190.             for (iCheckRow = iSrcRow + iRowOffset, iCheckCol = iSrcCol + iColOffset;
  191.                 iCheckRow !=  iDestRow;
  192.                 iCheckRow = iCheckRow + iRowOffset, iCheckCol = iCheckCol + iColOffset)
  193.             {
  194.                 if (GameBoard[iCheckRow][iCheckCol] != 0) {
  195.                     return false;
  196.                 }
  197.             }
  198.             return true;
  199.         }
  200.         return false;
  201.     }
  202. };
  203.  
  204. class KingPiece : public GamePiece
  205. {
  206. public:
  207.     KingPiece(char PieceColor) : GamePiece(PieceColor) {}
  208.     ~KingPiece() {}
  209. private:
  210.     virtual char GetPiece() {
  211.         return 'K';
  212.     }
  213.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, GamePiece* GameBoard[8][8]) {
  214.         int iRowDelta = iDestRow - iSrcRow;
  215.         int iColDelta = iDestCol - iSrcCol;
  216.         if (((iRowDelta >= -1) && (iRowDelta <= 1)) &&
  217.             ((iColDelta >= -1) && (iColDelta <= 1)))
  218.         {
  219.             return true;
  220.         }
  221.         return false;
  222.     }
  223. };
  224.  
  225. class CBoard
  226. {
  227. public:
  228.  
  229. GamePiece* MainGameBoard[8][8];
  230.     CBoard() {
  231.         for (int iRow = 0; iRow < 8; ++iRow) {
  232.             for (int iCol = 0; iCol < 8; ++iCol) {
  233.                 MainGameBoard[iRow][iCol] = 0;
  234.             }
  235.         }
  236.         // Allocate and place black pieces
  237.         for (int iCol = 0; iCol < 8; ++iCol) {
  238.             MainGameBoard[6][iCol] = new PawnPiece('B');
  239.         }
  240.         MainGameBoard[7][0] = new RookPiece('B');
  241.         MainGameBoard[7][1] = new KnightPiece('B');
  242.         MainGameBoard[7][2] = new BishopPiece('B');
  243.         MainGameBoard[7][3] = new KingPiece('B');
  244.         MainGameBoard[7][4] = new QueenPiece('B');
  245.         MainGameBoard[7][5] = new BishopPiece('B');
  246.         MainGameBoard[7][6] = new KnightPiece('B');
  247.         MainGameBoard[7][7] = new RookPiece('B');
  248.         // Allocate and place white pieces
  249.         for (int iCol = 0; iCol < 8; ++iCol) {
  250.             MainGameBoard[1][iCol] = new PawnPiece('W');
  251.         }
  252.         MainGameBoard[0][0] = new RookPiece('W');
  253.         MainGameBoard[0][1] = new KnightPiece('W');
  254.         MainGameBoard[0][2] = new BishopPiece('W');
  255.         MainGameBoard[0][3] = new KingPiece('W');
  256.         MainGameBoard[0][4] = new QueenPiece('W');
  257.         MainGameBoard[0][5] = new BishopPiece('W');
  258.         MainGameBoard[0][6] = new KnightPiece('W');
  259.         MainGameBoard[0][7] = new RookPiece('W');
  260.     }
  261.     ~CBoard() {
  262.         for (int iRow = 0; iRow < 8; ++iRow) {
  263.             for (int iCol = 0; iCol < 8; ++iCol) {
  264.                 delete MainGameBoard[iRow][iCol];
  265.                 MainGameBoard[iRow][iCol] = 0;
  266.             }
  267.         }
  268.     }
  269.  
  270.     void Print() {
  271.         using namespace std;
  272.         const int kiSquareWidth = 4;
  273.         const int kiSquareHeight = 3;
  274.         for (int iRow = 0; iRow < 8*kiSquareHeight; ++iRow) {
  275.             int iSquareRow = iRow/kiSquareHeight;
  276.             // Print side border with numbering
  277.             if (iRow % 3 == 1) {
  278.                 cout << '-' << (char)('1' + 7 - iSquareRow) << '-';
  279.             } else {
  280.                 cout << "---";
  281.             }
  282.             // Print the chess board
  283.             for (int iCol = 0; iCol < 8*kiSquareWidth; ++iCol) {
  284.                 int iSquareCol = iCol/kiSquareWidth;
  285.                 if (((iRow % 3) == 1) && ((iCol % 4) == 1 || (iCol % 4) == 2) && MainGameBoard[7-iSquareRow][iSquareCol] != 0) {
  286.                     if ((iCol % 4) == 1) {
  287.                         cout << MainGameBoard[7-iSquareRow][iSquareCol]->GetColor();
  288.                     } else {
  289.                         cout << MainGameBoard[7-iSquareRow][iSquareCol]->GetPiece();
  290.                     }
  291.                 } else {
  292.                     if ((iSquareRow + iSquareCol) % 2 == 1) {
  293.                         cout << '*';
  294.                     } else {
  295.                         cout << ' ';
  296.                     }
  297.                 }
  298.             }
  299.             cout << endl;
  300.         }
  301.         // Print the bottom border with numbers
  302.         for (int iRow = 0; iRow < kiSquareHeight; ++iRow) {
  303.             if (iRow % 3 == 1) {
  304.                 cout << "---";
  305.                 for (int iCol = 0; iCol < 8*kiSquareWidth; ++iCol) {
  306.                     int iSquareCol = iCol/kiSquareWidth;
  307.                     if ((iCol % 4) == 1) {
  308.                         cout << (iSquareCol + 1);
  309.                     } else {
  310.                         cout << '-';
  311.                     }
  312.                 }
  313.                 cout << endl;
  314.             } else {
  315.                 for (int iCol = 1; iCol < 9*kiSquareWidth; ++iCol) {
  316.                     cout << '-';
  317.                 }
  318.                 cout << endl;
  319.             }
  320.         }
  321.     }
  322.  
  323.     bool IsInCheck(char PieceColor) {
  324.         // Find the king
  325.         int iKingRow;
  326.         int iKingCol;
  327.         for (int iRow = 0; iRow < 8; ++iRow) {
  328.             for (int iCol = 0; iCol < 8; ++iCol) {
  329.                 if (MainGameBoard[iRow][iCol] != 0) {
  330.                     if (MainGameBoard[iRow][iCol]->GetColor() == PieceColor) {
  331.                         if (MainGameBoard[iRow][iCol]->GetPiece() == 'K') {
  332.                             iKingRow = iRow;
  333.                             iKingCol = iCol;
  334.                         }
  335.                     }
  336.                 }
  337.             }
  338.         }
  339.         // Run through the opponent's pieces and see if any can take the king
  340.         for (int iRow = 0; iRow < 8; ++iRow) {
  341.             for (int iCol = 0; iCol < 8; ++iCol) {
  342.                 if (MainGameBoard[iRow][iCol] != 0) {
  343.                     if (MainGameBoard[iRow][iCol]->GetColor() != PieceColor) {
  344.                         if (MainGameBoard[iRow][iCol]->IsLegalMove(iRow, iCol, iKingRow, iKingCol, MainGameBoard)) {
  345.                             return true;
  346.                         }
  347.                     }
  348.                 }
  349.             }
  350.         }
  351.  
  352.         return false;
  353.     }
  354.  
  355.  
  356.     struct move
  357.     {
  358.         /* data */
  359.         int srcrow, srccol, destrow, destcol,score;
  360.     };
  361.    
  362.  
  363.     bool CanMove(char PieceColor) {
  364.         // Run through all pieces
  365.         for (int iRow = 0; iRow < 8; ++iRow) {
  366.             for (int iCol = 0; iCol < 8; ++iCol) {
  367.                 if (MainGameBoard[iRow][iCol] != 0) {
  368.                     // If it is a piece of the current player, see if it has a legal move
  369.                     if (MainGameBoard[iRow][iCol]->GetColor() == PieceColor) {
  370.                         for (int iMoveRow = 0; iMoveRow < 8; ++iMoveRow) {
  371.                             for (int iMoveCol = 0; iMoveCol < 8; ++iMoveCol) {
  372.                                 if (MainGameBoard[iRow][iCol]->IsLegalMove(iRow, iCol, iMoveRow, iMoveCol, MainGameBoard)) {
  373.                                     // Make move and check whether king is in check
  374.                                     GamePiece* qpTemp                   = MainGameBoard[iMoveRow][iMoveCol];
  375.                                     MainGameBoard[iMoveRow][iMoveCol]   = MainGameBoard[iRow][iCol];
  376.                                     MainGameBoard[iRow][iCol]           = 0;
  377.                                     bool bCanMove = !IsInCheck(PieceColor);
  378.                                     // Undo the move
  379.                                     MainGameBoard[iRow][iCol]           = MainGameBoard[iMoveRow][iMoveCol];
  380.                                     MainGameBoard[iMoveRow][iMoveCol]   = qpTemp;
  381.                                     if (bCanMove) {
  382.                                         return true;
  383.                                     }
  384.                                 }
  385.                             }
  386.                         }
  387.                     }
  388.                 }
  389.             }
  390.         }
  391.         return false;
  392.     }
  393.    
  394.  
  395.     std::vector<move> possibleMoves(char pieceColor)
  396.     {   std::vector<move> output;
  397.         move toAdd;
  398.         for (int iRow = 0; iRow < 8; ++iRow) {
  399.             for (int iCol = 0; iCol < 8; ++iCol) {
  400.                 if (MainGameBoard[iRow][iCol] != 0) {
  401.                     // If it is a piece of the current player, see if it has a legal move
  402.                     if (MainGameBoard[iRow][iCol]->GetColor() == pieceColor) {
  403.                         for (int iMoveRow = 0; iMoveRow < 8; ++iMoveRow) {
  404.                             for (int iMoveCol = 0; iMoveCol < 8; ++iMoveCol) {
  405.                                 if (MainGameBoard[iRow][iCol]->IsLegalMove(iRow, iCol, iMoveRow, iMoveCol, MainGameBoard)) {
  406.                                     // Make move and check whether king is in check
  407.                                     GamePiece* qpTemp                   = MainGameBoard[iMoveRow][iMoveCol];
  408.                                     MainGameBoard[iMoveRow][iMoveCol]   = MainGameBoard[iRow][iCol];
  409.                                     MainGameBoard[iRow][iCol]           = 0;
  410.                                     bool bCanMove = !IsInCheck(pieceColor);
  411.                                     // Undo the move
  412.                                     MainGameBoard[iRow][iCol]           = MainGameBoard[iMoveRow][iMoveCol];
  413.                                     MainGameBoard[iMoveRow][iMoveCol]   = qpTemp;
  414.                                     if (bCanMove) {
  415.                                        toAdd.srcrow=iRow;
  416.                                        toAdd.srccol=iCol;
  417.                                        toAdd.destrow=iMoveRow;
  418.                                        toAdd.destcol=iMoveCol;
  419.                                        output.push_back(toAdd);
  420.  
  421.                                     }
  422.                                 }
  423.                             }
  424.                         }
  425.                     }
  426.                 }
  427.             }
  428.         }
  429.     return output;
  430.     }
  431.  
  432.     int score()
  433.     {
  434.         int whiteScore=0, blackScore=0, added=0;
  435.         for (int iRow = 0; iRow < 8; ++iRow) {
  436.             for (int iCol = 0; iCol < 8; ++iCol) {
  437.                 if (MainGameBoard[iRow][iCol] != 0) {
  438.                    
  439.                 if(MainGameBoard[iRow][iCol]->GetPiece()=='P')
  440.                      added=1;
  441.                 else if(MainGameBoard[iRow][iCol]->GetPiece()=='B')
  442.                      added=3;
  443.                 else if(MainGameBoard[iRow][iCol]->GetPiece()=='R')
  444.                      added=5;
  445.                 else if(MainGameBoard[iRow][iCol]->GetPiece()=='N')
  446.                      added=3;
  447.                 else if(MainGameBoard[iRow][iCol]->GetPiece()=='K')
  448.                      added=0;
  449.                 else if(MainGameBoard[iRow][iCol]->GetPiece()=='Q')
  450.                      added=9;
  451.  
  452.                 if (MainGameBoard[iRow][iCol]->GetColor() == 'W')
  453.                     whiteScore+=added;
  454.                 else
  455.                     blackScore+=added;
  456.                 }
  457.             }
  458.         }
  459.  
  460.         return whiteScore-blackScore;
  461.     }
  462.     void makemove(move to)
  463.     {
  464.        
  465.         MainGameBoard[to.destrow][to.destcol]     = MainGameBoard[to.srcrow][to.srccol];
  466.         MainGameBoard[to.srcrow][to.srccol] = 0;
  467.     }
  468.  
  469.      move minimax(int depth, bool minimize){
  470.         move best_move;
  471.         best_move.score = -1000000 + 2000000*minimize;
  472.         if(0 == depth){
  473.             best_move.score = score();
  474.             return best_move;
  475.         }
  476.  
  477.  
  478.  
  479.  
  480.             for(auto & to : possibleMoves('B')){
  481.                 CBoard branch = *this;
  482.                 branch.makemove(to);
  483.                 move option = branch.minimax(depth-1, !minimize);
  484.                 if((option.score > best_move.score && !minimize) || (option.score < best_move.score && minimize)){
  485.                     best_move.score = option.score;
  486.                     best_move.srccol = to.srccol;
  487.                     best_move.destcol = to.destcol;
  488.                     best_move.srcrow = to.srcrow;
  489.                     best_move.destrow = to.destrow;
  490.                    
  491.                 }
  492.             }
  493.        
  494.         return best_move;
  495.     }
  496.  
  497.     void AIMove(){
  498.         std::cout<<"yaha tak aagaya";
  499.        
  500.         bool minimize = true; //true for black, false for white
  501.         move m = minimax(4,minimize);
  502.         makemove(m);
  503.         Print();
  504.        
  505.     }
  506.  
  507.  
  508.    
  509. };
  510.  
  511. class ChessBoard
  512. {
  513. public:
  514.     ChessBoard() : mcPlayerTurn('W') {}
  515.     ~ChessBoard() {}
  516.  
  517.     void Start() {
  518.         while (!IsGameOver())
  519.         {
  520.             if(mcPlayerTurn=='B')
  521.             {
  522.                 std::cout<<"gyugyugyugu";
  523.                 mqGameBoard.AIMove();
  524.             }
  525.             else
  526.             GetNextMove(mqGameBoard.MainGameBoard);
  527.             AlternateTurn();
  528.             std::cout<<"Isgameover"<<IsGameOver()<<std::endl;
  529.         }
  530.         mqGameBoard.Print();
  531.     }
  532.  
  533.     void GetNextMove(GamePiece* GameBoard[8][8]) {
  534.         using namespace std;
  535.      
  536.            
  537.         bool bValidMove     = false;
  538.         do {
  539.            
  540.          
  541.            
  542.             cout<<"Rule for move is :"<<endl;
  543.             cout<<"Move by selecting row & column to another valid location using row & column"<<endl<<endl<<endl;
  544.             mqGameBoard.Print();
  545.  
  546.             // Get input and convert to coordinates
  547.             cout << mcPlayerTurn << "'s Move: ";
  548.             int iStartMove;
  549.             cin >> iStartMove;
  550.             int iStartRow = (iStartMove / 10) - 1;
  551.             int iStartCol = (iStartMove % 10) - 1;
  552.  
  553.             cout << "To: ";
  554.             int iEndMove;
  555.             cin >> iEndMove;
  556.             int iEndRow = (iEndMove / 10) - 1;
  557.             int iEndCol = (iEndMove % 10) - 1;
  558.  
  559.             // Check that the indices are in range
  560.             // and that the source and destination are different
  561.             if ((iStartRow >= 0 && iStartRow <= 7) &&
  562.                 (iStartCol >= 0 && iStartCol <= 7) &&
  563.                 (iEndRow >= 0 && iEndRow <= 7) &&
  564.                 (iEndCol >= 0 && iEndCol <= 7)) {
  565.                 // Additional checks in here
  566.                 GamePiece* qpCurrPiece = GameBoard[iStartRow][iStartCol];
  567.                 // Check that the piece is the correct color
  568.                 if ((qpCurrPiece != 0) && (qpCurrPiece->GetColor() == mcPlayerTurn)) {
  569.                     // Check that the destination is a valid destination
  570.                     if (qpCurrPiece->IsLegalMove(iStartRow, iStartCol, iEndRow, iEndCol, GameBoard)) {
  571.                         // Make the move
  572.                         GamePiece* qpTemp                   = GameBoard[iEndRow][iEndCol];
  573.                         GameBoard[iEndRow][iEndCol]     = GameBoard[iStartRow][iStartCol];
  574.                         GameBoard[iStartRow][iStartCol] = 0;
  575.                         // Make sure that the current player is not in check
  576.                         if (!mqGameBoard.IsInCheck(mcPlayerTurn)) {
  577.                             delete qpTemp;
  578.                             bValidMove = true;
  579.                         } else { // Undo the last move
  580.                             GameBoard[iStartRow][iStartCol] = GameBoard[iEndRow][iEndCol];
  581.                             GameBoard[iEndRow][iEndCol]     = qpTemp;
  582.                         }
  583.                     }
  584.                 }
  585.             }
  586.             if (!bValidMove) {
  587.                 cout << "Invalid Move!" << endl;
  588.             }
  589.         } while (!bValidMove);
  590.  
  591.     cout<<"The score is" << mqGameBoard.score();
  592.        
  593.      
  594.     }
  595.  
  596.     void AlternateTurn() {
  597.         mcPlayerTurn = (mcPlayerTurn == 'W') ? 'B' : 'W';
  598.     }
  599.  
  600.     bool IsGameOver() {
  601.         // Check that the current player can move
  602.         // If not, we have a stalemate or checkmate
  603.         bool bCanMove(false);
  604.         bCanMove = mqGameBoard.CanMove(mcPlayerTurn);
  605.         if (!bCanMove) {
  606.             if (mqGameBoard.IsInCheck(mcPlayerTurn)) {
  607.                 AlternateTurn();
  608.                 std::cout << "Checkmate, " << mcPlayerTurn << " Wins!" << std::endl;
  609.             } else {
  610.                 std::cout << "Stalemate!" << std::endl;
  611.             }
  612.         }
  613.         return !bCanMove;
  614.     }
  615. private:
  616.     CBoard mqGameBoard;
  617.     char mcPlayerTurn;
  618. };
  619.  
  620. int main() {
  621.     ChessBoard qGame;
  622.     qGame.Start();
  623.     return 0;
  624. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement