Battleships problem!!

I've been trying to build a battleships game for a course I'm doing at the moment, but I have hit a road block. My code works smoothly, but I don't know if I have structured it right so far, as I am not able to figure out how to place my ships on the board. Would anyone be able to give me a hand, or some insightful wisdom?

Thanks!!

Here's what I have so far:
It runs fine if you want to compile it on your computers

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

int Menu(); 
int Instructions(); 
void GameBoard(); 
char CoordEnterLetter();
int CoordEnterNumber();
char DirecCoord(); 
void AirCPlacement(); 
void BShipPlacement(); 
void DestPlacement(); 
void SubPlacement(); 
void PTBoatPlacement(); 


int main()
{
	//Characters for the different ships
	char AirCarrier = 'A';
	char Battleship = 'B';
	char Destroy = 'D';
	char Sub = 'S';
	char PtBoat = 'P';

	//Calls the menu function, which in turn, calls all the other functions
	Menu();

    int iTemp;
    cin >> iTemp;

    return 0;
}

void GameBoard()
{
	//Generates the game board
    const int ROWS = 12;
    const int COLUMNS = 12;
	bool bValue = false;

    char cBoard[ROWS][COLUMNS] = { { ' ', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
                                    { ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '7', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { ' ', '9', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
                                    { '1', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '} };

	
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
        {
            cout << cBoard[i][j];
        }
		
		cout << endl;

	//Calls the ship placement functions, asking where the user would like those ships to be placed
	

	}

}

char CoordEnterLetter()
{
	
	char cLetter = 'a';
	bool bValue = false;
	
	//While loop for entering a letter coordinate
	while (bValue == false)
	{
		cout << "Please enter a letter between A-J: ";
		cin >> cLetter;
		cout << endl;

		if (cLetter >= 0 && cLetter <= 64)
		{
			cout << "Invalid Input, please enter a letter between A-J" << endl;
			cout << endl;
		}
		else if (cLetter >= 75 && cLetter <= 96)
		{
			cout << "Invalid Input, please enter a letter between A-J" << endl;
			cout << endl;
		}
		else if (cLetter >= 107 && cLetter <= 256)
		{
			cout << "Invalid Input, please enter a letter between A-J" << endl;
			cout << endl;
		}
		else
		{
			break;
		}
	}



return cLetter;

}

int CoordEnterNumber()
{
	//While loop for entering a number coordinate
	int iNumber;
	bool bValue = false;

	while (bValue == false)
	{
		cout << "Please enter a number between 1-10: ";
		cin >> iNumber;
		cout << endl;

		if (iNumber == 0 || iNumber > 10)
		{
			cout << "Invalid Input, please enter a number between 1-10" << endl;
			cout << endl;
		}
		else 
		{
			break;
		}
	}

	return iNumber;
}

char DirecCoord()
{
	char cDirection = 'a';
	bool bValue = false;

	//While loop for selecting which direction the ship should be facing
	while (bValue == false)
	{
		cout << endl;
		cout << "Please enter a direction for your ship to face: " << endl;
		cout << "Up (U)" << endl;
		cout << "Down (D)" << endl;
		cout << "Left (L)" << endl;
		cout << "Right (R)" << endl;
		cout << "Enter here: ";
		cin >> cDirection;
		cout << endl;

		if ( cDirection == 68 || cDirection == 100)
		{
			cout << "You selected Down." << endl;
			cout << endl;
			break;
		}
		else if ( cDirection == 76 || cDirection == 108)
		{
			cout << "You selected Left." << endl;
			cout << endl;
			break;
		}
		else if ( cDirection == 82 || cDirection == 114)
		{
			cout << "You selected Right." << endl;
			cout << endl;
			break;
		}
		else if (cDirection == 85 || cDirection == 117)
		{
			cout << "You selected Up." << endl;
			cout << endl;
			break;
		}
		else
		{
			cout << "Invalid Input, please enter a new direction." << endl;
			cout << endl;
		}

	}

		return cDirection;
}


int Menu()
{
	//The Menu function
	char cMenu = 'a';
	bool bValue = false;

	cout << "Welcome To Battleships!!" << endl;
	cout << endl;
	
	while (bValue == false)
	{
		cout << "1: Play Game." << endl;
		cout << "2: Instructions." << endl;
		cout << "3: Exit Game." << endl;

		//The player can choose one of three options:
		//1) Play the game
		//2) View instructions
		//3) Exit the game
		cout << "What do you want to do? ";
		cin >> cMenu;
		cout << endl;

		if (cMenu == 49)
		{
			cout << "Prepare For Battle!!!" << endl;
			cout << endl;
			AirCPlacement();
			BShipPlacement();
			DestPlacement();
			SubPlacement();
			PTBoatPlacement();
		}
		else if (cMenu == 50)
		{
			Instructions();
		}
		else if (cMenu == 51)
		{
			cout << "Exiting." << endl;
			return 0;
		}
		else
		{
			cout << "Invalid Input, please enter a new number." << endl;
			cout << endl;
		}
	}

	return cMenu;
}

int Instructions()
{
	//The instructions function.
	//Pretty basic, just giving instructions so any new player knows how to play the game
	//Provided a board, with a key showing the different ships
	char cReturn = 'a';
	bool bValue = false;

	cout << "Instructions: " << endl;
	cout << endl;

	cout << "This game is set on two 10x10 grids, one for you, and one for your opponent." << endl;
	cout << "You set your ships on the grid either manually or randomly."  << endl;
	cout << "Once the ships have been set you enter a coordinate, one letter between A-J" << endl;
	cout << "and one number between 1-10" << endl;
	cout << "You will either register a hit 'X' or a miss 'O' depending on where your" << endl;
	cout << "opponent placed their boats." << endl;
	cout << "Your opponent will then have their turn, and try and hit one of your ships." << endl;
	cout << "You will continue to alternate turns until either you, or your opponent" << endl;
	cout << "has hit and sunk all of the ships on their opposing board." << endl;
	cout << endl;
	cout << endl;
	
	//Providing the option to return to the menu, or exit the game completely
	while (bValue == false)
	{
		cout << "Do you wish to: " << endl;
		cout << "1) Return to the menu: ";
		cin >> cReturn;
		cout << endl;

		if (cReturn == 49)
		{
			cout << "You have chosen to return to the menu." << endl;
			Menu();
		}
		else
		{
			cout << "Invalid Input, please enter a new number." << endl;
			cout << endl;
		}
	}

	return cReturn;

}

void AirCPlacement()
{

	cout << "Please enter coordinates for the Aircraft Carrier: " << endl;

	 char cGridLetter = CoordEnterLetter();
	 int iGridNumber = CoordEnterNumber();

	 cout << cGridLetter;
	 cout << iGridNumber << endl;

	char cCoordDirection = DirecCoord();
	GameBoard();

}

void BShipPlacement()
{
	
	cout << "Please enter coordinates for the Battleship: " << endl;

	 char cGridLetter = CoordEnterLetter();
	 int iGridNumber = CoordEnterNumber();

	 cout << cGridLetter;
	 cout << iGridNumber << endl;

	char cCoordDirection = DirecCoord();
	GameBoard();
}

void DestPlacement()
{
	cout << "Please enter coordinates for the Destroyer: " << endl;

	 char cGridLetter = CoordEnterLetter();
	 int iGridNumber = CoordEnterNumber();

	 cout << cGridLetter;
	 cout << iGridNumber << endl;

	char cCoordDirection = DirecCoord();
	GameBoard();
}

void SubPlacement()
{
	cout << "Please enter coordinates for the Submarine: " << endl;

	 char cGridLetter = CoordEnterLetter();
	 int iGridNumber = CoordEnterNumber();

	 cout << cGridLetter;
	 cout << iGridNumber << endl;

	char cCoordDirection = DirecCoord();
	GameBoard();
}

void PTBoatPlacement()
{
	cout << "Please enter coordinates for the Patrol Boat: " << endl;

	 char cGridLetter = CoordEnterLetter();
	 int iGridNumber = CoordEnterNumber();

	 cout << cGridLetter;
	 cout << iGridNumber << endl;

	char cCoordDirection = DirecCoord();
	GameBoard();

}
Maybe you should start practicing with a file system and/or classes? That'd make it easier on you for managing code/finding things faster.
Bad way of programming! (in my opinion)

You methods are not meant to be used like that, you should not have functions that call other functions in chains like that. It makes the code almost impossible to follow when programs get larger. Control of the program should be kept within main, this way it makes it easier to add new functionality. Also, are you familiar with argument passing? I think it would make it much easier for you.

-Blueberry
teste
Yeah I know its pretty bad programming, which is why I need you guys to tell me whats wrong!!

We have covered argument passing in class, but I still haven't fully grasped it. Having the basics of C++ crammed into my head in 3 weeks means some pretty important bits have slipped through the figurative cracks.

kong 288, we haven't learnt either of those in class yet, so chucking them into my code may look a bit suspect.

But thanks for the advice!! I'll start changing my code, certainly going to changing my functions around.
Topic archived. No new replies allowed.