📄 mazegenerator.java
字号:
int wallcol[] = new int[ (rows * columns) / 2];
int emptyCt = 0; // number of rooms
int wallCt = 0; // number of walls
//下面主要是先写入空房子 空房子连起来就是路
for (i = 1; i < rows - 1; i += 2) // make a grid of empty rooms
for (j = 1; j < columns - 1; j += 2) {
emptyCt++;
mazeSelf[i][j] = -emptyCt; // each room is represented by a different negative number
if (i < rows - 2) { // 记录这个房子下面的房子信息record info about wall below this room
wallrow[wallCt] = i + 1;
wallcol[wallCt] = j;
wallCt++; //墙的数量变化 对应编号的墙也记录行和列
}
if (j < columns - 2) { // record info about wall to right of this room
wallrow[wallCt] = i;
wallcol[wallCt] = j + 1;
wallCt++;
}
}
Random random = new Random();
int r;
for (i = wallCt - 1; i > 0; i--) {
r = random.nextInt() % wallCt; //(Math.random() * i); //
if (r < 0) {
r = -r;
} // choose a wall randomly and maybe tear it down
tearDown(wallrow[r], wallcol[r]);
wallrow[r] = wallrow[i];
wallcol[r] = wallcol[i];
}
for (i = 1; i < rows - 1; i++) // replace negative values in maze[][] with emptyCode
for (j = 1; j < columns - 1; j++)
if (mazeSelf[i][j] < 0)
mazeSelf[i][j] = emptyCode;
/* for (i =0; i < rows ; i++) // 去除现在的5
for (j = 0; j < columns ; j++) {
if ((mazeSelf[i][j]) ==5)
mazeSelf[i][j] =0;
}
*/
}
void tearDown(int row, int col) {
// Tear down a wall, unless doing so will form a loop. Tearing down a wall
// joins two "rooms" into one "room". (Rooms begin to look like corridors
// as they grow.) When a wall is torn down, the room codes on one side are
// converted to match those on the other side, so all the cells in a room
// have the same code. Note that if the room codes on both sides of a
// wall already have the same code, then tearing down that wall would
// create a loop, so the wall is left in place.
if (row % 2 == 1 && mazeSelf[row][col - 1] != mazeSelf[row][col + 1]) {
// row is odd; wall separates rooms horizontally
fill(row, col - 1, mazeSelf[row][col - 1], mazeSelf[row][col + 1]);
mazeSelf[row][col] = mazeSelf[row][col + 1];
}
else if (row % 2 == 0 && mazeSelf[row - 1][col] != mazeSelf[row + 1][col]) {
// row is even; wall separates rooms vertically
fill(row - 1, col, mazeSelf[row - 1][col], mazeSelf[row + 1][col]);
mazeSelf[row][col] = mazeSelf[row + 1][col];
}
}
void fill(int row, int col, int replace, int replaceWith) {
// called by tearDown() to change "room codes".
if (mazeSelf[row][col] == replace) {
mazeSelf[row][col] = replaceWith;
fill(row + 1, col, replace, replaceWith);
fill(row - 1, col, replace, replaceWith);
fill(row, col + 1, replace, replaceWith);
fill(row, col - 1, replace, replaceWith);
}
}
boolean isWall(int x, int y) {
if (x >= rows || y >= columns) {
return true;
}
if (x < 0 || y < 0) {
return true;
}
if ( (mazeSelf[y][x] == 6)) {
return true;
}
if ( (mazeSelf[y][x] == 5)) {
return true;
}
if ( (mazeSelf[y][x] == 42)) {
return true;
}
if ( (mazeSelf[y][x] == 48)) {
return true;
}
if ( (mazeSelf[y][x] == 49)) {
return true;
}
if ( (mazeSelf[y][x] == 50)) {
return true;
}
return false;
}
boolean isTreasure(int x, int y) {
if ( (mazeSelf[y][x] == 5)) {
return true;
}
return false;
}
/** public static void main(String[] args) {
MazeGenerator mase = new MazeGenerator(21, 21);
mase.mazeGen();
mase.mazePrint();
System.out.println();
System.out.println();
System.out.println();
mase.mazeAdjust();
mase.mazePrint();
}
*/
public boolean saveMapData(int row, int column, int manX, int manY, //x,y是主角当前的位置
int direction, int maxScore, int level,
int spade, int chalk, int openNum, int leftTime,int allTime) {
// int maxLength;
// byte ij;
String[] RS_to_del = RecordStore.listRecordStores(); //如果有纪录则先删除所有的纪录
// byte[][] mazeArray = new byte[row][column];
byte[] mazeByte = new byte[row * column]; //地图数组字节表示
// System.out.println("row * column" + row * column);
byte[] info = new byte[15]; //要保留的其它信息
info[0] = (byte) (row);
info[1] = (byte) (column);
info[2] = (byte) (direction); //人物
info[3] = (byte) (maxScore/10); //最高得分
info[4] = (byte) (manX);
info[5] = (byte) (manY);
info[6] = (byte) (level);
info[7] = (byte) (spade);
info[8] = (byte) (chalk);
info[9] = (byte) (openNum);
info[10] = (byte) (leftTime/10);
info[11] = (byte)(allTime/10);
info[12] = (byte) (leftTime%10);
info[13] = (byte)(allTime%10);
info[14] = (byte) (maxScore%10);
//info 1,row 2,column 3,character 4,maxScore
int i, j, k;
i = 0;
j = 0;
k = 0;
for (i = 0; i < row; i++)
for (j = 0; j < column; j++) {
//mazeArray[i][j] = (byte)(mazeSelf[i][j]);
// ij = (byte)(mazeSelf[i][j]);
// maxLength = ij.
mazeByte[k] = (byte) (mazeSelf[i][j]);
k++;
}
// System.out.println("status 1");
if (RS_to_del != null) {
try {
RecordStore.deleteRecordStore("MazeSelf");
}
catch (RecordStoreException e) {
// System.out.println("error 1");
e.printStackTrace();
}
try {
RecordStore.deleteRecordStore("information");
}
catch (RecordStoreException e) {
// System.out.println("error 2");
e.printStackTrace();
}
}
// System.out.println("status 2");
RecordStore maze = null; //数组
RecordStore information = null;
try {
maze = RecordStore.openRecordStore("MazeSelf", true);
// System.out.println("error 4");
maze.addRecord(mazeByte, 0, mazeByte.length);
// System.out.println("error 5");
maze.closeRecordStore();
}
catch (RecordStoreException e) {
// System.out.println("error 3");
e.printStackTrace();
return false;
}
// System.out.println("status 3");
try {
information = RecordStore.openRecordStore("information", true);
information.addRecord(info, 0, info.length);
information.closeRecordStore();
}
catch (RecordStoreException e) {
// System.out.println("error 4");
e.printStackTrace();
return false;
}
return true;
// maze.addRecord(mazeByte,0,mazeByte.length);
}
public boolean getMapData() {
byte[] info = new byte[15]; //info 1,row 2,column 3,character 4,maxScore,5,6主角的坐标
byte[] mazeByte = null;
// System.out.println("get error 3");
int row;
int column;
// int character;
// int maxScore;
// int direction;
// int level;
int i, j, k;
int size;
// RecordStore maze = null; //数组
// RecordStore information = null;
row = 0;
column = 0;
// character = 0;
// maxScore = 0;
i = 0;
j = 0;
k = 0;
int infoRecordID = 0;
try {
// System.out.println("get error 1");
RecordStore information = RecordStore.openRecordStore("information", true);
// System.out.println("get 1");
infoRecordID = information.getNextRecordID();
// info[0] = (byte) (row);
// System.out.println("get 4");
// System.out.println("infoid 1" + infoRecordID);
info = information.getRecord(1);
// System.out.println("get error 2");
// System.out.println("get 3");
information.closeRecordStore();
// row = (int) (info[0]);
this.rows = (int) (info[0]);
// column = (int) (info[1]);
this.columns = (int) (info[1]);
// direction = (int) (info[2]);
this.direction = (int) (info[2]);
this.maxScore = (int) (info[3])*10+(int)(info[14]);
// manX = (int) (info[4]);
this.manX = (int) (info[4]);
// manY = (int) (info[5]);
this.manY = (int) (info[5]);
this.level = (int) (info[6]);
this.spade = (int) (info[7]);
this.chalk = (int) (info[8]);
this.openNum = (int) (info[9]);
this.leftTime = (int) (info[10])*10+(int)(info[12]);
this.allTime = (int) (info[11])*10+(int)(info[13]);
// System.out.println("get error 2");
// information.closeRecordStore();
// System.out.println("get 2");
}
catch (Exception e) { //RecordStoreException e) {
// System.out.println("get error 2");
// e.printStackTrace();
return false;
}
mazeSelf = new int[this.rows][this.columns];
try {
RecordStore maze = RecordStore.openRecordStore("MazeSelf", true);
infoRecordID = maze.getNextRecordID();
// System.out.println("infoid 2" + infoRecordID);
mazeByte = maze.getRecord(1);
for (i=0;i<row*column;i++){
//System.out.println("mazeByte" + mazeByte[i]);
}
maze.closeRecordStore();
// System.out.println("status " + 5);
for (i = 0; i < this.rows; i++)
for (j = 0; j < this.columns; j++) {
mazeSelf[i][j] = (int) (mazeByte[k]);
k++;
}
// System.out.println("status " + 6);
}
catch (RecordStoreException e) {
// System.out.println("get error 3");
e.printStackTrace();
}
return true;
}
// public int getManX() {
// return manX;
// }
// public int getManY() {
// return manY;
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -