// 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; }
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; }