私は迷路ジェネレーターの作成に取り組んでいます。次のような「セル」クラスがあります。
public class Cell {
public boolean northWall;
public boolean southWall;
public boolean eastWall;
public boolean westWall;
public Cell north;
public Cell south;
public Cell east;
public Cell west;
public boolean visited;
public Cell() {
northWall = true;
southWall = true;
eastWall = true;
westWall = true;
visited = false;
}
public boolean hasUnvisitedNeighbors() {
return ((north != null && !north.Visited)
|| (south != null && !south.Visited)
|| (east != null && !east.Visited) || (west != null && !west.Visited));
}
public Cell removeRandomWall() {
List<Cell> unvisitedNeighbors = new ArrayList<Cell>();
if (north != null && !north.Visited)
unvisitedNeighbors.add(north);
if (south != null && !south.Visited)
unvisitedNeighbors.add(south);
if (west != null && !west.Visited)
unvisitedNeighbors.add(west);
if (east != null && !east.Visited)
unvisitedNeighbors.add(east);
if (unvisitedNeighbors.size() == 0) {
return null;
} else {
Random randGen = new Random();
Cell neighbor = unvisitedNeighbors.get(randGen
.nextInt((unvisitedNeighbors.size())));
if (neighbor == north) {
northWall = false;
north.southWall = false;
return north;
} else if (neighbor == south) {
southWall = false;
south.northWall = false;
return south;
} else if (neighbor == west) {
westWall = false;
west.eastWall = false;
return west;
} else if (neighbor == east) {
eastWall = false;
east.westWall = false;
return east;
}
return null;
}
}
}
私のプログラムの迷路は、単純にセルの 2 次元配列です。配列を作成した後、手動で入力し、隣接するセル (北、南、東、西) へのすべての参照を設定します。
私がクリーンアップしようとしているのはremoveRandomWall()です。訪問済みフラグが false に設定されている隣接セルをランダムに選択し、このセルとそれらを接続する隣接セルの両方の壁を削除するとします。
そのため、訪問されていないすべての隣接するセルを考慮し、ランダムに 1 つを選択してから、このセルと隣接するセルの壁を false に設定して、それらの間にパスが存在するようにする必要があります。上記で試してみましたが、非常にぎこちないようです。
誰でも私を助けることができますか?