1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
class Game {
private:
Board theBoard;
vector <Piece*> allThePieces; //a vector of Piece pointers
string whoseTurn; //holds either "black" or "white"
//this method converts the user's input into a form the board can understand.
//for instance, it changes the user's input "B" to 2. it also does the same for
//the input "3", which is changed to a 3.
int convertStringToNum(char); //implement this
//this method constructs all the pieces and places them on the board
void createPieces();//implement part of this; do one or two pieces
Piece * findPiece(int, int); //locates a particular piece using row and column coordinates
bool intersectsOtherPieces(int, int, int, int); //checks whether path moves through other pieces
//checks for a piece at a location, then removes it from the board
void capturePiece(int, int);
public:
//public methods
Game(); //constructor
~Game(); //destructor
void takeTurns(); //swaps whose turn it is and ask for moves
//tries to move a piece. if there are no problems, it returns true.
//if it is unsuccesful (for instance, if the move is illegal), it returns false.
bool movePiece(int, int, int, int);
//checks whether each team still has their king, if one is missing, the other
//team is declared the winner
bool isGameOver();
};
Game::Game() {
createPieces();
whoseTurn = "white";
cout <<"Let the games begin! \n";
} // end of createPieces
void Game::createPieces() {
for (int i=1; i<=8; i++) {
allThePieces.push_back(new Pawn (2, i, "black"));
allThePieces.push_back(new Pawn (7, i, "white"));
allThePieces.push_back(new King (1, 5, "black"));
allThePieces.push_back(new King (8, 5, "white"));
allThePieces.push_back(new Queen (1, 4, "black"));
allThePieces.push_back(new Queen (8, 4, "white"));
allThePieces.push_back(new Knight (1, 2, "black"));
allThePieces.push_back(new Knight (8, 2, "white"));
allThePieces.push_back(new Knight (1, 7, "black"));
allThePieces.push_back(new Knight (8, 7, "white"));
allThePieces.push_back(new Rook (1, i, "black"));
allThePieces.push_back(new Rook (8, i, "white"));
allThePieces.push_back(new Rook (1, i, "black"));
allThePieces.push_back(new Rook (8, i, "white"));
allThePieces.push_back(new Bishop (1, 8, "black"));
allThePieces.push_back(new Bishop (8, 1, "white"));
allThePieces.push_back(new Bishop (1, 1, "black"));
allThePieces.push_back(new Bishop (8, 8, "white"));
}
for (int i=0; i<allThePieces.size(); i++) {
theBoard.setPiece(allThePieces.at(i)->label, allThePieces.at(i)->currentRow-1, allThePieces.at(i)->currentColumn-1);
}
} //end of createPieces
bool Game::movePiece(int pieceRow, int pieceCol, int destRow, int destCol) {
Piece * tempPiece;
tempPiece = findPiece(pieceRow, pieceCol);
//first, make sure there's a piece where they selected
if (tempPiece->currentRow == -1) {
cout<<"no piece exists at "<<pieceRow<<", "<<pieceCol<<"\n";
return false;
}
//next, make sure they are moving one of their own pieces
else if (whoseTurn != tempPiece->color) {
cout<<"that is not your piece\n";
return false;
}
//next, check that the destination is not the same as the current location
else if (pieceRow == destRow && pieceCol == destCol) {
cout<<"your piece is already at that location\n";
return false;
}
//next, check to make sure i'm not moving onto a piece of my own color
else if (findPiece(destRow, destCol)->color == tempPiece->color) {
cout<<"can't move on top of a piece of your own color\n";
return false;
}
//next, make sure the path is clear to my goal spot
else if (intersectsOtherPieces(pieceRow, pieceCol, destRow, destCol) &&
!tempPiece->canHop ) {
cout<<"there is a piece in your way. only knights can jump over other pieces.\n";
return false;
}
//next, check to see if the move is legal for that piece
else if (!tempPiece->isMoveLegal(destRow, destCol)) {
cout<<"sorry, that move is not legal!\n";
return false;
}
//if we've made it this far, we're okay. place the piece
cout<<"moving piece "<<tempPiece->label<<" to "<<destRow<<", "<<destCol<<"\n";
//capture any piece that's there
capturePiece(destRow, destCol);
//inform the board
theBoard.setPiece(tempPiece->currentRow-1, tempPiece->currentColumn-1, destRow-1, destCol-1);
//change the piece's location
tempPiece->currentRow = destRow;
tempPiece->currentColumn = destCol;
} //end of movePiece
Piece * Game::findPiece(int row, int col) {
bool foundPiece = false;
Piece * currPiece;
for (int i=0; i<allThePieces.size(); i++) { //iterate through all the pieces
currPiece = allThePieces.at(i);
if (currPiece->currentRow == row && currPiece->currentColumn == col) {
return currPiece;
foundPiece = true;
} //end of searching for piece
}
if (!foundPiece) {
return &Piece("no piece", -1, -1, "no color");
}
} //end of findPiece
bool Game::intersectsOtherPieces(int sourceRow, int sourceColumn, int destRow, int destColumn) {
int tempRow = sourceRow;
int tempColumn = sourceColumn;
while(tempRow != destRow || tempColumn != destColumn) {
if(tempRow < destRow) { tempRow++; }
else if (tempRow > destRow) { tempRow--; }
if(tempColumn < destColumn) { tempColumn++; }
else if (tempColumn > destColumn) { tempColumn--; }
if (findPiece(tempRow, tempColumn)->currentRow != -1) {
//cout<<"found piece in your way at "<<tempRow<<", "<<tempColumn;
return true;
}
return false;
} //end of while
} //end of intersectsOtherPieces
int Game::convertStringToNum(char letter) {
//implement this, it does not work
return -1;
} //end of convertLetterToNum
void Game::takeTurns() {
if (whoseTurn == "white") { whoseTurn = "black"; }
else { whoseTurn = "white"; }
char source1, source2, dest1, dest2;
int sRow, sCol, dRow, dCol;
bool moveSuccessful = false;
while (!moveSuccessful) {
theBoard.drawBoard();
//prompt for move
cout<<whoseTurn<<", which piece would you like to move? > ";
cin>>source1>>source2;
cout<<"where would you like to move? > ";
cin>>dest1>>dest2;
system("cls");
//convert the letter to a number
if (isalpha(source1)) {
sCol = convertStringToNum(source1);
sRow = convertStringToNum(source2);
}
else {
sRow = convertStringToNum(source1);
sCol = convertStringToNum(source2);
}
if (isalpha(dest1)) {
dCol = convertStringToNum(dest1);
dRow = convertStringToNum(dest2);
}
else {
dRow = convertStringToNum(dest1);
dCol = convertStringToNum(dest2);
}
moveSuccessful = movePiece(sRow, sCol, dRow, dCol);
} //end of while move is not successful
} //end of takeTurns
|