Battleship Final Help

May 29, 2015 at 4:39pm
So my final is due today, it's to create a 2-player game of battleship, and everything was working perfectly except that it doesn't display the hits or misses on the board. I've tried everything and can't seem to get it to work, if someone could take a look at this I'd really appreciate it.
May 29, 2015 at 4:42pm
Could you post your code please (using code tags, [ code ] and [ /code ] without the spaces).
May 29, 2015 at 4:44pm
Trying to do that now, but my code is too long. What should I do
May 29, 2015 at 4:45pm
Can you post the bit of it that should be displaying the hits and misses? Is it a syntax error, or a logic error?
Last edited on May 29, 2015 at 4:45pm
May 29, 2015 at 4:48pm
I believe that it's a logic error. It lets you play through the game and will let you know if you got a hit, but it doesn't display it on the board. working on uploading it.
May 29, 2015 at 4:54pm
This is a part of it, probably not much use but it's tough to find exactly what I'm looking for in a 1000 line code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   cout << "Enter Horizontal Coordinate (0-9)" << endl;
        cin >> ShotH;
        if (grid[ShotV][ShotH] == ship)
        {
            gridshots2[ShotV][ShotH] == hit;
            cout << "Hit at (" << ShotV << "," << ShotH << ")" << endl;
            print_gridshots2(gridshots2);
            hits1++;
        }
        if (grid[ShotV][ShotH] != ship)
        {
            gridshots2[ShotV][ShotH] == missed;
            cout << "Miss at (" << ShotV << "," << ShotH << ")" << endl;
            print_gridshots2(gridshots2);
        }
        
} while(hits1 < 17 || hits2 < 17);
if (hits1 = 17)
{
   cout << Player2 << "Wins!" << endl;
}
May 29, 2015 at 5:01pm
What type is grid and what is ship? Can you post print_gridshots2()?
May 29, 2015 at 5:03pm
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
void print_gridshots2(int gridshots2[10][10])
{
    cout << "  ";
    for(char a = 'A'; a <= 'J'; a++)
    {
        cout << "   " << setw(1) << a;
    }
    cout << endl;
 
    for(int i = 0; i <= 9; i++)
    {
        if(i == 10)
        cout << i;
    else
        cout << " " << i ;
 
        for(int j = 0; j < 10 ; j++)
        {
             if(gridshots2[i][j] == occupied || gridshots2[i][j] == empty){
                cout << setw(4) << " -" ;
        }
        if(gridshots2[i][j] == missed )
        {
                cout << setw(4) << " o";
        }
            if(gridshots2[i][j] == hit )
        {
            cout << setw(4) << " x";
        }
            else if(gridshots2[i][j] == ship)
        {
            cout << setw(4) << " *";
        }
    }
        cout << "\n";
    }
}
Last edited on May 29, 2015 at 5:03pm
May 29, 2015 at 5:06pm
Only two things I can see wrong with that is you have if(i==10) when i can never equal 10. And you are using postfix increments rather than prefix. Also just noticed on the first bit of code you put, line 5 and 12 should use = not ==.
Last edited on May 29, 2015 at 5:07pm
Topic archived. No new replies allowed.