это моя первая попытка в Java, поэтому я решил написать алгоритм генерации лабиринта. Я уже написал это на C #, поэтому подумал, что было бы удобно написать что-то, что я делал раньше. Я использую Eclipse для написания и компиляции Java.
После того, как я закончил писать свою программу, и она не дала мне никаких ошибок или чего-либо еще, я попытался запустить ее, но она выдает странный вывод в консоли: он говорит: at helloWorld.mazeAlgorithm(helloWorld.java:52)
столько раз, сколько Высота и ширина лабиринта, который я пытаюсь создать. Я понятия не имею, что делать, и я также не мог ничего найти в Интернете.
(и я знаю, что мой код, вероятно, действительно плохой, но я спешил, и я просто хотел посмотреть, смогу ли я что-нибудь написать быстро.)
Вот оно:
import java.applet.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class helloWorld extends Applet
{
final int horizontalSize = 10;
final int verticalSize = 10;
final int LEFT = 1;
final int UP = 2;
final int RIGHT = 3;
final int DOWN = 4;
ArrayList<cell> visitedCells;
public void paint(Graphics g)
{
initialize(g);
cell startCell = new cell();
startCell.x = 0;
startCell.y = 0;
mazeAlgorithm(startCell, g);
}
public void initialize (Graphics g)
{
for (int x = 0; x< horizontalSize*10+1; x=x+10)
{
g.drawLine(x, 0, x, verticalSize*10);
}
for (int y = 0; y < verticalSize*10+1; y=y+10)
{
g.drawLine(0, y, horizontalSize*10, y);
}
visitedCells = new ArrayList<cell>();
cell currentCell = new cell();
currentCell.x = 0;
currentCell.y = 0;
visitedCells.add(currentCell);
}
public void mazeAlgorithm(cell currentCell, Graphics g)
{
ArrayList<cell> neighbourCells = getUnvisitedNeighbourCells(currentCell);
while(neighbourCells.size()> 0)
{
cell nextCell = neighbourCells.get(randomNumber(neighbourCells.size()-1));
removeWall(nextCell, g);
visitedCells.add(nextCell);
mazeAlgorithm(nextCell, g);
neighbourCells = getUnvisitedNeighbourCells(currentCell);
}
return;
}
public ArrayList<cell> getUnvisitedNeighbourCells(cell cellToCheck)
{
ArrayList<cell> unvisitedNeighbourCells = new ArrayList<cell>();
cell tempCell = new cell();
tempCell.x = cellToCheck.x-1;
tempCell.y = cellToCheck.y;
tempCell.direction = LEFT;
if (cellToCheck.x > 0 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
tempCell.x = cellToCheck.x;
tempCell.y = cellToCheck.y-1;
tempCell.direction = UP;
if (cellToCheck.y > 0 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
tempCell.x = cellToCheck.x+1;
tempCell.y = cellToCheck.y;
tempCell.direction = RIGHT;
if (cellToCheck.x < horizontalSize-1 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
tempCell.x = cellToCheck.x;
tempCell.y = cellToCheck.y+1;
tempCell.direction = DOWN;
if (cellToCheck.y < verticalSize-1 && !visitedCells.contains(tempCell))
unvisitedNeighbourCells.add(tempCell);
return unvisitedNeighbourCells;
}
public void removeWall(cell Cell, Graphics g)
{
g.setColor(Color.WHITE);
switch(Cell.direction)
{
case LEFT:
g.drawLine(Cell.x*10, Cell.y*10-10, Cell.x*10, Cell.y*10);
break;
case UP:
g.drawLine(Cell.x*10-10, Cell.y*10, Cell.x*10, Cell.y*10);
break;
case RIGHT:
g.drawLine(Cell.x*10-10, Cell.y*10-10, Cell.x*10-10, Cell.y*10);
break;
case DOWN:
g.drawLine(Cell.x*10-10, Cell.y*10-10, Cell.x*10, Cell.y*10-10);
break;
}
return;
}
private int randomNumber(int max)
{
Random random = new Random();
return random.nextInt(max);
}
}
class cell
{
public int x;
public int y;
public int direction;
}
Заранее большое спасибо!