📄 mazegenerator.java
字号:
import java.util.Random;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class MazeGenerator {
public int maxScore =0;
public int level = 0;
public int manX = 0;
public int manY = 0; //主角的位置,用于读取地图
public int direction = 0;
public int character = 0;
public int spade = 0; //铁铲
public int chalk = 0; //粉笔
public int openNum = 0; //已经打开的宝箱
public int leftTime = 0; //剩余时间
public int allTime = 0; //总时间
int mazeSelf[][];
int rows, columns; //rows 是 迷宫的行 columns 是 迷宫的列
final static int backgroudCode = 0; //迷宫内方块的类型
final static int wallCode = 6;
final static int pathCode = 2;
final static int emptyCode = 9;
final static int visitedCode = 4;
MazeGenerator(int rows, int columns) { //初始化迷宫数组
this.rows = rows;
this.columns = columns;
mazeSelf = new int[rows][columns];
int i, j = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++) {
mazeSelf[i][j] = wallCode;
}
}
public void mazeAdjust() {
final int rows, columns;
int i, j, k1, k2, k3, k4;
int position; //当前位置信息
rows = this.rows;
columns = this.columns;
Random xRoad = new Random(); //横路有十种随机地图选择
Random yRoad = new Random(); //直路有十种随机地图选择
Random treasure = new Random(); //(int)(rows / 3));//根据地图大小确定宝物数量
int treasureNum = Math.abs(treasure.nextInt() % (int) (rows / 3)); //确定宝物数量
i = 0;
j = 0;
k1 = 3; //k1,k2,k3,k4每个区的宝物最大数量
k2 = 3;
k3 = 3;
k4 = 3;
Random haveTreasure = new Random();
Random roadType = new Random();
for (i = 1; i < rows - 1; i++)
for (j = 1; j < columns - 1; j++) {
if (mazeSelf[i][j] != 6) {
position = judge(i, j); //判断当前的道路是否是
switch (position) { //取出地图对应的数字
case 1:
mazeSelf[i][j] = 9 + Math.abs(xRoad.nextInt() % 10);
break;
case 2:
mazeSelf[i][j] = 19 + Math.abs(yRoad.nextInt() % 10);
break;
case 3:
mazeSelf[i][j] = 37;
break;
case 4:
mazeSelf[i][j] = 29;
break;
case 5:
mazeSelf[i][j] = 30;
break;
case 6:
mazeSelf[i][j] = 31;
break;
case 7:
mazeSelf[i][j] = 32;
break;
case 8:
mazeSelf[i][j] = 33;
break;
case 9:
mazeSelf[i][j] = 34;
break;
case 10:
mazeSelf[i][j] = 35;
break;
case 11:
mazeSelf[i][j] = 36;
break;
case 12:
mazeSelf[i][j] = 38;
break;
case 13:
mazeSelf[i][j] = 39;
break;
case 14:
mazeSelf[i][j] = 40;
break;
case 15:
mazeSelf[i][j] = 41;
break;
default:
}
}
else {
}
}
for (i = 1; i < rows - 1; i++)
for (j = 1; j < columns - 1; j++) {
if (mazeSelf[i][j] == 6) {
if ( ( (mazeSelf[i - 1][j] != 6) | (mazeSelf[i + 1][j] != 6) |
(mazeSelf[i][j - 1] != 6) | (mazeSelf[i][j + 1] != 6)) &
( (haveTreasure.nextInt() % 2) == 1) & (k1 > 0)
& (treasureNum > 0) & (i < (int) (rows / 2)) &
(j < (int) (rows / 2))) {
mazeSelf[i][j] = 5;
k1--;
treasureNum--;
}
if ( ( (mazeSelf[i - 1][j] != 6) | (mazeSelf[i + 1][j] != 6) |
(mazeSelf[i][j - 1] != 6) | (mazeSelf[i][j + 1] != 6)) &
( (haveTreasure.nextInt() % 2) == 1) & (k2 > 0)
& (treasureNum > 0) & (i > (int) (rows / 2)) &
(j < (int) (rows / 2))) {
mazeSelf[i][j] = 5;
k2--;
treasureNum--;
}
if ( ( (mazeSelf[i - 1][j] != 6) | (mazeSelf[i + 1][j] != 6) |
(mazeSelf[i][j - 1] != 6) | (mazeSelf[i][j + 1] != 6)) &
( (haveTreasure.nextInt() % 2) == 1) & (k3 > 0)
& (treasureNum > 0) & (i < (int) (rows / 2)) &
(j > (int) (rows / 2))) {
mazeSelf[i][j] = 5;
k3--;
treasureNum--;
}
if ( ( (mazeSelf[i - 1][j] != 6) | (mazeSelf[i + 1][j] != 6) |
(mazeSelf[i][j - 1] != 6) | (mazeSelf[i][j + 1] != 6)) &
( (haveTreasure.nextInt() % 2) == 1) & (k4 > 0)
& (treasureNum > 0) & (i > (int) (rows / 2)) &
(j > (int) (rows / 2))) {
mazeSelf[i][j] = 5;
k4--;
treasureNum--;
}
//把墙的图变换一下
if (mazeSelf[i][j] == 6) {
switch (Math.abs(roadType.nextInt() % 10)) {
case 1:
mazeSelf[i][j] = 48;
break;
case 2:
mazeSelf[i][j] = 49;
break;
case 3:
mazeSelf[i][j] = 50;
break;
}
}
}
}
mazeSelf[1][1] = 7;
mazeSelf[rows - 2][columns - 2] = 8;
}
public int judge(int row, int column) {
int status;
status = 0;
//1,是横的路
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] != 6))
status = 1;
{
}
//2,是垂直的路
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] == 6)) {
status = 2;
}
//3,是十字路口
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] != 6)) {
status = 3;
}
//4,是二叉路 左是墙上是墙 对应 xy1.png
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] != 6)) {
status = 4;
}
//5,是二叉路 右是墙上是墙 对应 xy2.png
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] == 6)) {
status = 5;
}
//6,是二叉路 右是墙下是墙 对应 xy3.png
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] == 6)) {
status = 6;
}
//7,是二叉路 左是墙下是墙 对应 xy4.png
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] != 6)) {
status = 7;
}
//8,是三叉路 下是墙 对应 xyz1.png
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] != 6)) {
status = 8;
}
//9,是三叉路 左是墙 对应 xyz2.png
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] != 6)) {
status = 9;
}
//10,是三叉路 上是墙 对应 xyz3.png
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] != 6)) {
status = 10;
}
//11,是三叉路 右是墙 对应 xyz4.png
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] == 6)) {
status = 11;
}
//12,是路的尽头,右是路,对应end1
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] != 6)) {
status = 12;
}
//13,是路的尽头,左是路,对应end2
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] != 6) & (mazeSelf[row][column + 1] == 6)) {
status = 13;
}
//14,是路的尽头,下是路,对应end3
if ( (mazeSelf[row - 1][column] == 6) & (mazeSelf[row + 1][column] != 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] == 6)) {
status = 14;
}
//15,是路的尽头,上是路,对应end4
if ( (mazeSelf[row - 1][column] != 6) & (mazeSelf[row + 1][column] == 6) &
(mazeSelf[row][column - 1] == 6) & (mazeSelf[row][column + 1] == 6)) {
status = 15;
}
return status;
}
public void mazeInit(int rows, int columns) {
this.rows = rows;
this.columns = columns;
mazeSelf = new int[rows][columns];
int i, j = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < columns; j++) {
mazeSelf[rows][columns] = wallCode;
}
}
public void mazeDestroy() {
mazeSelf = null;
}
public void mazePrint() {
int i, j = 0;
try {
for (i = 0; i < this.rows; i++) {
for (j = 0; j < this.columns; j++) {
//System.out.print(mazeSelf[i][j]);
if (mazeSelf[i][j] < 10) {
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println("");
}
}
catch (Exception e) {
System.out.print(e);
}
}
public int[][] mazeGet() {
return (mazeSelf);
}
public void mazeSet(int row, int column, int value) {
mazeSelf[row][column] = value;
}
public void mazeGen() {
// Create a random maze. The strategy is to start with
// a grid of disconnnected "rooms" separated by walls.
// then look at each of the separating walls, in a random
// order. If tearing down a wall would not create a loop
// in the maze, then tear it down. Otherwise, leave it in place.
if (mazeSelf == null) {
mazeInit(rows, columns);
}
int i, j = 0;
int wallrow[] = new int[ (rows * columns) / 2]; // position of walls between rooms
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -