Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class CAPiece {
  4. public:
  5.     CAPiece(char cColor) : mcColor(cColor) {}
  6.     ~CAPiece() {}
  7.     virtual char GetPiece() = 0;
  8.     char GetColor() {
  9.         return mcColor;
  10.     }
  11.     bool Islegalmove(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) {
  12.         CAPiece* qpDest = qpaaBoard[iDestRow][iDestCol];
  13.         if ((qpDest == 0) || (mcColor != qpDest->GetColor())) {
  14.             return AreSquaresLegal(iSrcRow, iSrcCol, iDestRow, iDestCol, qpaaBoard);
  15.         }
  16.         return false;
  17.     }
  18. private:
  19.     virtual bool AreSquaresLegal(int SrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) = 0;
  20.     char mcColor;
  21. };
  22.  
  23. class CPawn : public CAPiece
  24. {
  25. public:
  26.     CPawn(char cColor) : CAPiece(cColor) {}
  27.     ~CPawn() {}
  28. private:
  29.     virtual char GetPiece() {
  30.         return 'p';
  31.     }
  32.     bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) {
  33.         CAPiece* qpDest = qpaaBoard[iDestRow][iDestCol];
  34.         if (qpDest == 0) {
  35.             // Destination square is unoccupied
  36.             if (iSrcCol == iDestCol) {
  37.                 if (GetColor() == 'W') {
  38.                     if (iDestRow == iSrcRow + 1) {
  39.                         return true;
  40.                     }
  41.                 }
  42.                 else {
  43.                     if (iDestRow == iSrcRow - 1) {
  44.                         return true;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         else {
  50.             // Dest holds peiece of opposite color
  51.             if ((iSrcCol == iDestCol + 1) || (iSrcCol == iDestCol - 1)) {
  52.                 if (GetColor() == 'W') {
  53.                     if (iDestRow == iSrcRow + 1) {
  54.                         return true;
  55.                     }
  56.                 }
  57.                 else {
  58.                     if (iDestRow == iSrcRow - 1) {
  59.                         return true;
  60.                     }
  61.                 }
  62.             }
  63.          }
  64.             return false;
  65.         }
  66.     };
  67.     class CKnight : public CAPiece
  68.     {
  69.     public:
  70.         CKnight(char cColor) : CAPiece(cColor) {}
  71.         ~CKnight() {}
  72.     private:
  73.         virtual char GetPiece() {
  74.             return 'N';
  75.         }
  76.         bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) {
  77.             // Destination square is unoccupied or occupied by opposite color
  78.             if ((iSrcCol == iDestCol + 1) || (iSrcCol == iDestCol - 1)) {
  79.                 if ((iSrcRow == iDestRow + 2) || (iSrcCol == iDestCol - 2)) {
  80.                     return true;
  81.                 }
  82.             }
  83.             if ((iSrcCol == iDestCol + 2) || (iSrcCol == iDestCol - 2)) {
  84.                 if ((iSrcRow == iDestRow + 1) || (iSrcRow == iDestRow - 1)) {
  85.                     return true;
  86.                 }
  87.             }
  88.             return false;
  89.         }
  90.     };
  91.  
  92.     class CBishop : public CAPiece
  93.     {
  94.     public:
  95.         CBishop(char cColor) : CAPiece(cColor) {}
  96.         ~CBishop() {}
  97.     private:
  98.         virtual char GetPiece() {
  99.             return 'B';
  100.         }
  101.         bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) {
  102.             if ((iDestCol - iSrcCol == iDestRow - iSrcRow) || (iDestCol - iSrcCol == iSrcRow - iDestRow)) {
  103.             // Make sure all inervening squares are empty
  104.             int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  105.             int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  106.             int iCheckRow;
  107.             int iCheckCol;
  108.             for (iCheckRow = iSrcRow + iRowOffset, iCheckCol = iSrcCol + iColOffset;
  109.                 iCheckRow != iDestRow;
  110.                 iCheckRow = iCheckRow + iRowOffset, iCheckCol = iCheckCol + iColOffset)
  111.             {
  112.                 if (qpaaBoard[iCheckRow][iCheckCol] != 0) {
  113.                     return false;
  114.                 }
  115.             }
  116.             return true;
  117.         }
  118.         return false;
  119.     }
  120. };
  121.  
  122.     class CRook : public CAPiece
  123.     {
  124.     public:
  125.         CRook(char cColor) : CAPiece(cColor) {}
  126.         ~CRook() {}
  127.     private:
  128.         virtual char GetPiece() {
  129.             return 'R';
  130.         }
  131.         bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[8][8]) {
  132.             if (iSrcRow == iDestRow) {
  133.                 // Make sure that all inervening squares are empty
  134.                 int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  135.                 for (int iCheckCol = iSrcCol + iColOffset; iCheckCol != iDestCol; iCheckCol = iCheckCol + iColOffset) {
  136.                     if (qpaaBoard[iSrcRow][iCheckCol] != 0) {
  137.                         return false;
  138.                     }
  139.                 }
  140.                 return true;
  141.             }
  142.             else if (iDestCol == iSrcCol) {
  143.                 // Make sure that all inervening squares are empty
  144.                 int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  145.                 for (int iCheckRow = iSrcRow + iRowOffset; iCheckRow != iDestRow; iCheckRow = iCheckRow + iRowOffset) {
  146.                     if (qpaaBoard[iCheckRow][iSrcCol] != 0) {
  147.                         return false;
  148.                     }
  149.                 }
  150.                 return true;
  151.             }
  152.             return false;
  153.         }
  154.     };
  155.  
  156.     class CQueen : public CAPiece
  157.     {
  158.     public:
  159.         CQueen(char cColor) : CAPiece(cColor) {}
  160.         ~CQueen() {}
  161.     private:
  162.         virtual char GetPiece() {
  163.             return 'Q';
  164.         }
  165.         bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestCol, CAPiece* qpaaBoard[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 (qpaaBoard[iSrcRow][iCheckCol] != 0) {
  171.                         return false;
  172.                     }
  173.                 }
  174.                 return true;
  175.             }
  176.             else if (iDestCol == iSrcCol) {
  177.                 // Make sure that all invervening squares are empty
  178.                 int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  179.                 for (int iCheckRow = iSrcRow + iRowOffset; iCheckRow != iDestRow; iCheckRow = iCheckRow + iRowOffset) {
  180.                     if (qpaaBoard[iCheckRow][iSrcCol] != 0) {
  181.                         return false;
  182.                     }
  183.                 }
  184.                 return true;
  185.             }
  186.             else if ((iDestCol - iSrcCol == iDestRow - iSrcRow) || (iDestCol - iSrcCol == iSrcRow - iDestRow)) {
  187.                 // Make sure that all inverevening squares are empty
  188.                 int iRowOffset = (iDestRow - iSrcRow > 0) ? 1 : -1;
  189.                 int iColOffset = (iDestCol - iSrcCol > 0) ? 1 : -1;
  190.                 int iCheckRow;
  191.                 int iCheckCol;
  192.                 for (iCheckRow = iSrcRow + iRowOffset, iCheckCol = iSrcCol + iColOffset;
  193.                     iCheckRow != iDestRow;
  194.                     iCheckRow = iCheckRow + iRowOffset, iCheckCol = iCheckCol + iColOffset)
  195.                 {
  196.                     if (qpaaBoard[iCheckRow][iCheckCol] != 0) {
  197.                         return false;
  198.                     }
  199.                 }
  200.                 return true;
  201.             }
  202.             return false;
  203.         }
  204.     };
  205.  
  206.     class CKing : public CAPiece
  207.     {
  208.         CKing(char cColor) : CAPiece(cColor) {}
  209.         ~CKing() {}
  210.     private:
  211.         virtual char GetPiece() {
  212.             return 'K';
  213.             }
  214.         bool AreSquaresLegal(int iSrcRow, int iSrcCol, int iDestRow, int iDestcol, CAPiece* qpaaBoard[8][8]) {
  215.             int iRowDelta = iDestRow - iSrcRow;
  216.             int iColDelta = iDestcol - iSrcCol;
  217.             if (((iRowDelta >= -1) && (iColDelta <= 1)) &&
  218.                 ((iColDelta >= -1) && (iColDelta <= 1)))
  219.             {
  220.             return true;
  221.             }
  222.             return false;
  223.         }
  224. };
  225.  
  226.     class CBoard
  227.     {
  228.     public:
  229.         CBoard() {
  230.             for (int iRow = 0; iRow < 8; ++iRow) {
  231.                 for (int iCol = 0; iCol < 8; ++iCol) {
  232.                     mqpaaBoard[iRow][iCol] = 0;
  233.                 }
  234.             }
  235.             // Allocate and place back pieces
  236.             for (int iCol = 0; iCol < 8; ++iCol) {
  237.                 mqpaaBoard[6][iCol] = new CPawn('B');
  238.             }
  239.             mqpaaBoard[7][0] = new CRook('B');
  240.             mqpaaBoard[7][1] = new CKnight('B');
  241.             mqpaaBoard[7][2] = new CBishop('B');
  242.             mqpaaBoard[7][3] = new CKing('B');
  243.             mqpaaBoard[7][4] = new CQueen('B');
  244.             mqpaaBoard[7][5] = new CBishop('B');
  245.             mqpaaBoard[7][6] = new CKnight('B');
  246.             mqpaaBoard[7][7] = new CRook('B');
  247.             // Allocate and place white pieces
  248.             for (int iCol = 0; iCol < 8; ++iCol) {
  249.                 mqpaaBoard[1][iCol] = new CPawn('W');
  250.             }
  251.             mqpaaBoard[0][0] = new CRook('W');
  252.             mqpaaBoard[0][1] = new CKnight('W');
  253.             mqpaaBoard[0][2] = new CBishop('W');
  254.             mqpaaBoard[0][3] = new CKing('W');
  255.             mqpaaBoard[0][4] = new CQueen('W');
  256.             mqpaaBoard[0][5] = new CBishop('W');
  257.             mqpaaBoard[0][6] = new CKnight('W');
  258.             mqpaaBoard[0][7] = new CRook('W');
  259.         }
  260.         ~CBoard() {
  261.             for (int iRow = 0; iRow < 8; ++iRow) {
  262.                 for (int iCol = 0; iCol < 8; ++iCol) {
  263.                     delete mqpaaBoard[iRow][iCol];
  264.                     mqpaaBoard[iRow][iCol] = 0;
  265.                 }
  266.             }
  267.         }
  268.  
  269.         void print() {
  270.             using namespace std;
  271.             const int kiSquareWidth = 4;
  272.             const int kiSquareHeight = 3;
  273.             for (int iRow = 0; iRow < 8 * kiSquareHeight; ++iRow) {
  274.                 // Print side border with numbering
  275.                 if (iRow % 3 == 1) {
  276.                     cout << '_' << (char)('1' + 7 - iSquareRow) << '-';
  277.                 }
  278.                 else {
  279.                     cout << "---";
  280.                 }
  281.                 // Print the chess board
  282.                 for (int iCol = 0; iCol < 8 * kiSquareWidth; ++iCol) {
  283.                     int iSquareCol = iCol / kiSquareWidth;
  284.                     if ((iCol % 4) == 1) {
  285.                         cout << mqpaaBoard[7 - iSquareRow][iSquareCol]->GetColor();
  286.                     }
  287.                     else {
  288.                         if ((iSquareRow + iSquareCol) % 2 == 1) {
  289.                             cout << '*';
  290.                         }
  291.                         else {
  292.                             cout << ' ';
  293.                         }
  294.                     }
  295.                 }
  296.                 cout << endl;
  297.             }
  298.             // Print the bottom border with numbers
  299.             for (int iRow = 0; iRow < kiSquareHeight; ++iRow) {
  300.                 if (iRow % 3 == 1) {
  301.                     cout << "---";
  302.                     for (int iCol = 0; iCol < 8 * kiSquareWidth; ++iCol) {
  303.                         int iSquareCol = iCol / kiSquareWidth;
  304.                         if ((iCol % 4) == 1) {
  305.                             cout << (iSquareCol + 1);
  306.                         }
  307.                         else {
  308.                             cout << '-';
  309.                         }
  310.                     }
  311.                     cout << endl;
  312.                 }
  313.                 else {
  314.                     for (int iCol = 1; iCol < 9 * kiSquareWidth; ++iCol) {
  315.                         cout << '-';
  316.                     }
  317.                     cout << endl;
  318.                 }
  319.             }
  320.         }
  321.  
  322.         bool IsInCheck(char cColor) {
  323.             // Find the king
  324.             int iKingRow;
  325.             int iKingCol;
  326.             for (int iRow = 0; iRow < 8; ++iRow) {
  327.                 for (int iCol = 0; iRow < 8; ++iCol) {
  328.                     if (mqpaaBoard[iRow][iCol] != 0) {
  329.                         if (mqpaaBoard[iRow][iCol]->GetColor() == cColor) {
  330.                             if (mqpaaBoard[iRow][iCol]->GetPiece() == 'K') {
  331.                                 iKingRow = iRow;
  332.                             }
  333.                         }
  334.                     }
  335.                 }
  336.             }
  337.             // Run through the opponent's pieces and see if any can take the king
  338.             for (int iRow = 0; iRow < 8; ++iRow) {
  339.                 for (int iCol = 0; iRow < 8; ++iCol) {
  340.                     if (mqpaaBoard[iRow][iCol] != 0) {
  341.                         if (mqpaaBoard[iRow][iCol]->IsLegalMove(iRow, iCol, iKingRow, iKingCol, mqpaaBoard)) {
  342.                             return true;
  343.                         }
  344.                     }
  345.                 }
  346.             }
  347.         }
  348.     }
  349.     return false;
  350. }
  351.  
  352. bool CanMove(char cColor) {
  353.     // Run through all pieces
  354.     for (int iRow = 0; iRow < 8; ++iRow) {
  355.         for (int iCol = 0; iCol < 8; ++iCol) {
  356.             if (mqpaaBoard[iRow][iCol] != 0) {
  357.                 // If it is a piece of the current player, see if it has a legal move
  358.                 if (mqpaaBoard[iRow][iCol]->GetColor() == cColor) {
  359.                     for (int iMoveRow = 0; iMoveRow < 8; ++iMoveRow) {
  360.                         for (int iMoveCol = 0; iMoveCol < 8; ++iMoveCol) {
  361.                             if (mqpaaBoard[iRow][iCol]->IsLegalMove(iRow, iCol, iMoveRow, iMoveCol, mqpaaBoard)) {
  362.                                 // Make move and check wheter king is in check
  363.                                 CAPiece* qpTemp
  364.                                     mqpaaBoard[iMoveRow][iMoveCol] = mqpaaBoard[iRow][iCol];
  365.                                 mqpaaBoard[iRow][iCol] = 0;
  366.                                 bool bCanMove = !IsInCheck(cColor);
  367.                                 // Undo the move
  368.                                 mqpaaBoard[iRow][iCol] = mqpaaBoard[iMoveRow][iMoveCol];
  369.                                 mqpaaBoard[iMoveRow][iMoveCol] = qpTemp;
  370.                                 if (bCanMove) {
  371.                                     return true;
  372.                                 }
  373.                             }
  374.                         }
  375.                     }
  376.                 }
  377.             }
  378.         }
  379.         return false;
  380.     }
  381.  
  382.     CAPiece* mqpaaBoard[8][8];
  383. };
  384.  
  385. class CChess
  386. {
  387. public:
  388.     CChess() : mcPlayerTurn('W') {}
  389.     ~CChess() {}
  390.  
  391.     void start() {
  392.         do {
  393.             GetNextMove(mqGameBoard.mqpaaBoard);
  394.             AlternateTurn();
  395.         } while (!IsGameOver());
  396.         mqGameBoard.Print();
  397.     }
  398.  
  399. void GetNextMove(CAPiece* qpaaBoard[8][8]) {
  400.     using namespace std;
  401.     bool bValidMove = false;
  402.     do {
  403.         mqGameBoard.print();
  404.  
  405.         // Get input and convert to coordinates
  406.         cout << mcPlayerTurn << " 's Move: ";
  407.         int iStartMove;
  408.         cin >> iStartMove;
  409.         int iStartRow = (iStartMove / 10) - 1;
  410.         int iStartCol = (iStartMove % 10) - 1;
  411.  
  412.         cout << "To; ";
  413.         int iEndMove;
  414.         cin >> iEndMove;
  415.         int iEndRow = (iEndMove / 10) - 1;
  416.         int iEndCol = (iEndMove % 10) - 1;
  417.  
  418.         // Check that the idices are i range
  419.         // and that the source and destination are different
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement