1

I'm trying to make a simple battle ship game. I'm stuck on trying to randomly place the ships on my board.I have a feeling it is because I should be using a vector instead of an array. but I'm not sure how to create a 2d vector.

Here's what I have so far:

using namespace std;



void clearBoard(const int row, const int col) 
{
    int grid[row][col];
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col;j++) {
            grid[i][j] = 0;
            cout << grid[i][j] << " ";

        }
        cout << endl;
    }
}

void setShips(int max_ships1, int row, int col)
{
    int ship_counter = 0;
    while(ship_counter < max_ships1) {
        int x = rand() % row;
        int y = rand() % col;
        int matrix[x][y]
        if (matrix[x][y] != 1) {
            ship_counter++;
            matrix[x][y] = 1;
            cout << matrix[x][y] << " ";
        }
    cout << endl;
    }
}

int main(int argc, char* argv[])
{

    int _row = atoi(argv[0]);
    int _col = atoi(argv[2]);

    int max_ships;
    if (_row > _col) {
        max_ships = _row;
    }
    else if (_col > _row) {
        max_ships = _col;
    }
    else {
        max_ships = _row;
    }

    cout << "enter the size of the board:";
    cin >> _row >> _col;
    clearBoard(_row, _col);
    setShips(_row,_col,max_ships);





    return 0;

}

If the user decides on a 3x3 board, the first function returns:

0 0 0
0 0 0
0 0 0

I'm hoping to randomly generate 1's to represent a battleship's position. Here's an example on a 3x3 board:

1 0 0
0 1 0
1 0 0

Thanks.

6
  • 1
    int _row = atoi(argv[0]); Did you mean argv[1]? Commented Jul 12, 2015 at 0:10
  • Don't think so. I want the user to enter the board size like "4 4", with a space in between. Commented Jul 12, 2015 at 0:17
  • 2
    This is not how argv represent command line arguments. See e.g. stackoverflow.com/a/3024202. Commented Jul 12, 2015 at 0:26
  • 2d vector: std::vector<std::vector<int>> grid(row, std::vector<int>(col)) Commented Jul 12, 2015 at 1:08
  • argv[0] is the name of your program. argv[1] is the first argument. Commented Jul 12, 2015 at 2:25

2 Answers 2

1

Your main problem is that grid goes out of scope (and is effectively deleted) when clearBoard returns, and matrix (a completely unrelated array to grid) goes out of scope each time through your while loop. That means that you're filling grid with zeroes, then creating a bunch of other arrays with random data and sometimes setting one element to 1. You need to either make grid global, or return it from clearBoard and pass it to setShips as an argument.

Passing around multidimensional raw arrays is kind of complicated, so using a vector might make this a bit easier for you if you don't want to make the array global. To create a two-dimensional vector, you might think that std::vector<std::vector<int>> will work, but that's not quite right. You can do it that way, as mentioned in the comments, but technically, std::vector<std::vector<int>> is what's called a jagged array, meaning that you can have each row be a different length - unlike your raw 2D array, where you know that each row must always be the same length.

The proper way to make a two-dimensional array with a vector is this:

std::vector<int> grid(row * col);
grid[i * row + j] = 1; // Or i * col + j will also work.

As you can see, it's a bit more complicated to get an element out by its (x,y) coordinates. It doesn't matter which of the two calculation methods you use, but in one version i is x and j is y, while in the other, the reverse is true (i is y, etc).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I just have one question about implementation. Once I declare std::vector<int> grid(row * col);, should set grid[i * row + j] = 1; in setShips and grid[i * row + j] = 0; in clearBoard? Sorry to ask, grabbing the elements is slightly confusing to me.
Pretty much, but don't forget that you need to do that on the same object, unlike in the code you posted (as my first paragraph says).
When I declared std::vector<int> grid(row * col); in my function, it says that vector of type <int> does not have a call operator. I tried changing the return type of the function from void to int, but still the same error. I tried declaring the array global, but I get the error: Floating point exception: 8. Very sorry to bother you like this but I'm new to programming and don't really have anyone that can help me.
You must be doing something else wrong, because that statement std::vector<int> grid(row * col); is correct and has nothing to do with call operators.
0

You can also set up a 2D vector like this :

#include<vector>
#include<iostream>
using namespace std;

int main()
{

    std::vector< std::vector<int> > grid2D;

    //m * n is the size of the grid

    int m = 10;
    int n = 10;

    //Grow rows by m
    grid2D.resize(m);

   //Grow Columns by n
    for(int i = 0 ; i < m ; ++i) grid2D[i].resize(n);

  // print out 2D grid on screen
    for(i = 0 ; i < m ; ++i){
        for(int j = 0 ; j < n ; ++j){       

            grid2D[i][j]=0;
            cout<<""<<grid2D[i][j]<<" ";

        }
        cout<<"\n";

    }
    cout<<"\n";
    return 0;
}    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.