Battleship random ship placement

For a school assignment I have to create a battleship game where one random 4 length battleship (horizontal or vertical) is generated in a 8x8 gameboard. The player has 15 torpedoes to try and sink the ship. I used a 2D vector and have finished most of the code:

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
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

using namespace std;

void generateship(vector<vector<int> >&field);
void fire(vector<vector<int> >&field);
void display(const vector<vector<int> >field);

int main()
{
	srand(time(0));

	vector<vector<int> >field(8);
	for (int x = 0; x < field.size(); x++)
		field[x].resize(8);
	for (int x = 0; x < field.size(); x++)
		for (int y = 0; y < field[y].size(); y++)
			field[x][y] = 0;

	generateship(field);
	fire(field);


	system("pause");

	return 0;
}
void generateship(vector<vector<int> >&field)
{
	int row1 = rand() % 8;
	int col1 = rand() % 8;
	do 
	{
		int row2 = rand() % 3 + (row1 - 1);
		int col2 = rand() % 3 + (col1 - 1);
	} while (row2 != row1 && col2)
	int col3 = rand()
	display(field);
}
void fire(vector<vector<int> >&field)
{
	int row, col;
	int torps = 15;
	int hitcounter = 0;
	while (hitcounter != 4 || torps != 0)
	{
		cout << torps << " torpedoes remain. Fire where? ";
		cin >> row >> col;
		switch (field[row][col])
		{
		case 0: cout << "Miss!" << endl << endl;
			field[row][col] = 2;
			break;
		case 1: cout << "Hit!" << endl << endl;
			field[row][col] = 3;
			hitcounter = hitcounter + 1;
			break;
		case 2: cout << "Missed again!" << endl << endl;
			break;
		case 3: cout << "Hit again!" << endl << endl;
			break;
		}
		torps = torps - 1;
		display(field);
	}
	if (hitcounter == 4)
		cout << "You win!";
	else if (torps == 0)
		cout << "You are out of torpedoes! Game over.";
}
void display(const vector<vector<int> >field)
{
	for (int row = 0; row < 8; row++)
	{
		for (int col = 0; col < 8; col++)
		{
			switch (field[row][col])
			{
			case 0:		cout << ". ";
				break;
			case 1:		cout << ". ";
				break;
			case 2:		cout << "X ";
				break;
			case 3:		cout << "O ";
				break;
			}
		}
		cout << endl;
	}
}


As you can probably see I am struggling with the "generateship" function. My goal is to generate RANDOMLY one 4x1 ship (horizontal or vertical) that is completely within my 8x8 2D vector. Any advice/help/comments appreciated!
Last edited on
I would generate first the direction (vertical or horizontal). Then I generate the beginning (x and y) of the battleship. I assume that either x or y increases by 4. So if you get that the battleship is vertical, you can have maximum y for the beginning of the battleship only 4 (y=4,5,6,7), while x is between 0 and 7. If the ship is horizontal, x is between 0 and 4, y is between 0 and 7. So all you need is 3 random numbers.
Topic archived. No new replies allowed.