Battleship program, should not hit same location twice

Write your question here.

Any help would be appreciated. I know the code is not very well put together, but I've been working on this for about 4 hours now and I'm about as scrambled as my code is.

Not sure how much anyone will be able to help with this as it requires a driver to run it. However I'm pretty desperate at this point.

Essentially I have it working to the point where if my shot misses, I select a random location to fire on within the grid (1-10 for columns A-J for rows). If my last shot was a hit then I check the location and if it's less than the last column I add a column and fire in the same row. If it's more than the last column I subtract a column and fire.

The part I'm having trouble with is not shooting in the same location twice. I've declared an array "int arrayShots[BS_GRID_ROW][BS_GRID_COLS]" which holds the elements for rows and columns. I'm not sure how/when to read in my shots to the array, and how to then search them to check for duplicates. I have a bool named duplicate to set if true, and if not true then I'll take the shot.

Again thanks for any assistance with this.



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
  // Every student will create their own file to contain the Battle Ship
// functions. A basic skeleton of this file follows.
// NOTE: if you wish to use the rand() function, do NOT call srand()--it is
//   already being called by the main() program driver.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "battleship.h"
#include <cmath>

using namespace std;
// Sample skeleton for attack function
// Change the name to reflect your own name
int
Attack(int mode, int opponent)
{
    int column, ret;
    bool duplicate;
    char row;
    static char prerow;
    static int precol;
    int arrayShots[BS_GRID_ROWS][BS_GRID_COLS];
// You will probably need static variables here to keep track of status,
// Such as where you fired upon already, what ships were hit, etc.

    switch(mode) {
    case BS_MODE_NEW_GAME:
        // initialize static data structures
        prerow = 'B';
        precol = 5;
        duplicate = false;

        break;
    case BS_MODE_CONTINUE_GAME:
// Student must create some algorithm to decide what row and column to fire
//   on, then call exactly as follows.
        // Search arrayShots for duplicate of row/shot
        // Put random generate in while loop that loops while duplicate is found within arrayShots
        if (!(ret & BS_SHIP_HIT)){
            row = (rand() % 10) + 65;
            column = (rand() % 10) + 1;
            ret = fire[opponent](row, column);
        }


//  If you hit the opponent, you must look at the integer return code
//  here to help decide where to attack next!
        if (ret & BS_SHIP_HIT){
        // do something
            prerow = row;
            precol = column;
            if (column < BS_GRID_COLS)
                column += 1;
            else
                column -= 1;
        ret = fire[opponent](row, column);
        }



        break;
    }
    return 0;
}

// Rename the attack function by replacing lastName above with yours.
// Make sure you replace the function name in the line below as well.
// Then insert your name in the last line.
int (*battleship[MaxPlayerCount])(int, int) = {NULL, Attack};
int (*fire[MaxPlayerCount])(char, int)= {NULL, incomingStub};
char const *playerName[MaxPlayerCount] = {"", ""};
Last edited on
I've overhauled my code a bit for function more properly, the above code wasn't actually really working as intended at all.

Now I have it working properly, it checks for misses, hits, and sinks. Now I just need to understand how to handle duplicate values. I believe I need to store each row/column for every shot in my arrayShots[BS_GRID_ROWS][BS_GRID_COLS], just prior to taking my shot each time. Then I need to write some code in each if statement that will loop through the array and see if the shot generated by the if statement is already in the array. If it is, it will loop again.

I guess my question is, how do I add the values of the shot to the array, and how would I search for both row and column within the array?

I was thinking of adding arrayShots[row][column]; to the end of my code, but I'm uncertain if thats even adding the values into the array or not? If it is, then I supposed I'll needed nested for loops within a while loop for each of my if statements?

Sorry if I'm rambling or way off base, my brain is fried.

Here is what i have now:

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
// Every student will create their own file to contain the Battle Ship
// functions. A basic skeleton of this file follows.
// NOTE: if you wish to use the rand() function, do NOT call srand()--it is
//   already being called by the main() program driver.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "battleship.h"
#include <cmath>

using namespace std;
// Sample skeleton for attack function
// Change the name to reflect your own name
int
Attack(int mode, int opponent)
{
    int column, ret;
    bool duplicate;
    char row;
    static char prerow;
    static int precol;
    int arrayShots[BS_GRID_ROWS][BS_GRID_COLS];
// You will probably need static variables here to keep track of status,
// Such as where you fired upon already, what ships were hit, etc.

    switch(mode) {
    case BS_MODE_NEW_GAME:
        // initialize static data structures
        prerow = 'B';
        precol = 5;
        duplicate = false;

        break;
    case BS_MODE_CONTINUE_GAME:
// Student must create some algorithm to decide what row and column to fire
//   on, then call exactly as follows.
        // Search arrayShots for duplicate of row/shot
        // Put random generate in while loop that loops while duplicate is found within arrayShots
        if (!(ret & BS_SHIP_HIT)){
            row = (rand() % 10) + 65;
            column = (rand() % 10) + 1;

        }



//  If you hit the opponent, you must look at the integer return code
//  here to help decide where to attack next!
        if (ret & BS_SHIP_HIT){
            if (precol < BS_GRID_COLS)
                column = precol + 1;
            else
                column = precol - 1;
            row = prerow;
        }

        if (ret & BS_SHIP_SANK){
            row = (rand() % 10) + 65;
            column = (rand() % 10) + 1;
        }
        ret = fire[opponent](row, column);
        prerow = row;
        precol = column;

        break;
    }
    return 0;
}

// Rename the attack function by replacing lastName above with yours.
// Make sure you replace the function name in the line below as well.
// Then insert your name in the last line.
int (*battleship[MaxPlayerCount])(int, int) = {NULL, Attack};
int (*fire[MaxPlayerCount])(char, int)= {NULL, incomingStub};
char const *playerName[MaxPlayerCount] = {"", ""};
Last edited on
Topic archived. No new replies allowed.