Battleship problems!

i am creating a very simple and "robust" version of battleship for my c++ class. the game is just pretty much the computer placing a board of his own and you try to find his locations but you do not have your own board or locations (for now). i tam almsot positive the problem lies in the setup_board method... i have only done the set up for the aircraft carrier (you will see what i mean), because the problem would be the same for the other ships as well.

p.s. i am aware of alot of useless code, and things that i have written here that havent been implemented, this is a very very very early rough draft and i want to at least get the basics working before i clean it up (because there is alot of cleaning up to do)

p.s.s. please ignore the comments, i may have written them at an earlier time and forgot to change or delete them accordingly so just completely ignore all of them


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
/

#include <iostream>
using namespace std;

struct ship {
	bool horiz_vert; //assign int 1-0 for true or fast. true is horizontal and false is vertical
	int coord1;
	int coord2;
	int size; //for health values!!!
};

//For all the different type of shifts
ship aircraft_carrier;
ship battleship;
ship submarine;
ship cruiser;
ship destroyer;

int usernum;
int grid[9][9]; //grid for battleship
//COLISION DETECTION!!!!

int coord1; //made only in method scope to not make anything overconfusion
int coord2;

//JUST MAKE SO YOu GUESS COMOUTER< You DONT PLACE THE BOAS IN THIS VERSION

void clrscrn() { //only way to clear screen
	cout << "\n \n \n \n \n \n \n \n " << endl;

}
void print_grid() { //function for refreshing grid

	cout << "1-2-3-4-5-6-7-8-9-10" << endl;
	for (int j = 0; j <= 9; j++) { //column for loop
		for (int i = 0; i <= 9; i++) { //row for loop

			cout << grid[i][j];
			if (i <= 8)
				cout << "-"; //hyphens make game easier to read

		}

		cout << " " << j + 1 << endl; //for formatting, makes grid correct
	}

}
void player_move() {
	char comma; //to take up space of comma when asking coorinates.
	cout << "please type in your coordinates ex. 5,2" << endl;
	cin >> coord1 >> comma >> coord2;

	coord1 -= 1; //-1 because of zero adding problem. only way to fix it
	coord2 -= 1;

	if (coord1 > 9 || coord2 > 9) {
		cout << "invalid coordinates, must be in between 1 and ten" << endl;
		player_move(); //INCURSION
	}

}

void setup_board() { //only sets up for computer...

	aircraft_carrier.size = 5;
	aircraft_carrier.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	aircraft_carrier.coord1 = rand() % 9 + 1;
	aircraft_carrier.coord2 = rand() % 9 + 1;

	battleship.size = 4;
	battleship.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	battleship.coord1 = rand() % 9 + 1;
	battleship.coord2 = rand() % 9 + 1;

	submarine.size = 4;
	submarine.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	submarine.coord1 = rand() % 9 + 1;
	submarine.coord2 = rand() % 9 + 1;

	destroyer.size = 4;
	destroyer.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	destroyer.coord1 = rand() % 9 + 1;
	destroyer.coord2 = rand() % 9 + 1;

	if (aircraft_carrier.horiz_vert = 1) { // if it is true... then solving for horizontal

		for (int i = 0; i <= 5; i++) { // 5 because that is the amount of health...
			if (coord1 == aircraft_carrier.coord1 + i
					|| coord2 == aircraft_carrier.coord2 + i) {
				grid[aircraft_carrier.coord1][aircraft_carrier.coord2] = 1; //1 signfies hit...
				clrscrn();
				print_grid();
				cout << "YOU HAVE HIT MY AIRCRAFT CARRIER!" << endl;

			}
		}
	} else {
		grid[coord1][coord2] = 2; // 2 signifies miss...
		clrscrn();
		player_move();
		setup_board();
		cout << "MISS" << endl;

	}

}

//ADD COLLISION DETECTION!

void comp_move() { //FOR COMPUTER GENERETAED MOVE

}



int main() {

	cout
			<< "Welcome to Battleship!, when selecting coordinates please keep in mind the first coordinate is the column and the econd is the row \n the 1 symbol signifies a hit while the 2 symbol signifies a miss"
			<< endl;

	for (int j = 0; j <= 9; j++) { //column for loop
		for (int i = 0; i <= 9; i++) { //row for loop

			grid[j][i] = 0; //sets everything to 0 for

		}
	}

	print_grid();

//grid[2][3] = 1; // changing group 2/3 will change the coordinates 3 and 4 on the rid, because 0 counts as a number...
//clrscrn();
//print_grid();

	player_move();
	setup_board();
	//guess from 1-
	return 0;
}



thanks!
There are a few problems here.

- First if the aircraft carrier is vertical the player automatically misses.

- You never call "srand(...)" to seed the random number generator. So random numbers aren't actually being generated.

