2D Array to create a grid map

How would I create a simple grid-like map using a 2D array, in C++?

Like this
1
2
3
4
5
6
7
8
9
10
11
12
 ————————————————————————  |
 |    |                           
 |        |                       
 ————————————————————————  |                 
 |            |                        
 |     x             |                         
 —————————————————————————|
 |        |                   
 |             |                  
 —————————————————————————|
 |                      |                        
 |—————————————————————————|


The 'x' means that, there's one person. (It needs to be random)
PD: This map is so shitty.
Really appreciate any help to get started, thank you

Last edited on
If its going to be a random size, then you probably want to use vectors. Apart from that, here is an example of how you could do so:
1
2
3
4
5
6
7
8
9
10
11
12
// our grid-like map
std::vector<std::vector<char>> grid;

std::size_t width, height;
// get the width and the height using some random number generator

grid.resize(width); // set the size of the grid
for (auto& col: grid)
    col.resize(height); // set the size of each column in the grid

// Now, we can add things in. For example:
grid[5][3] = 'x'; // you can make this random if you want 


This should give you a start. You'll have to generate the contents of the map yourself, though, but it shouldn't be too hard. Note that the coordinates of the map go from (0,0) to (grid.size() - 1, grid[0].size() - 1). The way I've done it there, all the columns are the same height.
Last edited on
Topic archived. No new replies allowed.