Best way to create a map where the cells have individual properties

Hi!

I and a couple of my friends are quite green but we have decided to expand our knowledge in the C++ language. So we have started a game project.

However, the map will consist of hexagon shaped cells with individual properties. Landscape, current population and so on.

Therefor we thought that it would be good to create a class "Hex" that contain the data members and functions needed for each individual cell.

But, how do we create an n amount of objects with the names hex(0-n)?

Or is there a better way to create the map and still have individual properties?

Thankful for any answer.
we thought that it would be good to create a class "Hex"

I would call it "Cell" as "Hex" can be easily confused with Hexadecimal.

But, how do we create an n amount of objects with the names hex(0-n)?

If you know the length beforehand, then use an array:

1
2
3
4
5
6
7
8
9
const int NUM_CELLS = 100;
Cell cellMap[NUM_CELLS];

void init()
{
     cellMap[0].setLandscape("Green");
     cellMap[1].setCurrentPopulation(42);
     ...
}


and if you don't, use a vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
std::vector<Cell> cellMap; //or vector<Cell*> to avoid pushing copies

void init()
{
     Cell aCell;
     cellMap.push_back(aCell);
     cellMap[0].setLandscape("Pretty");

     Cell anotherCell;
     cellMap.push_back(anotherCell);
     cellMap[1].setCurrentPopulation(1);

     ...
}
Thank you,

Never really used arrays that much. Guess I'll be using them more in the future.

Good suggestion to name change too.

Thank you once again, been bugging me for days.
Topic archived. No new replies allowed.