I'm not sure if I worded that correctly or not, but I'm trying display a 2d vector as a grid - randomly choosing 2 points on the edges/border of the grid (where y=0 and x=0..) and essentially randomly place a common character as a path between those two points. I will show a mockup of what I mean and my approach so far for obtaining this task. My logic could be wrong, but I feel like I may be on to a valid approach.
This is just a sample, but I want
it to randomly do this every time
the function is called. Let '@'
be the starting points.
width
|--------------------------------|
- .............................@....
| .............................#....
| .............................#....
h | @####........................#....
e | ....###......................#....
i | ......#......................#....
g | ......#...................####....
h | ......#########...........#.......
t | ..............#############.......
| ..................................
| ..................................
- ..................................
The class has:
- a private 2d vector of chars.
- default constructor level(int width, height)
- resizes the vector to the correct width and height and fills it all with '.'
This is the function that displays the level to the console:
// Dump level to console
void Level::displayLevel(){
// Iterate the outer vector
for( auto it = levelGrid.begin(); it != levelGrid.end(); it++ ){
// Iterate the inner vector
for( auto itInner = (*it).begin();
itInner != (*it).end();
itInner++ ){
cout << (*itInner);
}
// End of line as we just output a row
cout << endl;
}
}
I have an idea of of to do it, but not exactly sure how to implement it. I explain what I think I should be doing in the comments of the function and will edit it as it comes together.
I have a global random generator: mt19937 mt; And I seed it in main. It's been awhile since i've used a random generator and I forget how the syntax works. I would greatly appreciate the quick tip :)
This is what I'm working on but barely have it started:
void Level::generatePath(){
//randomly choose node locations: (rX,0) and (0,rY)
int rX = //random int between 0 and width-1
int rY = //random int between 0 and height-1
/***
* Lost my train of thought while thinking about the logic :(
*
* set levelGrid[rX][0] = '@'
* set levelGrid[0][rY] = '@'
*
* choose a random adjacent spot (in the direction towards the opposite node)
* from those locations?
*
* repeat?
*
**********/
}
I'm really struggling with this and not exactly sure how or where to start from. I'll try anything that seems like it has potential to work and update this as I progress. Any suggestions will be greatly appreciated. Thanks!