//==================================================== includes #include #include #include #include // For time() #include // For srand() and rand() using namespace std; //========================================= global declarations // Damage enum defines possible values at each grid point. enum Damage {WATER_UNBOMBED, WATER_BOMBED, SHIP_UNBOMBED , SHIP_BOMBED}; // The display array defines what to display for // each of the Damage enum values. Changes to the // Damage enum require this to be changed also. const char display[] = {'-', 'O', '-', 'X'}; const int SHIP_SIZE = 3; const int NUMBER_OF_SHIPS = 4; const int CANAL_LENGTH = 24; // Width of the grid. Damage grid[CANAL_LENGTH]; // Records status of every grid cell. //================================================== prototypes void dropBomb(int coord); void executeCommand(char cmd); void findOpenWater(int positions[], int& number); void initGrid(); bool isGameOver(); void printGrid(); //======================================================== main int main() { srand(time(0)); // Initialize random number generator cout << "You are to find and bomb ships in the Suez Canal." << endl; cout << "Each ship is " << SHIP_SIZE << " units long." << endl; cout << "Use the b command and follow it with a coordinate to drop a bomb." << endl; char ans; do { initGrid(); // Randomly place ships on the grid. printGrid(); char commandCode; while (cin >> commandCode) { executeCommand(commandCode); if (isGameOver()) { cout << "I surrender." << endl; break; } } cout << "Do you want to play again? (y/n) "; cin >> ans; } while (ans == 'y'); return 0; }//end main //======================================================= executeCommand void executeCommand(char cmd) { switch (cmd) { case 'q': // quit exit(0); break; case 'b': // bomb int loc; cin >> loc; if (loc>=0 && loc=0; i--) { if (grid[i]==WATER_UNBOMBED) { consecutiveWater++; if (consecutiveWater > SHIP_SIZE) { //-- record this as a possible position. positions[number] = i; number++; } } else { consecutiveWater = 0; } } } //====================================================== printGrid void printGrid() { cout << endl; for (int i=0; i