- You should seperate the "collision detection" and the board setup into seperate functions. As it stands in order to detect a 'hit' or 'miss', you have to reassign the ships coordinates. That's not how I remember battleship being played.

- Your "collision detection" seems to be a little off, Lines 89 and 90. Right now it only matters if coord1 OR coord2 are guessed correctly and it will be registered as a hit. Also you don't have anything displayed if the player misses.

- This really isn't a proper game loop.
yea im still a beginner, and i wanted to do something cool for the final project.thank for your help, could you i assumed that the for loop would check if any of the coordinates said were contained by the ship by checking the starting points, then checking both horizontal, and vertical points,
ok i kind of got it working.. here take a look, i took aout one of the for loops and it there are some problems with the coordinates 9,10 and 3,3. also i cant seem to fint the battleship (i have only done one...) i dont know if that is working correctly

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
#include <iostream>
using namespace std;

struct ship {
	bool horiz_vert; //assign int 1-0 for true or fast. true is horizontal and false is vertical
	int coord1;
	int coord2;
	int size; //for health values!!!
};

//For all the different type of shifts
ship aircraft_carrier;
ship battleship;
ship submarine;
ship cruiser;
ship destroyer;

int usernum;
int grid[9][9]; //grid for battleship
//COLISION DETECTION!!!!

int coord1; //made only in method scope to not make anything overconfusion
int coord2;

//JUST MAKE SO YOu GUESS COMOUTER< You DONT PLACE THE BOAS IN THIS VERSION

void clrscrn() { //only way to clear screen
	cout << "\n \n \n \n \n \n \n \n " << endl;

}
void print_grid() { //function for refreshing grid

	cout << "1-2-3-4-5-6-7-8-9-10" << endl;
	for (int j = 0; j <= 9; j++) { //column for loop
		for (int i = 0; i <= 9; i++) { //row for loop

			cout << grid[i][j];
			if (i <= 8)
				cout << "-"; //hyphens make game easier to read

		}

		cout << " " << j + 1 << endl; //for formatting, makes grid correct
	}

}
void player_move() {
	char comma; //to take up space of comma when asking coorinates.
	cout << "please type in your coordinates ex. 5,2" << endl;
	cin >> coord1 >> comma >> coord2;

	coord1 -= 1; //-1 because of zero adding problem. only way to fix it
	coord2 -= 1;

	if (coord1 > 9 || coord2 > 9) {
		cout << "invalid coordinates, must be in between 1 and ten" << endl;
		player_move(); //INCURSION
	}

}

void setup_board() { //only sets up for computer...

	aircraft_carrier.size = 5;
	aircraft_carrier.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	aircraft_carrier.coord1 = rand() % 9 + 1;
	aircraft_carrier.coord2 = rand() % 9 + 1;

	battleship.size = 4;
	battleship.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	battleship.coord1 = rand() % 9 + 1;
	battleship.coord2 = rand() % 9 + 1;

	submarine.size = 4;
	submarine.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	submarine.coord1 = rand() % 9 + 1;
	submarine.coord2 = rand() % 9 + 1;

	destroyer.size = 4;
	destroyer.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
	destroyer.coord1 = rand() % 9 + 1;
	destroyer.coord2 = rand() % 9 + 1;

		for (int i = 0; i <= 5; i++) { // 5 because that is the amount of health...
			if (coord1 == aircraft_carrier.coord1 + i
					|| coord2 == aircraft_carrier.coord2 + i) {
				grid[aircraft_carrier.coord1][aircraft_carrier.coord2] = 1; //1 signfies hit...
				clrscrn();
				print_grid();
				cout << "YOU HAVE HIT MY AIRCRAFT CARRIER!" << endl;

			}
		 else {
			grid[coord1][coord2] = 2; // 2 signifies miss...
			clrscrn();
			print_grid();
			cout << "MISS" << endl;
			player_move();
			setup_board();



	}
		}}


//ADD COLLISION DETECTION!

void comp_move() { //FOR COMPUTER GENERETAED MOVE

}



int main() {

	cout
			<< "Welcome to Battleship!, when selecting coordinates please keep in mind the first coordinate is the column and the econd is the row \n the 1 symbol signifies a hit while the 2 symbol signifies a miss"
			<< endl;

	for (int j = 0; j <= 9; j++) { //column for loop
		for (int i = 0; i <= 9; i++) { //row for loop

			grid[j][i] = 0; //sets everything to 0 for

		}
	}

	print_grid();

//grid[2][3] = 1; // changing group 2/3 will change the coordinates 3 and 4 on the rid, because 0 counts as a number...
//clrscrn();
//print_grid();

	player_move();
	setup_board();
	//guess from 1-
	return 0;
}
Last edited on
You have created a 9x9 grid int grid[9][9];
but When you loop you treat it as a 10x10 grid.
oh i made a mistake but yea the grid is supposed to display 10 rows and 10 columns,
but the problem is in the 9,10 and 3,3 coordinates, you will see what i mean if you run it. also, i dont think ive set the board up correctly. as i am guesing the coordinates of th ship, random values across the board change to 1 and then i guess all the coordinates around it, nothing comes of it
Shouldn't the || on line 86 be &&?

