📄 mazebuilder.java
字号:
package istarion.core;
import java.util.*;
import istarion.frontend.Toolkit;
/**
* Implementation of an algorithm to create
* random generated mazes.
*/
public class MazeBuilder
{
private char[][] __maze = null;
private int __dungeonSizeX = 0;
private int __dungeonSizeY = 0;
private int __currentX = 0;
private int __currentY = 0;
private int __currentFacing = Dungeon.NOWHERE;
private int __recursions = 0;
private boolean[][] __environment = null;
private Vector __tmpList = null;
private Vector __dirList = null;
private Random __rnd = null;
/**
* Starts maze generating algorithm.
* @param maze the array which holds the maze.
* @param startX the x-coordinate to start from, within the maze.
* @param startY the yx-coordinate to start from, within the maze.
* @param the initial facing the facing of the 'pointer' that draws the maze.
* @recDepth the maximal recursion depth of the algorithm. Algorithm
* stops and leaves an unfinished maze when reaching this dephts.
* @param init true when the array containing the maze shall be
* cleared before running the algorithm on it.
*/
public void makeMaze(char[][] maze, int startX, int startY,
int startFacing, int recDepth, boolean init)
{
__rnd = new Random();
__environment = new boolean[3][3];
__tmpList = new Vector();
__dirList = new Vector();
__maze = maze;
__currentX = startX;
__currentY = startY;
__dungeonSizeX = __maze.length;
__dungeonSizeY = __maze[0].length;
if (init)
{
for (int a = 0; a < __dungeonSizeX; a++)
for (int b = 0; b < __dungeonSizeY; b++)
{
__maze[a][b] = Dungeon.WALL;
}
}
__currentFacing = startFacing;
__recursions = recDepth;
carve(Dungeon.NOWHERE);
if ((__recursions > -1) && (__recursions == (recDepth - 1)))
{
__maze[__currentX][__currentY] = Dungeon.WALL;
return; //ready
}
__maze[startX][startY] = Dungeon.FLOOR;
return; //ready
}
private void carve(int cameFrom)
{
if (__recursions > 0) __recursions--;
if (__recursions == 0) return;
__maze[__currentX][__currentY] = Dungeon.FLOOR;
int dirnum = 0;
int[] dirs = Toolkit.tk().getRandomIntSequence(5);
while(dirnum < 5)
{
//System.err.println(dirs[dirnum] + 1);
if (((dirs[dirnum] + 1) == Dungeon.NORTH) && (!(cameFrom == Dungeon.NORTH)) && goNorth())
carve(Dungeon.SOUTH);
else if (((dirs[dirnum] + 1) == Dungeon.SOUTH) && (!(cameFrom == Dungeon.SOUTH)) && goSouth())
carve(Dungeon.NORTH);
else if (((dirs[dirnum] + 1) == Dungeon.WEST) && (!(cameFrom == Dungeon.WEST)) && goWest())
carve(Dungeon.EAST);
else if (((dirs[dirnum] + 1) == Dungeon.EAST) && (!(cameFrom == Dungeon.EAST)) && goEast())
carve(Dungeon.WEST);
dirnum++;
}
if (cameFrom == Dungeon.NORTH) __currentY--;
if (cameFrom == Dungeon.SOUTH) __currentY++;
if (cameFrom == Dungeon.EAST) __currentX++;
if (cameFrom == Dungeon.WEST) __currentX--;
return;
}
private Vector getRandomDirectionSequenceList()
{
Vector __tmpList = new Vector();
__tmpList.addElement(new Integer(Dungeon.NORTH));
__tmpList.addElement(new Integer(Dungeon.SOUTH));
__tmpList.addElement(new Integer(Dungeon.EAST));
__tmpList.addElement(new Integer(Dungeon.WEST));
Vector __newList = new Vector();
int size = __tmpList.size();
int nextInt = 0;
for (int i = 0; i < size; i++)
{
nextInt = __rnd.nextInt() % __tmpList.size();
if (nextInt < 0) nextInt = nextInt * (-1);
__newList.addElement(__tmpList.elementAt(nextInt));
__tmpList.removeElementAt(nextInt);
}
__tmpList = null;
return __newList;
}
private boolean goNorth()
{
if (gotPermission(__currentX, __currentY - 1, Dungeon.SOUTH))
{
__currentY--;
return(true);
}
return(false);
}
private boolean goSouth()
{
if (gotPermission(__currentX, __currentY + 1, Dungeon.NORTH))
{
__currentY++;
return(true);
}
return(false);
}
private boolean goEast()
{
if (gotPermission(__currentX + 1, __currentY, Dungeon.WEST))
{
__currentX++;
return(true);
}
return(false);
}
private boolean goWest()
{
if (gotPermission(__currentX - 1, __currentY, Dungeon.EAST))
{
__currentX--;
return(true);
}
return(false);
}
private boolean gotPermission(int x, int y, int cameFrom)
{
if ((y < 1) || (y >= __dungeonSizeY)) return false;
if ((x < 1) || (x >= __dungeonSizeX)) return false;
if (__maze[x][y] == Dungeon.FLOOR) return false;
lookupEnvironment(x, y);
if (cameFrom == Dungeon.EAST)
{
if (__environment[1][0]) return false;
if (__environment[1][2]) return false;
if (__environment[2][0]) return false;
if (__environment[2][1]) return false;
if (__environment[2][2]) return false;
}
if (cameFrom == Dungeon.WEST)
{
if (__environment[0][0]) return false;
if (__environment[0][1]) return false;
if (__environment[0][2]) return false;
if (__environment[1][0]) return false;
if (__environment[1][2]) return false;
}
if (cameFrom == Dungeon.NORTH)
{
if (__environment[0][0]) return false;
if (__environment[0][1]) return false;
if (__environment[1][0]) return false;
if (__environment[2][0]) return false;
if (__environment[2][1]) return false;
}
if (cameFrom == Dungeon.SOUTH)
{
if (__environment[0][1]) return false;
if (__environment[0][2]) return false;
if (__environment[1][2]) return false;
if (__environment[2][1]) return false;
if (__environment[2][2]) return false;
}
return(true);
}
private void lookupEnvironment(int x, int y)
{
for (int a = -1; a < 2; a++)
{
for (int b = -1; b < 2; b++)
{
if (((x - b) >= __dungeonSizeX) || ((x - b) < 0))
__environment[b + 1][a + 1] = true;
else if (((y - a) >= __dungeonSizeY) || ((y - a) < 0))
__environment[b + 1][a + 1] = true;
else if (__maze[x - b][y - a] == Dungeon.FLOOR)
__environment[b + 1][a + 1] = true;
else
__environment[b + 1][a + 1] = false;
}
}
}
/**
* For testing only.
*/
/*
public static void testPrint(int maze[][])
{
for (int a = 0; a < maze.length; a++)
{
for (int b = 0; b < maze[a].length; b++)
{
System.err.print(maze[a][b]);
}
System.err.print('\n');
}
}
*/
/**
* For testing only.
*/
/*
public static void main(String args[])
{
MazeBuilder m = new MazeBuilder();
int maze[][] = new int[20][20];
m.makeMaze(maze, 1, 1, 2, 1000);
testPrint(maze);
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -