-1

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!

4

1 回答 1

0

试试这样的东西,它现在不会编译,但经过一些抛光它会给你你需要的东西。(你需要注意不要超越国界)

bool Level:getNext(int X, int Y, pair<int, int> target, vector<pair<int,int>> &path, vector<vector<bool>> &used) {
  path.emplace_back(X,Y);
  used[X][Y] = true;
  if(target.first == X && target.second == Y) return true;
  vector<int> directions = {1,2,3,4};
  for(int i = 1; i <= 4; ++i) {
     int rand = // random from 0 to 4-i;
     int dir = directions[rand];
     int x=X, y=Y;
     directions.erase[rand];
     if (dir == 1) x = X+1;
     else if (dir == 2) x = X-1;
     else if (dir == 3) y=Y+1;
     else if (dir == 4) y=Y-1;
     if (!used[x][y])
        if (getNext(x, y, target, path, used)) return true;
  }  
  path.pop_back();
  used[X][Y] = false;
  return false;
}
于 2013-03-09T00:26:35.203 回答