// Return false without changing anything if moving one step from (r,c) // in the indicated direction would hit a run off the edge of the colosseum. // Otherwise, update r and c to the position resulting from the move and // return true. bool attemptMove(const Colosseum& colosseum, int dir, int& r, int& c) { switch (dir) { case NORTH: if (r <= 1) { return false; } else { r--; break; } case EAST: if (c >= colosseum.cols()) { return false; } else { c++; break; } case SOUTH: if (r >= colosseum.rows()) { return false; } else { r++; break; } case WEST: if (c <= 1) { return false; } else { c--; break; } } return true; }
bool attemptMove(const Colosseum& colosseum, int dir, int& r, int& c) { int rnew = r; int cnew = c; switch (dir) { case NORTH: if (r <= 1) return false; else rnew--; break; case EAST: if (c >= colosseum.cols()) return false; else cnew++; break; case SOUTH: if (r >= colosseum.rows()) return false; else rnew++; break; case WEST: if (c <= 1) return false; else cnew--; break; } r = rnew; c = cnew; return true; }
string Game::takePlayerTurn() { for (;;) { cout << "Your move (n/e/s/w/p or nothing): "; string playerMove; getline(cin, playerMove); Player* player = m_colosseum->player(); int dir; if (playerMove.size() == 0) { if (recommendMove(*m_colosseum, player->row(), player->col(), dir)) return player->move(dir); else return player->push(); } else if (playerMove.size() == 1) { if (tolower(playerMove[0]) == 'p') return player->push(); else if (charToDir(playerMove[0], dir)) return player->move(dir); } cout << "Player move must be nothing, or 1 character n/e/s/w/p." << endl; } }
int main(int argc, char *argv[]) { Pokemon a; Pokemon b; Colosseum c; char playagain = 'y'; do //This runs the game as many times until the user wants to end { c.userBuild(a); c.userBuild(b); c.play(a,b); cout << "Would you like to play again (y/n): " << endl; cin >> playagain; }while(playagain=='y'); cout << "Thanks for playing!" << endl; return 0; }
void Game::play() { m_colosseum->display(""); while ( ! m_colosseum->player()->isDead() && m_colosseum->villainCount() > 0) { string msg = takePlayerTurn(); Player* player = m_colosseum->player(); if (player->isDead()) break; m_colosseum->moveVillains(); m_colosseum->display(msg); } if (m_colosseum->player()->isDead()) cout << "You lose." << endl; else cout << "You win." << endl; }
int computeDanger(const Colosseum& colosseum, int r, int c) { // Our measure of danger will be the number of villains that might move // to position r,c. If a villain is at that position, it is fatal, // so a large value is returned. if (colosseum.numberOfVillainsAt(r,c) > 0) return MAXVILLAINS+1; int danger = 0; if (r > 1) danger += colosseum.numberOfVillainsAt(r-1,c); if (r < colosseum.rows()) danger += colosseum.numberOfVillainsAt(r+1,c); if (c > 1) danger += colosseum.numberOfVillainsAt(r,c-1); if (c < colosseum.cols()) danger += colosseum.numberOfVillainsAt(r,c+1); return danger; }