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();
}
}
}
}
내가 밖으로 인쇄 할 때 나는 "#"(첫 번째 위치에 "-"가있는) 미로를 얻습니다. 즉, 미로가 생성되지 않았습니다. 올바른 길. 하지만 왜 작동하지 않는지 나는 알 수 없습니다. 나는 그것이 CurrentCell 변수와 관련이 있다고 생각하지만, 나는 확신 할 수 없다. 아무도 내 오류를 찾아 내도록 도울 수 있습니까? 나는 그것을 찾으려고했지만 아무 소용이 없습니다. 매우 감사!문제 DFS 미로 만들기
'String Maze [] [] = 새로운 문자열 [20] [20]'을'char char [] [] = new char [20] [20]'로 바꾼다 :'... == '...'대신에 '#''... equals ("#") 대신 –
명명 규칙을 지켜보십시오 ... 변수의 이름을 지정할 때 첫 번째 단어는 대문자로 표시되지 않으며 이후 단어가 있습니다. 또한'final' (명시 적으로'NORTH','SOUTH','EAST' 및'WEST' 변수 참조)로 선언하지 않는 한 변수 이름의 모든 문자를 대문자로 사용하지 마십시오. – fireshadow52