Battleship help!

A while ago I was here with a project on battleship. Now I am working with that program again for another project. I need to add a ship. I wrote the code but when a run it, only one ship will be in the grid. How do I get the 2nd?

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
#include "Project2.h"

using namespace std;

//PROJECT 2...ADD ANOTHER SHIP TO GRID

//========================================= 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();
void initGrid2(); //shows initGrid2
bool isGameOver();
void print();

//======================================================== main
int main() {

    srand(time(0));  // Initialize random number generator
    
    char ans;
    do {
        initGrid();  // Randomly place ships on the grid.
		initGrid2();  // Randomly places ship on the grid.  //new code
        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<WIDTH){
				if(yloc >= 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<WIDTH; i++) {  // clear grid
		for(int j = 0; j < HEIGHT; j++){
			grid[i][j] = WATER_UNBOMBED;
		}
    }
	int startx = rand() % (WIDTH-2); // Place a 3-cell ship in grid.
	int starty = rand() % (HEIGHT-2);
    grid[startx+0][starty+0] = SHIP_UNBOMBED;
    grid[startx+1][starty+1] = SHIP_UNBOMBED;
    grid[startx+2][starty+2] = SHIP_UNBOMBED;
	}
//=======================================================initGrid2	NEW CODE
// initGrid2() places random ships on the grid.
void initGrid2() {
    for (int i=0; i<WIDTH; i++) {  // clear grid
		for(int j = 0; j < HEIGHT; j++){
			grid[i][j] = WATER_UNBOMBED;
		}
    }

	int startx = rand() % (WIDTH-1); // Place a 2-cell ship in grid.
	int starty = rand() % (HEIGHT-1);
    grid[startx+0][starty+0] = SHIP_UNBOMBED;
    grid[startx+1][starty+1] = SHIP_UNBOMBED;
    //grid[startx+2][starty+2] = SHIP_UNBOMBED;
	}


//========================================================== print
void print() {
    cout << endl;

	cout << setw(2) << " ";
    
    //-- Print coordinates.
    for (int coord=0; coord<WIDTH; coord++) {
        cout << setw(2) << coord;
    }

	cout << endl;

    for (int i=0; i<WIDTH; i++) {
	    cout << setw(2) << i;
		for(int j = 0; j < HEIGHT; j++){
			cout << " " << display[grid[i][j]];
		}
		cout << endl;
    }
    
    cout << endl << endl;
}

//====================================================== isGameOver
bool isGameOver() {
    for (int i=0; i<WIDTH; i++) {
		for(int j = 0; j < HEIGHT; j++){
			if (grid[i][j] == SHIP_UNBOMBED) {
				return false;
			}
        }
    }
    return true;
}


It is in initGrid and initGrid2
Last edited on
You could create a class for the ships.
1
2
3
4
5
6
class ship
{
public:
xCoord;
yCoord;
};

and then just create an variable of type ship
1
2
ship uboat;
ship ship1;

etc...
So would I get rid of the initGrids? I am really not the best programmer, I always get confused.
you could just create a function in the class like
 
void setcoords();

and then set them using the same code as initGrids.
Except you would have to pass the coordinates to the grid inside the function.
Then it would be as simple as
 
uboat.setcoords();
Last edited on
Still can't seem to get this to work.

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
 #include "Project2.h"

using namespace std;

//PROJECT 2...ADD ANOTHER SHIP TO GRID

//========================================= global declarations
// Damage enum defines possible values at each grid point.
enum Damage {WATER_UNBOMBED, WATER_BOMBED, 
             SHIP_UNBOMBED , SHIP_BOMBED, SHIP_UNBOMBED2, SHIP_BOMBED2};

// 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();
void initGrid2(); //shows initGrid2
bool isGameOver();
void print();

//======================================================== main
int main() {

    srand(time(0));  // Initialize random number generator
    
    char ans;
    do {
        initGrid();  // Randomly place ships on the grid.
		initGrid2();  // Randomly places ship on the grid.  //new code
        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<WIDTH){
				if(yloc >= 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;
		  
	case SHIP_UNBOMBED2:
		grid[xcoord][ycoord] = SHIP_BOMBED2;
          break;
    }
    return;
}

//======================================================= initGrid
// initGrid() places random ships on the grid.
void initGrid() {
    for (int i=0; i<WIDTH; i++) {  // clear grid
		for(int j = 0; j < HEIGHT; j++){
			grid[i][j] = WATER_UNBOMBED;
		}
    }
	int startx = rand() % (WIDTH-2); // Place a 3-cell ship in grid.
	int starty = rand() % (HEIGHT-2);
    grid[startx+0][starty+0] = SHIP_UNBOMBED;
    grid[startx+1][starty+1] = SHIP_UNBOMBED;
    grid[startx+2][starty+2] = SHIP_UNBOMBED;
	}
//=======================================================initGrid2	NEW CODE
// initGrid2() places random ships on the grid.
void initGrid2() {
    for (int p=0; p<WIDTH; p++) {  // clear grid
		for(int q = 0; q < HEIGHT; q++){
			grid[p][q] = WATER_UNBOMBED;
		}
    }

	int startx = rand() % (WIDTH-1); // Place a 2-cell ship in grid.
	int starty = rand() % (HEIGHT-1);
    grid[startx+0][starty+0] = SHIP_UNBOMBED;
    grid[startx+1][starty+1] = SHIP_UNBOMBED;
    //grid[startx+2][starty+2] = SHIP_UNBOMBED;
	}


//========================================================== print
void print() {
    cout << endl;

	cout << setw(2) << " ";
    
    //-- Print coordinates.
    for (int coord=0; coord<WIDTH; coord++) {
        cout << setw(2) << coord;
    }

	cout << endl;

    for (int i=0; i<WIDTH; i++) {
	    cout << setw(2) << i;
		for(int j = 0; j < HEIGHT; j++){
			cout << " " << display[grid[i][j]];
	
		}
		cout << endl;
    }
    
	
    cout << endl << endl;
}

//====================================================== isGameOver
bool isGameOver() {
    for (int i=0; i<WIDTH; i++) {
		for(int j = 0; j < HEIGHT; j++){
			if (grid[i][j] == SHIP_UNBOMBED) {
				return false;
				for(int p=0; p<WIDTH; p++) {
					for(int q = 0; q < HEIGHT; q++) {
						if (grid[p][q] == SHIP_UNBOMBED2) {
							return false;
						}
					}
				}
			}
        }
    }
    return true;
}  


I feel like I am really close but cannot get to to work.
What exactly is wrong with it?
I cant check because you haven't included the header.
^^ Agreed with jidder. It would be nice to see the header and also I would add in some sort of test case to check if the user has entered the same coordinates.
Topic archived. No new replies allowed.