純粋にランダムにするには、空のグリッドと「候補」リスト (これも空) から始めます。
最初のタイルをグリッドの中央に配置し、隣接する各タイルを「候補」リストに配置したばかりのタイルに追加します。次に、各ターン、「候補」リストからランダムなエントリを選択し、そこにタイルを配置します。タイルを配置した場所の隣に隣接する各グリッド位置を見て、空になっているそれぞれについて、次回の「候補」リストに入れます (まだそこにない場合)。
タイル グリッドに穴ができないようにするには、既に埋められている隣接するタイルの数に基づいて、グリッド位置を選択する確率を増やします (したがって、隣接するタイルが 1 つだけ埋められている場合、可能性は低くなります。それらがすべて埋められている場合、非常に高い確率になります)。
擬似コード:
grid = new array[width,height];
candidates = new list();
function place_tile(x,y) {
// place the tile at the given location
grid[x,y] = 1;
// loop through all the adjacent grid locations around the one
// we just placed
for(y1 = y - 1; y1 < y + 1; y1++) {
for(x1 = x - 1; x1 < x + 1; x1++) {
// if this location doesn't have a tile and isn't already in
// the candidate list, add it
if (grid[x,y] != 1 && !candidates.contains(x1,y1)) {
candidates.add(x1,y1);
}
}
}
}
// place the first tile in the centre
place_tile(width/2, height/2);
while (!finished) {
// choose a random tile from the candidate list
int index = rand(0, candidates.length - 1);
// place a tile at that location (remove the entry from
// the candidate list)
x, y = candidates[index];
candidates.remove(index);
place_tile(x, y);
}