Professional Documents
Culture Documents
import java.util.*;
public class Maze {
private int rows;
private int columns;
private Square[][] elements;
private Square goal;
private static final String CLOSED_TOP = "+ - ";
private static final String OPEN_TOP = "+ ";
private static final String CLOSED_LEFT = "| ";
private static final String CLOSED_LEFT_PATH = "| . ";
private static final String CLOSED_LEFT_START = "| S ";
private static final String CLOSED_LEFT_GOAL = "| G ";
private static final String OPEN_LEFT = " ";
private static final String OPEN_LEFT_PATH = " . ";
private static final String OPEN_LEFT_START = " S ";
private static final String OPEN_LEFT_GOAL = " G ";
private List<Square> opened = new ArrayList<Square>();
private List<Square> closed = new ArrayList<Square>();
private List<Square> bestList = new ArrayList<Square>();
public Maze(int rows, int columns) {
this.rows = rows;
this.columns = columns;
elements = new Square[rows][columns];
init();
}
private void init() {
createSquares();
setStartAndGoal();
generateAdjacenies();
}
public int getRows() {
return rows;
}
public int getColumns() {
return columns;
}
private void setStartAndGoal() {
elements[0][0].setStart(true);
Random random = new Random();
int goalX = 0, goalY = 0;
while (goalX == 0 && goalY == 0) {
goalX = random.nextInt(rows);
goalY = random.nextInt(columns);
}
goal = elements[goalX][goalY];
goal.setEnd(true);
}
private void generateAdjacenies() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
elements[i][j].calculateAdjacencies();
}
}
}
private void createSquares() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
elements[i][j] = new Square(i, j, this);
}
}
}
public Square getSquare(int x, int y) {
return elements[x][y];
}
public void setSquare(Square square) {
elements[square.getX()][square.getY()] = square;
}
public void draw() {
System.out.println("Drawing maze");
drawContents();
drawBorder();
}
private void drawContents() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Square square = elements[i][j];
drawTop(square);
}
System.out.println("+");
if (closed.contains(neighbor)) {
Square tmpSquare = new Square(neighbor.getX(),
neighbor.getY(), this);
tmpSquare.setParent(best);
if (tmpSquare.getPassThrough(goal) >= neighbor.getPassThrough(goal)) {
continue;
}
}
neighbor.setParent(best);
opened.remove(neighbor);
closed.remove(neighbor);
opened.add(0, neighbor);
}
}
}
System.out.println("No Path to goal");
}
private void populateBestList(Square square) {
bestList.add(square);
if (square.getParent().isStart() == false) {
populateBestList(square.getParent());
}
return;
}
private Square findBestPassThrough() {
return parent;
}
public void setParent(Square parent) {
this.parent = parent;
}
public void calculateAdjacencies() {
int top = x - 1;
int bottom = x + 1;
int left = y - 1;
int right = y + 1;
if (bottom < maze.getRows()) {
if (isAdjacent()) {
maze.getSquare(bottom, y).addAdjacency(this);
this.addAdjacency(maze.getSquare(bottom, y));
}
}
if (right < maze.getColumns()) {
if (isAdjacent()) {
maze.getSquare(x, right).addAdjacency(this);
this.addAdjacency(maze.getSquare(x, right));
}
}
}
public void addAdjacency(Square square) {
adjacencies.add(square);
}
public void removeAdjacency(Square square) {
adjacencies.remove(square);
}
public double getPassThrough(Square goal) {
if (isStart()) {
return 0.0;
}
return getLocalCost(goal) + getParentCost();
}
public double getLocalCost(Square goal) {
if (isStart()) {
return 0.0;
}
localCost = 1.0 * (Math.abs(x - goal.getX()) + Math.abs(y - goal.getY()));
return localCost;
}
public double getParentCost() {
if (isStart()) {
return 0.0;
}
if (parentCost == 0.0) {
parentCost = 1.0 + .5 * (parent.getParentCost() - 1.0);
}
return parentCost;
}
public boolean isAdjacent() {
if (Math.random() > .5) {
return true;
}
return false;
}
}
SAMPLE OUTPUT:
Drawing maze
+-+-+-+-+-+-+-+-+-+-+
|S| | | | | |
+ + +-+ + + + +-+-+-+
| | G| |
+-+ +-+ +-+ +-+-+ + +
| | | | |
+ +-+-+-+ + + + + + +
| | | | | | | |
+-+ + +-+ +-+ +-+-+ +
| | | | | | |
+ +-+-+-+ + +-+ + +-+
| | | | | |
+-+-+ + + +-+ + +-+ +
| | | | |
+ + +-+-+-+ + + + +-+
| | | | |
+-+ + + + + +-+-+-+ +
| | | | |
+-+ + +-+-+-+-+-+ + +
| | | | |
+-+-+-+-+-+-+-+-+-+-+
Calculating best path...
Found Goal
Drawing maze
+-+-+-+-+-+-+-+-+-+-+
|S| |. . | | | |
+ + +-+ + + + +-+-+-+
| . . . . .| . . . G| |
+-+ +-+ +-+ +-+-+ + +
| | | | |
+ +-+-+-+ + + + + + +
| | | | | | | |
+-+ + +-+ +-+ +-+-+ +
| | | | | | |
+ +-+-+-+ + +-+ + +-+
| | | | | |
+-+-+ + + +-+ + +-+ +
| | | | |
+ + +-+-+-+ + + + +-+
| | | | |
+-+ + + + + +-+-+-+ +
| | | | |
+-+ + +-+-+-+-+-+ + +
| | | | |
+-+-+-+-+-+-+-+-+-+-+