Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- #include <stdlib.h>
- #include <vector>
- #include <string.h>
- #ifdef OS_WINDOWS
- #include <windows.h>
- #include <time.h>
- #define LET_ME_SLEEP Sleep(7000);
- #define LET_ME_CLEAR system("cls");
- #else
- #include <unistd.h>
- #define LET_ME_SLEEP sleep(2);
- #define LET_ME_CLEAR system("clear");
- #endif
- using namespace std;
- void StartScreen();
- void SetBoard ();
- void DispBoard ();
- bool Check(char c);
- int Game (char c);
- char SwitchTurn (char c);
- bool CanMove(char c);
- bool EndGame(char c);
- class Piece{
- protected:
- char color;
- public:
- Piece(char c):color(c){}
- virtual ~Piece() {}
- virtual char GetPiece() = 0;
- virtual bool IsLegalMove(int iRow, int iCol, int fRow, int fCol) = 0;
- char GetColor()
- {
- return color;
- }
- };
- Piece* ChessBoard[8][8];
- struct Contain {
- Piece* Board[8][8];
- };
- vector <Contain> MyVector;
- class Pawn : public Piece
- {
- public:
- Pawn(char c) : Piece(c) {}
- ~Pawn() {}
- char GetPiece()
- {
- return 'P';
- }
- bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
- {
- Piece* finalPos = ChessBoard[fRow][fCol];
- if ((finalPos == 0) || (color != finalPos -> GetColor()))
- {
- if (finalPos == 0)
- { // hayt7rak
- if (iCol == fCol)
- {
- if (GetColor() == 'W')
- {
- if ((fRow == iRow - 1) || ((fRow == 4) && (iRow == 6) && (ChessBoard[5][iCol] == 0)))
- {
- return true;
- }
- }
- else
- {
- if ((fRow == iRow + 1) || ((fRow == 3) && (iRow == 1) && (ChessBoard[2][iCol] == 0)))
- {
- return true;
- }
- }
- }
- }
- else
- {
- // hayakol
- if ((iCol == fCol + 1) || (iCol == fCol - 1))
- {
- if (GetColor() == 'W')
- {
- if (fRow == iRow - 1)
- {
- return true;
- }
- }
- else
- {
- if (fRow == iRow + 1)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- return false;
- }
- };
- class Rook : public Piece
- {
- public:
- Rook(char c) : Piece(c) {}
- ~Rook() {}
- char GetPiece()
- {
- return 'R';
- }
- bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
- {
- Piece* finalPos = ChessBoard[fRow][fCol];
- if ((finalPos == 0) || (color != finalPos -> GetColor()))
- {
- if (((abs(fRow - iRow) >0) && (abs (fCol - iCol) == 0)) || ((abs(fRow - iRow) == 0) && (abs (fCol - iCol) > 0)) )
- {
- if (iRow == fRow) // 3ard
- {
- int Check_Number = abs (fCol-iCol) - 1; // kam check hane3mlo
- if (fCol>iCol) // ymeen
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[iRow][++iCol] != 0)
- return false;
- }
- }
- else if (fCol<iCol) // shmal
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[iRow][--iCol] != 0)
- return false;
- }
- }
- }
- else if (iCol == fCol) // tool
- {
- int Check_Number = abs (fRow - iRow) - 1; // kam check hane3mlo
- //cout << "kam marra: " << Check_Number << endl;
- if (fRow>iRow) // ta7t
- {
- //cout << "ta7t" << endl;
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[++iRow][iCol] != 0)
- return false;
- }
- }
- else if (fRow<iRow) // foo2
- {
- //cout << "foo2" << endl;
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[--iRow][iCol] != 0)
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- return false;
- }
- };
- class Knight : public Piece
- {
- public:
- Knight(char c) : Piece(c) {}
- ~Knight() {}
- char GetPiece()
- {
- return 'N';
- }
- bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
- {
- Piece* finalPos = ChessBoard[fRow][fCol];
- if ((finalPos == 0) || (color != finalPos -> GetColor()))
- {
- // 5atwa bel tool w 5atweten bel ganb
- if (abs(fCol - iCol) == 2)
- {
- if (abs(fRow - iRow) == 1)
- return true;
- }
- // 5atwa bel ganb w 5atweten bel tool
- if (abs(fCol - iCol) == 1)
- {
- if (abs(fRow - iRow) == 2)
- return true;
- }
- return false;
- }
- return false;
- }
- };
- class Bishop : public Piece
- {
- public:
- Bishop(char c) : Piece(c) {}
- ~Bishop() {}
- char GetPiece()
- {
- return 'B';
- }
- bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
- {
- Piece* finalPos = ChessBoard[fRow][fCol];
- if ((finalPos == 0) || (color != finalPos -> GetColor()))
- {
- if (abs(fRow - iRow) == abs(fCol - iCol) && (abs (fRow - iRow) > 0)) // net2aked eno et7rak aslan w law et7rak net2aked eno diagonally
- {
- int Check_Number = abs (fRow-iRow) - 1;// aw fcol-icol mesh far2a // kam check hane3mlo
- //cout << "kam marra: " << Check_Number << endl;
- if (fRow < iRow) // et7rak foo2
- {
- if (fCol>iCol) // et7rak foo2 ymeen
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[--iRow][++iCol] != 0)
- return false;
- }
- }
- else if (fCol<iCol) // foo2 shmal
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[--iRow][--iCol] != 0)
- return false;
- }
- }
- }
- else if (fRow>iRow) // et7rak ta7t
- {
- if (fCol>iCol) // ta7t ymeen
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[++iRow][++iCol] != 0)
- return false;
- }
- }
- else if (fCol<iCol) // t7t shmal
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[++iRow][--iCol] != 0)
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- return false;
- }
- };
- class Queen : public Piece
- {
- public:
- Queen(char c) : Piece(c) {}
- ~Queen() {}
- char GetPiece()
- {
- return 'Q';
- }
- bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
- {
- Piece* finalPos = ChessBoard[fRow][fCol];
- if ((finalPos == 0) || (color != finalPos -> GetColor()))
- {
- if (((abs(fRow - iRow) >0) && (abs (fCol - iCol) == 0)) || ((abs(fRow - iRow) == 0) && (abs (fCol - iCol) > 0)) )
- {
- if (iRow == fRow) // 3ard
- {
- int Check_Number = abs (fCol-iCol) - 1; // kam check hane3mlo
- if (fCol>iCol) // ymeen
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[iRow][++iCol] != 0)
- return false;
- }
- }
- else if (fCol<iCol) // shmal
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[iRow][--iCol] != 0)
- return false;
- }
- }
- }
- else if (iCol == fCol) // tool
- {
- int Check_Number = abs (fRow - iRow) - 1; // kam check hane3mlo
- // cout << "kam marra: " << Check_Number << endl;
- if (fRow>iRow) // ta7t
- {
- //cout << "ta7t" << endl;
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[++iRow][iCol] != 0)
- return false;
- }
- }
- else if (fRow<iRow) // foo2
- {
- // cout << "foo2" << endl;
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[--iRow][iCol] != 0)
- return false;
- }
- }
- }
- return true;
- }
- else if (abs(fRow - iRow) == abs(fCol - iCol) && (abs (fRow - iRow) > 0)) // net2aked eno et7rak aslan w law et7rak net2aked eno diagonally
- {
- int Check_Number = abs (fRow-iRow) - 1;// aw fcol-icol mesh far2a // kam check hane3mlo
- //cout << "kam marra: " << Check_Number << endl;
- if (fRow < iRow) // et7rak foo2
- {
- if (fCol>iCol) // et7rak foo2 ymeen
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[--iRow][++iCol] != 0)
- return false;
- }
- }
- else if (fCol<iCol) // foo2 shmal
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[--iRow][--iCol] != 0)
- return false;
- }
- }
- }
- else if (fRow>iRow) // et7rak ta7t
- {
- if (fCol>iCol) // ta7t ymeen
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[++iRow][++iCol] != 0)
- return false;
- }
- }
- else if (fCol<iCol) // t7t shmal
- {
- for (int count = 0; count < Check_Number; count++)
- {
- if (ChessBoard[++iRow][--iCol] != 0)
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- return false;
- }
- };
- class King : public Piece
- {
- public:
- King(char c) : Piece(c) {}
- ~King() {}
- char GetPiece()
- {
- return 'K';
- }
- bool IsLegalMove(int iRow, int iCol, int fRow, int fCol)
- {
- Piece* finalPos = ChessBoard[fRow][fCol];
- if ((finalPos == 0) || (color != finalPos -> GetColor()))
- {
- if ((((fRow - iRow) >= -1) && ((fRow - iRow) <= 1)) && (((fCol - iCol) >= -1) && ((fCol - iCol) <= 1)))
- {
- return true;
- }
- return false;
- }
- return false;
- }
- };
- int wScore = 0, bScore = 0;
- int main()
- {
- char Turn;
- int i,LOL,x;
- StartScreen();
- here:
- Turn='W';
- SetBoard();
- do
- {
- DispBoard();
- LOL=Game(Turn);
- if(LOL==-1)goto here;
- Contain mytemp;
- for (int myrows=0; myrows<8; myrows++)
- {
- for (int mycols=0; mycols<8; mycols++)
- {
- mytemp.Board[myrows][mycols] = ChessBoard[myrows][mycols];
- }
- }
- MyVector.push_back(mytemp);
- Turn = SwitchTurn(Turn);
- } while (EndGame(Turn) == false);
- cout<<"\n\nDo you want to play another game ?\n[1]for Yes\n[2]for No\n\n";
- cin>>i;
- if(i==1)goto here;
- return 0;
- }
- void StartScreen(){
- cout<<"#################################################\n";
- cout<<"#################################################\n";
- cout<<"#### BHB Ultimate CLI Chess Game v0.1 ####\n";
- cout<<"#################################################\n\n\n";
- cout<<"You can switch to one of these commands by inserting the command number :\n";
- cout<<"-Reset Game ==> 100\n";
- cout<<"-Undo Game ==> 101\n";
- cout<<"-Exit Game ==> 102\n\n";
- LET_ME_SLEEP
- }
- void SetBoard ()
- {
- int rows,cols;
- for (rows=0; rows<8; rows++)
- {
- for (cols=0; cols<8; cols++)
- ChessBoard[rows][cols] = 0; // Set kolaha b NULL
- }
- // Set el black pieces
- cols = 0;
- ChessBoard[0][cols++] = new Rook('B');
- ChessBoard[0][cols++] = new Knight('B');
- ChessBoard[0][cols++] = new Bishop('B');
- ChessBoard[0][cols++] = new Queen('B');
- ChessBoard[0][cols++] = new King('B');
- ChessBoard[0][cols++] = new Bishop('B');
- ChessBoard[0][cols++] = new Knight('B');
- ChessBoard[0][cols] = new Rook('B');
- cols = 0;
- while (cols<8)
- ChessBoard[1][cols++] = new Pawn('B');
- // Set el white pieces
- cols = 0;
- ChessBoard[7][cols++] = new Rook('W');
- ChessBoard[7][cols++] = new Knight('W');
- ChessBoard[7][cols++] = new Bishop('W');
- ChessBoard[7][cols++] = new Queen('W');
- ChessBoard[7][cols++] = new King('W');
- ChessBoard[7][cols++] = new Bishop('W');
- ChessBoard[7][cols++] = new Knight('W');
- ChessBoard[7][cols] = new Rook('W');
- cols = 0;
- while (cols<8)
- ChessBoard[6][cols++] = new Pawn('W');
- Contain mytemp;
- for (int myrows=0; myrows<8; myrows++)
- {
- for (int mycols=0; mycols<8; mycols++)
- {
- mytemp.Board[myrows][mycols] = ChessBoard[myrows][mycols];
- }
- }
- MyVector.push_back(mytemp);
- }
- void DispBoard ()
- {
- LET_ME_CLEAR
- for (int rows=0; rows<8; rows++)
- {
- cout << rows+1 << " ";
- for (int cols=0; cols<8; cols++)
- {
- if (ChessBoard[rows][cols] == 0)
- {
- if ((rows %2 ==0 && cols %2 == 0) || (rows %2 !=0 && cols %2 != 0))
- cout << "--";
- else
- cout << "++";
- }
- else
- cout << ChessBoard[rows][cols]->GetColor() << ChessBoard [rows][cols] -> GetPiece();
- cout << " ";
- }
- if (rows == 4)
- {
- cout << " White: " << wScore << " " << "Black: " << bScore;
- }
- cout << "\n" << endl;
- }
- cout << " ";
- // bytba3 el arqam ta7t
- for (int x = 1; x<9; x++)
- cout << setw(2) << x << " ";
- cout << "\n" << endl;
- }
- int Game (char c)
- {
- bool Valid = false;
- do
- {
- cout << c << "'s turn: ";
- int Start;
- cin >> Start;
- if(Start == 100)return -1;
- else if (Start == 101)
- {
- Piece* TempPiece;
- Contain TempCon;
- MyVector.pop_back();
- TempCon = MyVector.back();
- MyVector.pop_back();
- for (int rows=0; rows<8; rows++)
- {
- for (int cols=0; cols<8; cols++)
- {
- TempPiece = TempCon.Board[rows][cols];
- ChessBoard [rows][cols] = TempPiece;
- }
- }
- DispBoard();
- return 1;
- } //CODE FOR UNDO
- else if (Start == 102){cout<<"\n\nExiting BHB Chess ...\n\n";exit(0);}
- int StartRow = (Start / 10) - 1;
- int StartCol = (Start % 10) - 1;
- cout << "To: ";
- int End;
- cin >> End;
- int EndRow = (End / 10) - 1;
- int EndCol = (End % 10) - 1;
- if ((StartRow >= 0 && StartRow <= 7) && (StartCol >= 0 && StartCol <= 7) &&(EndRow >= 0 && EndRow <= 7) &&(EndCol >= 0 && EndCol <= 7))
- {
- //cout << "Hi 1" << endl;
- Piece * CurrentPiece = ChessBoard [StartRow][StartCol];
- if ((CurrentPiece != 0) && (CurrentPiece -> GetColor() == c))
- {
- //cout << "Hi 2" << endl;
- if (CurrentPiece->IsLegalMove(StartRow, StartCol, EndRow, EndCol))
- {
- //cout << "Hi 3" << endl;
- Piece* TempPiece = ChessBoard[EndRow][EndCol];
- ChessBoard[EndRow][EndCol] = ChessBoard[StartRow][StartCol];
- ChessBoard[StartRow][StartCol] = 0;
- // cout << c << endl;
- if (!Check(c))
- {
- //cout << "Hi 4" << endl;
- delete TempPiece;
- Valid = true;
- }
- else
- { // Undo the last move
- ChessBoard[StartRow][StartCol] = ChessBoard[EndRow][EndCol];
- ChessBoard[EndRow][EndCol]= TempPiece;
- }
- }
- }
- }
- if (Valid == false)
- {
- cout << "Invalid Move" << endl;
- }
- } while (Valid == false);
- return 0;
- }
- char SwitchTurn (char c)
- {
- if (c == 'W')
- return 'B';
- else
- return 'W';
- }
- bool Check(char c)
- {
- int KingRow,KingCol;
- for (int Row = 0; Row < 8; Row++)
- {
- for (int Col = 0; Col < 8; Col++)
- {
- if (ChessBoard[Row][Col] != 0)
- {
- if ((ChessBoard[Row][Col]-> GetColor() == c) && (ChessBoard [Row][Col]->GetPiece() =='K'))
- {
- KingRow = Row;
- KingCol = Col;
- //cout << KingRow << " " << KingCol << endl;
- }
- }
- }
- }
- for (int Row = 0; Row < 8; Row++)
- {
- for (int Col = 0; Col < 8; Col++)
- {
- if (ChessBoard[Row][Col] != 0)
- {
- if (ChessBoard[Row][Col]->GetColor() != c)
- {
- if (ChessBoard[Row][Col]->IsLegalMove(Row, Col, KingRow, KingCol))
- {
- //cout << ChessBoard[Row][Col]->GetColor() << ChessBoard[Row][Col]->GetPiece() << endl;
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- bool CanMove(char c)
- {
- // Run through all pieces
- for (int iRow = 0; iRow < 8; iRow++)
- {
- for (int iCol = 0; iCol < 8; iCol++)
- {
- if ((ChessBoard[iRow][iCol] != 0) && (ChessBoard[iRow][iCol]->GetColor() == c))
- {
- // If it is a piece of the current player, see if it has a legal move
- for (int fRow = 0; fRow < 8; fRow++)
- {
- for (int fCol = 0; fCol < 8; fCol++)
- {
- if (ChessBoard[iRow][iCol]->IsLegalMove(iRow, iCol, fRow, fCol))
- {
- // Make move and check whether king is in check
- Piece* Temp = ChessBoard[fRow][fCol];
- ChessBoard[fRow][fCol] = ChessBoard[iRow][iCol];
- ChessBoard[iRow][iCol] = 0;
- bool Move_Bool = !Check(c);
- // fel 7alteen Undo the move
- ChessBoard[iRow][iCol] = ChessBoard[fRow][fCol];
- ChessBoard[fRow][fCol] = Temp;
- if (Move_Bool)
- {
- return true;
- }
- }
- }
- }
- }
- }
- }
- return false;
- }
- bool EndGame(char c)
- {
- char Turn;
- // Check that the current player can move
- // If not, we have a stalemate or checkmate
- bool Move_Bool(false);
- Move_Bool = CanMove(c);
- if (Move_Bool == false)
- {
- if (Check(c))
- {
- Turn = SwitchTurn(c);
- cout << "Checkmate, " << Turn << " Wins!" << endl;
- if (Turn == 'W')
- {
- wScore++;
- }
- else
- {
- bScore++;
- }
- }
- else
- {
- cout << "Stalemate!" << endl;
- wScore++;
- bScore++;
- }
- }
- return !Move_Bool;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement