import java.util.*;
public class MazeGenerator
{
public void init()
{
String Maze[][] = new String [20][20];
for (int i =0; i <20; i++) {
for (int j = 0; j < 20; j++) {
Maze[i][j] = "#";
}
}
generate(Maze);
for (int i =0; i <20; i++) {
for (int j = 0; j < 20; j++) {
System.out.print(" " + Maze[i][j]);
}
System.out.println("");
}
}
public void generate (String Maze[][])
{
Stack <String> CellStack = new Stack<String>();
int TotalCells = Maze.length * Maze.length;
int x = 10, y = 10;
String CurrentCell = Maze[x][y];
Maze[x][y] = "-";
CellStack.push(CurrentCell);
int VisitedCell = 1;
boolean EastT, WestT, NorthT, SouthT;
while(VisitedCell < TotalCells)
{
String EAST = Maze[x+1][y];
String WEST = Maze[x-1][y];
String NORTH = Maze[x][y+1];
String SOUTH = Maze[x][y-1];
if(EAST == "#")
EastT = true;
else
EastT = false;
if(WEST == "#")
WestT = true;
else
WestT = false;
if(NORTH == "#")
NorthT = true;
else
NorthT = false;
if(SOUTH == "#")
SouthT = true;
else
SouthT = false;
if(WestT == true || EastT == true || NorthT == true || SouthT == true)
{
double Random = (int) (Math.random() * 4) + 1;
switch ((int) Random)
{
case 1:
if(EastT == true){
CurrentCell = EAST;
break;
}
else
break;
case 2:
if(WestT == true){
CurrentCell = WEST;
break;
}
else
break;
case 3:
if(NorthT == true){
CurrentCell = NORTH;
break;
}
else
break;
case 4:
if(SouthT == true){
CurrentCell = SOUTH;
break;
}
else
break;
}
CurrentCell = "-";
CellStack.push(CurrentCell);
VisitedCell++;
}
else
{
CurrentCell = CellStack.pop();
}
}
}
}
私がそれを印刷すると、迷路が作成されていないことを意味する "#"がその中にあります(最初の場所に " - "が1つあります)正しい方法。しかし、私はそれが動作しない理由を見ることができません。私はそれがCurrentCell変数と関係があるかもしれないと思うが、私は確信が持てない。誰もが私のエラーを見つけるのを助けることができる、私はそれを見つけようとしているが、役に立たない。とても有難い!DFSメイズを作成するときのトラブル
'String Maze [] [] =新しい文字列[20] [20]'を 'char 'に変更する[Maze [] [] = new char [20] [20]': '... == '...'の代わりに '#' 'を使用します。 –
命名規則を見る...変数に名前を付けるとき、最初の単語は大文字ではなく、その後の単語があります。また、 'NORTH'、' SOUTH'、 'EAST'、' WEST'変数を参照して明示的に宣言されていない限り、変数名のすべての文字を大文字にしないでください。 – fireshadow52