updating grid in battleships

Hello all, I have another question today on my console battleship program. The game displays a sample 10x10 grid and allows the user to enter x,y coordinates to guess the location of the 5 battleships. The program runs and does as it should, although I would like to know how I could update the grid, to show the user that the ship has been hit. Thanks.

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
/* Joe McMahon  C++ 4 Battleship*/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <time.h>

using namespace std;

//define constants
const int rows = 10;
const int elements = 10;

//num of total ships
int maxships = 5;

//define array
int matrix[rows][elements];
char grid[10][10];

//*****function definitions*****
void Clear()
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < elements; j++)
		{
			matrix[i][j] = 0;
		}//end for
	}//end for
}//end of clear

void printGrid() {
	// print the current grid
	cout << endl;
	for (int x = 0; x<10; x++) {
		for (int y = 0; y<10; y++) {
			cout << setw(2) << grid[x][y];
		}
		cout << endl;
	}
	cout << endl;
}//end of printGrid

void createGrid() {
	// create a blank grid   
	for (int x = 0; x<10; x++) {
		for (int y = 0; y<10; y++) {
			grid[x][y] = '0';
		}
	}
}//end of createGrid

void displayMessage()
{
	cout << endl << "Welcome to Battleships!" << endl;
	cout << endl << "The rules are simple:" << endl;
	cout << endl << "You have to guess the location of all the ships." << endl;
	cout << endl << "Type the x and y coordinates, starting with 0." << endl;
	cout << endl << "Below is a sample of the grid, choose your coordinates." << endl;
	createGrid();
	printGrid();
}//end of displayMessage

void resultsMessage()
{
	cout << endl << "Thanks for playing!" << endl;
	cout << endl << "The number 1 indicates the ships that are still standing." << endl;
	cout << endl << "The number 2 indicates the ships you hit!" << endl;
}//end of resultsMessage

void Show()
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < elements; j++)
		{
			cout << matrix[i][j] << " ";
		}//end for
		cout << endl;
	}//end for
}//end of show

int NumberOfShips()
{
	int c = 0;

	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < elements; j++)
		{
			if (matrix[i][j] == 1)
				c++;
		}//end for
	}//end for

	return c;
}//end of NumberOfShips

void SetShips()
{
	int s = 0;
	while (s < maxships)
	{
		int x = rand() % rows;
		int y = rand() % elements;
		if (matrix[x][y] != 1)
		{
			s++;
			matrix[x][y] = 1;
		}//end if
	}//end while
}//end of SetShips

bool Attack(int x, int y)
{
	if (matrix[x][y] == 1)
	{
		matrix[x][y] = 2;
		return true;
	}//end if
	return false;
}//end of Attack

int main()
{
	//call functions
	srand(time(NULL));
	Clear();
	SetShips();
	displayMessage();

	//variables
	int pos1 = 0;
	int pos2 = 0;
	char choice;
	char prompt;

	//begin while
	while (1)
	{
		//ask for input
		cout << "Please input location(x, y): ";
		cin >> pos1 >> pos2;

		//begin if
		if (Attack(pos2, pos1) == true)
		{
			cout << "You sunk the battleship! :)" << endl;
		}
		else
		{
			cout << "Sorry, no ship at that position!" << endl;
		}//end
		cout << "Number of ships left: " << NumberOfShips() << endl;
		if (NumberOfShips() == 0)
		{
			cout << "Congratulations, you won the game!" << endl;
		}//end if
		cout << "Do you want to surrender (y/n)? ";
		cin >> prompt;
		if (prompt == 'y')
			break;
		//end if
	}//end while
	cout << endl << "Game over!" << endl;
	cout << endl << "Key:" << endl;
	Show();
	resultsMessage();
	
	//close
	system("pause");
	return 0;
}//end of main 
Last edited on
In your Attack function you actually update the grid - line 118,
so you need only to display the grid again.
Okay, thanks for the clarification. I will work on that today and see if I can finish it. :)
Topic archived. No new replies allowed.