📄 mazemodel.java
字号:
import java.util.*;
public class MazeModel extends Object {
static private int NORTH=0,SOUTH=1,EAST=2,WEST=3,FAILED=4;
static private int ROW_MAX=15, COL_MAX=30;
private char[][] maze;
private int startRow, startCol;
private MazeView view;
private class ChoicePoint extends Object {
private int row, col, direction;
private ChoicePoint(int row, int col, int direction) {// constructor
this.row = row; this.col = col; this.direction = direction; }
public int getDirection() { return direction; }
public int getRow() { return row; }
public int getCol() { return col; }
}
public MazeModel(MazeView mv) { // constructor MazeView mv
view = mv;
maze = new char[ROW_MAX][COL_MAX];
initMaze();
}
public void initMaze() {
for(int row=0; row < ROW_MAX; row++)
for(int col=0; col < COL_MAX; col++)
maze[row][col] = '*';
}
private void findStartPos() {
for(int row=0; row < ROW_MAX; row++)
for(int col=0; col < COL_MAX; col++)
if (maze[row][col] == 'P')
{ startRow = row; startCol = col; return; }
}
public boolean solveMaze(){
findStartPos();
maze[startRow][startCol] = '*';
boolean solved = getOut();
maze[startRow][startCol] = 'P';
view.printMaze();
return solved;
}
public boolean getOut(){
Stack s = new Stack();
ChoicePoint c;
int currDir = -1;
int currRow, currCol, newRow, newCol;
c = new ChoicePoint(startRow, startCol, currDir);
s.push(c);
while(s.size() > 0) { // while1
c = (ChoicePoint) s.pop();
currDir = c.getDirection();
currRow = c.getRow();
currCol = c.getCol();
while(currDir != FAILED) { // while2
if (currDir == NORTH)
{ newRow = currRow - 1; newCol = currCol; }
else if (currDir == SOUTH)
{ newRow = currRow + 1; newCol = currCol; }
else if (currDir == EAST)
{ newRow = currRow; newCol = currCol + 1; }
else if (currDir == WEST)
{ newRow = currRow; newCol = currCol - 1; }
else
{ currDir += 1; continue; }
if (maze[newRow][newCol] == 'T')
return true;
else
if (maze[newRow][newCol] == ' ') {
maze[newRow][newCol] = '.';
s.push(new ChoicePoint(currRow,currCol,currDir));
currRow = newRow;
currCol = newCol;
currDir = NORTH; }
else
currDir += 1;
} // end of while2, next step should be in backtrace
maze[currRow][currCol]='A'; // new add
}// end of while1
return false;
}
// the following methods are called from MazeView.java
public void setMaze(String mazeStr) {
for(int row=0; row < ROW_MAX; row++)
{
int strPos = row * COL_MAX + row;
for( int col = 0; col < COL_MAX; col++)
{
maze[row][col] = mazeStr.charAt(strPos);
strPos++;
}
strPos++;
}
}
public String getMaze() {
String str = "";
for(int row=0; row < ROW_MAX; row++)
{
for( int col = 0; col < COL_MAX; col++)
str = str + maze[row][col];
str = str + "\n";
}
return str;
}
public String getResult() {
String str = "";
for(int row=0; row < ROW_MAX; row++)
{
for( int col = 0; col < COL_MAX; col++) {
if (maze[row][col]=='A')
maze[row][col]=' ';
str = str + maze[row][col];
}
str = str + "\n";
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -