Battleship

Hy guys.

I made a simple Battleship game. I am actually okay with its functionality but maybe you have some input to improve it. ;)

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <string>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

//global variable declaration

string output[24];
int random_numbers[4];

int counter = 5;//tells you the amount of remaining ships
int grid;//grid declaration for input
bool setup_done  = false;//main game loop -> controls whether everything in initialized/ also quits the game at the end
bool valid_input = false;//inner loop -> controls the input

//All used function prototypes
void setup_Grid();//Just to make the Grid look better at initialization = I mean the grey boxes^^
void DisplayGrid();//At first it Clears the screen and then prints out the table and the grid
void create_random_numbers();//Here the random number are initialized
inline void input_grid();//checks wether the input is correct
inline void hit_conditions();//looks out for a "hit"

void main()
{
	setup_Grid();
	DisplayGrid();
	create_random_numbers();

	setup_done = true;
	while (setup_done)//main loop
	{
		input_grid();//inner loop inside the function
		hit_conditions();
	}
	system("pause");
}

//Function declaration
void setup_Grid()
{
	for (int i = 0; i <= 24; i++)
	{
		output[i] = 0xB0;
	}
}
void DisplayGrid()
{
	system("CLS");//Clears the screen
	cout << "GRID CODES: \n";
	cout << "  " << 0 << " |  " << 1 << " |  " << 2 << " | " << 3 << "  | " << 4 << "\n";
	cout << "-----------------------\n";
	cout << "  " << 5 << " |  " << 6 << " |  " << 7 << " | " << 8 << "  | " << 9 << "\n";
	cout << "-----------------------\n";
	cout << " " << 10 << " | " << 11 << " | " << 12 << " | " << 13 << " | " << 14 << "\n";
	cout << "-----------------------\n";
	cout << " " << 15 << " | " << 16 << " | " << 17 << " | " << 18 << " | " << 19 << "\n";
	cout << "-----------------------\n";
	cout << " " << 20 << " | " << 21 << " | " << 22 << " | " << 23 << " | " << 24 << "\n";
	cout << "\n\n" << "Your field: " << counter << " ships left!\n";
	cout << " " << output[0] << "  | " << output[1] << "  | " << output[2] << "  | " << output[3] << "  | " << output[4] << "\n";
	cout << "-----------------------\n";
	cout << " " << output[5] << "  | " << output[6] << "  | " << output[7] << "  | " << output[8] << "  | " << output[9] << "\n";
	cout << "-----------------------\n";
	cout << " " << output[10] << "  | " << output[11] << "  | " << output[12] << "  | " << output[13] << "  | " << output[14] << "\n";
	cout << "-----------------------\n";
	cout << " " << output[15] << "  | " << output[16] << "  | " << output[17] << "  | " << output[18] << "  | " << output[19] << "\n";
	cout << "-----------------------\n";
	cout << " " << output[20] << "  | " << output[21] << "  | " << output[22] << "  | " << output[23] << "  | " << output[24] << "\n";
}
void create_random_numbers()
{
	srand(time(NULL));
	for (int i = 0; i<5; i++)
	{
		bool check; //variable to check or number is already used
		int n; //variable to store the number in
		do
		{
			n = rand() % 24 + 0 ;
			//check or number is already used:
			check = true;
			for (int j = 0; j<i; j++)
				if (n == random_numbers[j]) //if number is already used
				{
					check = false; //set check to false
					break; //no need to check the other elements of value[]
				}
		} while (!check); //loop until new, unique number is found
		random_numbers[i] = n; //store the generated number in the array
	}
	/*for (int i = 0; i < 5; i++) Just to see the random numbers^^
	{
		cout << " " << random_numbers[i] << endl;
	}*/
}
void input_grid()
{
	while (valid_input == false)
	{
		cout << "\nEnter a valid grid code: ";
		cin >> grid;
		if (grid >= 0 && grid <= 24)
		{
			valid_input = true;
		}
		else
		{
			cout << "Please type in a number between 0-24! \n";
			valid_input = false;
		}
	}
}
void hit_conditions()
{
	//First ship
	if (grid == random_numbers[0])
	{
		counter--;
		output[grid] = 'X';
		
		system("CLS");
		DisplayGrid();
		cout << "\nYou hit the ship!\n";
		valid_input = false;
		setup_done = true;
		if (counter == 0)//checks whether all ships were hit
		{
			system("CLS");
			DisplayGrid();
			valid_input = true;
			setup_done = false;
			cout << "\nYou hit all ships!\n";
		}
	}
	// Second ship
	else if (grid == random_numbers[1])
	{
		counter--;
		output[grid] = 'X';
				
		system("CLS");
		DisplayGrid();
		cout << "\nYou hit the ship!\n";
		valid_input = false;
		setup_done = true;
		if (counter == 0)//checks whether all ships were hit
		{
			system("CLS");
			DisplayGrid();
			valid_input = true;
			setup_done = false;
			cout << "\nYou hit all ships!\n";
		}
	}
	//Third ship
	else if (grid == random_numbers[2])
	{
		counter--;
		output[grid] = 'X';

		system("CLS");
		DisplayGrid();
		cout << "\nYou hit the ship!\n";
		valid_input = false;
		setup_done = true;
		if (counter == 0)//checks whether all ships were hit
		{
			system("CLS");
			DisplayGrid();
			valid_input = true;
			setup_done = false;
			cout << "\nYou hit all ships!\n";
		}
	}
	//Forth ship
	else if (grid == random_numbers[3])
	{
		counter--;
		output[grid] = 'X';

		system("CLS");
		DisplayGrid();
		cout << "\nYou hit the ship!\n";
		valid_input = false;
		setup_done = true;
		if (counter == 0)//checks whether all ships were hit
		{
			system("CLS");
			DisplayGrid();
			valid_input = true;
			setup_done = false;
			cout << "\nYou hit all ships!\n";
		}
	}
	//Fith ship
	else if (grid == random_numbers[4])
	{
		counter--;
		output[grid] = 'X';

		system("CLS");
		DisplayGrid();
		cout << "\nYou hit the ship!\n";
		valid_input = false;
		setup_done = true;
		if (counter == 0)//checks whether all ships were hit
		{
			system("CLS");
			DisplayGrid();
			valid_input = true;
			setup_done = false;
			cout << "\nYou hit all ships!\n";
		}
	}
	//Executed if nothing was "hit"
	else
	{
		output[grid] = 'O';
		system("CLS");
		DisplayGrid();
		cout << "\nYou did not hit a ship!\n";
		valid_input = false;
		setup_done = true;
	}
}
Last edited on
IMO way too many global variables, strive for none.

The function main() must be defined to return an int, and you should return an int from this function.

I would use loops in DisplayGrid()
Imagine you want to change the size of the board.

Another way to improve would be to a class for the gameboard and game
Topic archived. No new replies allowed.