-1

私がそれを正しく表現したかどうかはわかりませんが、2d ベクトルをグリッドとして表示しようとしています - グリッドのエッジ/境界上の 2 つの点をランダムに選択します (ここで y=0 および x=0..)そして基本的に、共通の文字をそれらの 2 点間のパスとしてランダムに配置します。私が意味することと、このタスクを達成するためのこれまでのアプローチのモックアップを示します。私の論理は間違っている可能性がありますが、有効なアプローチを取っているように感じます.

               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  |   ..............#############.......
           |   ..................................
           |   ..................................
           -   ..................................

クラスには次のものがあります。

  • char のプライベート 2d ベクトル。
  • デフォルトのコンストラクター レベル (int 幅、高さ)
    • ベクターのサイズを正しい幅と高さに変更し、すべてを「.」で埋めます。

これは、コンソールにレベルを表示する関数です。

// 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;
        }
}

私はそれを行うという考えを持っていますが、それを実装する方法が正確にはわかりません。関数のコメントで何をすべきだと思うかを説明し、まとまり次第編集します。

私はグローバル乱数発生器を持っています: mt19937 mt; そして、私はそれをメインに種付けします。乱数発生器を使用してからしばらく経ちましたが、構文がどのように機能するかを忘れています。簡単なヒントをいただければ幸いです:)

これは私が取り組んでいるものですが、ほとんど開始していません:

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?
     *
     **********/

}

私はこれに本当に苦労しており、どのように、またはどこから始めればよいか正確にはわかりません。うまくいく可能性があると思われるものは何でも試し、進行するにつれてこれを更新します. どんな提案でも大歓迎です。ありがとう!

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 に答える