battleship program logic issue

Hello, my program I am working on currently is a battleship program. The program runs and I have it display the grid with the answers for now because the problem is that the program does not display if a ship is hit or not. Here is the code so far... I believe it is a logic error, any help is much appreciated. The user should guess where the coordinates are, enter x y respectively.

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

const int rows = 10;
const int elements = 10;

int maxships = 5;

int matrix[rows][elements];
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;
}

void Clear()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
matrix[i][j] = 0;
}
}
}

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

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++;
}
}

return c;
}

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;
}
}
}

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

int main()
{
srand(time(NULL));
Clear();
SetShips();
displayMessage();
Show();
int pos1 = 0;
int pos2 = 0;
char prompt;
while (1)
{
cout << "Please input location(X, Y): ";
cin >> pos1 >> pos2;
if (Attack(pos1, pos2) == true)
{
cout << "You got me! :)" << endl;
}
else
{
cout << "Sorry no ship at that position!" << endl;
}
cout << "Number of ships left: " << NumberOfShips() << endl;
cout << "Do you want to surrender (y/n)? "; 
cin >> prompt;
if (prompt == 'y')
break;
if (NumberOfShips() == 0)
{
cout << "Congratulations, you won the game!" << endl;
}
}
cout << "Game over!" << endl;
Show();
system("pause");
return 0;
}
Last edited on
Has nobody told you about code tags? http://www.cplusplus.com/forum/articles/16853/
No, I tried using the code format button, but nothing happened. Can I manually add the code tags?
the program does not display if a ship is hit or not

Please be more specific in the statement of your problems. Your program displays "Sorry no ship at that position!" just fine for me. Is that what you mean by display? Or by display do you mean that your 10 by 10 grid printing should change? You never re-draw your grid, so how could it change?

If I have to guess, what I think you meant to say is, "Even when I type in the correct coordinate, it say I missed". This is because you are getting your x and y coordinates confused.



If your grid looks like this:
1
2
3
4
5
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
...


The 1 is at index [2][0]. A 2D array can be thought of as an "array of arrays", meaning your array actually looks like this:
1
2
3
4
5
{ { 0 0 0 0 0 0 0 0 0 0 },
  { 0 0 0 0 0 0 0 0 0 0 },
  { 1 0 0 0 0 0 0 0 0 0 },
  { 0 0 0 0 0 0 0 0 0 0 },
  {         ...         }  } 


Where you can think of the indices being [row_number][col_number]. This more accurately corresponds to [y][x], not [x][y].

1
2
3
4
Please input location(X, Y): 2 0
You got me! :)
Number of ships left: 4
Do you want to surrender (y/n)?  


So perhaps you meant
1
2
3
cout << "Please input location(X, Y): ";
cin >> pos1 >> pos2;
if (Attack(pos2, pos1) == true)


Not sure if that fully solves your problem though, because I'm not exactly sure what your problem is.

Also,
1
2
3
4
5
6
7
8
9
10
11
12
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++;
}
}

You never return anything from this function.

Please turn on compiler warnings. On g++ (GCC), this is the -Wall compiler flag. For visual studio, see https://msdn.microsoft.com/en-us/library/thxezb7y.aspx
Last edited on
Sorry for the lack of explanation, Ganado. You were right, I meant it missed even if the correct coordinates were typed. Thanks for the explanation.
Yep, no problem. Happy coding!
Topic archived. No new replies allowed.