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