Also your use of recursion is a bit strange. Why are you calling setup_board() from setup_board()? When you do this you move the aircraft_carrier to a new location each time so it could just as well go to a location I have already bombed.
yes thank you i was having trouble and that was why, but still, it on coordinates 3-3 and 9,9. it messes up. here is my revised code, can you look it over, because it stil isnt quite right..

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
#include <iostream>
using namespace std;

struct ship {
	bool horiz_vert; //assign int 1-0 for true or fast. true is horizontal and false is vertical
	int coord1;
	int coord2;
	int size; //for health values!!!
};

//For all the different type of shifts
ship aircraft_carrier;
ship battleship;
ship submarine;
ship cruiser;
ship destroyer;

int usernum;
int grid[9][9]; //grid for battleship
//COLISION DETECTION!!!!

int coord1; //made only in method scope to not make anything overconfusion
int coord2;

//JUST MAKE SO YOu GUESS COMOUTER< You DONT PLACE THE BOAS IN THIS VERSION

void clrscrn() { //only way to clear screen
	cout << "\n \n \n \n \n \n \n \n " << endl;

}
void print_grid() { //function for refreshing grid

	cout << "1-2-3-4-5-6-7-8-9-10" << endl;
	for (int j = 0; j <= 9; j++) { //column for loop
		for (int i = 0; i <= 9; i++) { //row for loop

			cout << grid[i][j];
			if (i <= 8)
				cout << "-"; //hyphens make game easier to read

		}

		cout << " " << j + 1 << endl; //for formatting, makes grid correct
	}

}
void player_move() {
	char comma; //to take up space of comma when asking coorinates.
	cout << "please type in your coordinates ex. 5,2" << endl;
	cin >> coord1 >> comma >> coord2;

	coord1 -= 1; //-1 because of zero adding problem. only way to fix it
	coord2 -= 1;

print_grid();

if(grid[coord1][coord2] == 2){
		cout<<"you have already used this space! "<<endl;
		player_move();
}
if (coord1 > 9 || coord2 > 9) {
		cout << "invalid coordinates, must be in between 1 and ten" << endl;
		player_move(); //RECURSION
	}

}

void setup_board() { //only sets up for computer...

	aircraft_carrier.size = 5;

	aircraft_carrier.coord1 = rand() % 9 + 1;
	aircraft_carrier.coord2 = rand() % 9 + 1;

	battleship.size = 4;
	battleship.coord1 = rand() % 9 + 1;
	battleship.coord2 = rand() % 9 + 1;

	submarine.size = 4;
	submarine.coord1 = rand() % 9 + 1;
	submarine.coord2 = rand() % 9 + 1;

	destroyer.size = 4;
	destroyer.coord1 = rand() % 9 + 1;
	destroyer.coord2 = rand() % 9 + 1;

		for (int i = 0; i <= 5; i++) { // 5 because that is the amount of health...
			if (coord1 == aircraft_carrier.coord1 + i
					|| coord2 == aircraft_carrier.coord2 + i) {
				grid[aircraft_carrier.coord1][aircraft_carrier.coord2] = 1; //1 signfies hit...
				clrscrn();
				print_grid();
				cout << "YOU HAVE HIT MY AIRCRAFT CARRIER!" << endl;


			}
		 else {
			grid[coord1][coord2] = 2; // 2 signifies miss...
			clrscrn();
			print_grid();
			cout << "MISS" << endl;
			player_move();



	}
		}}


//ADD COLLISION DETECTION!

void comp_move() { //FOR COMPUTER GENERETAED MOVE

}



int main() {

	cout
			<< "Welcome to Battleship!, when selecting coordinates please keep in mind the first coordinate is the column and the second is the row \n the 1 symbol signifies a hit while the 2 symbol signifies a miss \n \n at any time type in 99 and it will reveal the enemies hiding spots!"
			<< endl;

	for (int j = 0; j <=10; j++) { //column for loop
		for (int i = 0; i <= 10; i++) { //row for loop

			grid[j][i] = 0; //sets everything to 0 for

		}
	}

	print_grid();

//grid[2][3] = 1; // changing group 2/3 will change the coordinates 3 and 4 on the rid, because 0 counts as a number...
//clrscrn();
//print_grid();

setup_board();

	//guess from 1-
	return 0;
}
Topic archived. No new replies allowed.