CharlesTheZouch
CharlesTheZouch

Reputation: 73

Implementing a 2d array for an object

Creating a crossword puzzle generator. The grid size is chosen by input and the grid will be generated. I'd like the grid to be an object with rows and columns but also a 2d array which will allow me to divide the grid into smaller sections for randomising between blank and numbered squares. I am not sure where to implement it. It has to be a 2d array as I will do dividing and inverting the layout.

Here is my Grid class with some methods. (And the rest)

class Grid 
{
    int rows; //x
    int columns; //y
    Square field;

public:
    void SetXY(int x, int y)
    {
        rows = x;
        columns = y;
        return;
    }
public:
    void DisplaySize()
    {
        cout << "Rows = ", rows, "Columns = ", columns;
    }
};

Upvotes: 1

Views: 47

Answers (1)

Jesper Juhl
Jesper Juhl

Reputation: 31465

The simplest way to implement a 2D array is to use a std::array<std::array<>> or std::vector<std::vector<>> - depending on whether it needs to be a static or dynamically sized array.

But, you can also just use a one dimensional std::array or std::vector and then just get the second dimension by indexing like row*size_of_row+column.

Upvotes: 1

Related Questions