0

TL;DR タイルマップ内のすべてのタイルにランダムにアクセスする

タイルのレイヤー全体 (10x10 のみ) を塗りつぶしてから、タイルをランダムに削除する}}のforようなループを実行することで、タイルのランダムな位置を生成する方法があります。for (int x = 0; x < 13; x++) { for (int y = 0; y < 11; y++)また、これには約 30 の上限があります。問題は、ループが実行されると、左側の上限が使い果たされることです ( x=0 y=0 で開始し、 x0 x1 x2 x3 を実行するためです。 ..)。座標をランダムに生成しようとしましたが、すべての座標を超えていないため、これは機能しませんでした。

マップ内のすべての座標をランダムな順序でスキャンするためのより良い方法を知っている人はいますか?

4

2 に答える 2

1

これを達成するためのベストプラクティスは、二重ハッシュを使用することだと思います。詳細については、次のリンクを参照してください: Double Hashing。簡単に説明してみます。

事前に計算する必要がある 2 つの補助ハッシュ関数があります。そして、forループで実行されるメインのハッシュ関数。これをテストしてみましょう (これは疑似コードになります):

key = random_number() //lets get a random number and call it "key"
module = map_size_x // map size for our module

//form of hash function 1 is: h1(key) = key % module, lets compute the hash 1 for our main hash function
aux1 = key % module
//form of hash function 2 is: h2(key) = 1 + (key % module'), where module' is module smaller for a small number (lets use 1), lets compute it:
aux2 = 1 + (key % (module - 1))

//the main hash function which will generate a random permutation is in the form of: h(key, index) = (h1(key) + index*h2(key)) % module. we already have h1 and h2 so lets loop this through:
for (i = 0; i < map_size_x; i++)
{
     randomElement = (aux1 + i*aux2) % module //here we have index of the random element
     //DO STUFF HERE
}

別の順列を取得するには、 の値を変更するだけですkey。詳細については、リンクを確認してください。

お役に立てれば。乾杯。

于 2013-05-29T11:55:11.147 に答える
1

Number your tiles 0 to n anyway you want. Create a NSMutableIndexSet, and add all to it. Use a random number generator scaled to the number of items still in the index set (actually the range of first to last), grab one, then remove it from the set. If the random number is not in the set generate a new be, etc, until you find one in the set.

于 2013-05-29T11:42:54.287 に答える