### Battleship game question

I am writing a program for a project for my c++ class. Our instructor gave us code for the game and we have to add something to it. One thing we have to add is a rejection if they already guessed that grid location. How would I do that? BTW this is only a 1 player Battleship game. The user guesses that comps. location.
Each cell in the grid can be one of several values.
- empty
- part of a ship, possibly indicating the type of ship.
- A partially hit ship
- A sunk ship
- A hit on an empty cell

I am just confused. I don't really know how to write the part of code that tells that that grid location was already guessed.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` ``````#ifndef __PROJECT1_H_ #define __PROJECT1_H_ #include #include #include #include // For time() #include // For srand() and rand() #endif 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 WIDTH = 5; // Width of the grid. const int HEIGHT= 5; Damage grid[WIDTH][HEIGHT]; // Records status of every grid cell. //================================================== prototypes void dropBomb(int xcoord, int ycoord); void executeCommand(char cmd); void initGrid(); bool isGameOver(); void print(); //======================================================== main int main() { srand(time(0)); // Initialize random number generator char ans; do { initGrid(); // Randomly place ships on the grid. print(); cout << "Please enter your orders sir: "; char commandCode; while (cin >> commandCode) { executeCommand(commandCode); if (isGameOver()) { cout << "Congratuations, you win" << endl; break; } } cout << "Do you want to play again? (y/n) "; ans = 'n'; cin >> ans; } while (ans == 'y'); return 0; }//end main //======================================================= executeCommand void executeCommand(char cmd) { switch (cmd) { case 'q': // quit cout << "Yes sir, now quitting. It's been an honour sir." << endl; exit(0); break; case 'b': // bomb int xloc, yloc; cin >> xloc; cin >> yloc; if (xloc>=0 && xloc= 0 && yloc < HEIGHT){ cout << "Yes sir. Dropping ordinance at (x,y):(" << xloc << "," << yloc << ")" << endl; dropBomb(xloc, yloc); }else{ cout << "Error: Y Bombing coordinate must be between 0 and " << HEIGHT-1 << endl; } } else { cout << "Error: X Bombing coordinate must be between 0 and " << WIDTH-1 << endl; } print(); cout << "Please enter your orders sir: "; break; default: // error cerr << "Bad input " << cmd << endl; break; } } //======================================================= dropBomb void dropBomb(int xcoord, int ycoord) { switch (grid[xcoord][ycoord]) { case WATER_UNBOMBED: grid[xcoord][ycoord] = WATER_BOMBED; break; case SHIP_UNBOMBED: grid[xcoord][ycoord] = SHIP_BOMBED; break; } return; } //======================================================= initGrid // initGrid() places random ships on the grid. void initGrid() { for (int i=0; i

here is the code i was given
Last edited on
At line 83 you're checking that x and y are inbounds. That is also where you want to check if the grid location has already been bombed.
 ``123456`` `````` if (grid[x][y] == WATER_BOMBED || grid[x][y] == SHIP_BOMBED) { cout << "That location has already been bombed" << endl; cout << "Try again" << endl; return; // exit and prompt for anoth command }``````

AbstractionAnon, I am getting an error with that. It says "expected declaration".
If you're getting "expected declaration", you probably have unmatched {}.
Look for an extra }.

If you can't spot it, post your executeCommand function in its entirity.
Last edited on
Topic archived. No new replies allowed.