java写的一个迷宫小游戏 (累死我了)
public class TestDemo2 {
public static void main(String[] args) {
Maze maze =new Maze(3,3);
maze.goMaze();
}
}
import java.util.*;
public class Maze {
private static Scanner scanner = new Scanner(System.in);
private MazeNode[][] mazenodes;
private int row;
private int colum;
private Stack<MazeNode> stack;
public Maze( int row, int colum) {
stack= new Stack<>();
this.mazenodes =new MazeNode[row][colum];
this.row = row;
this.colum = colum;
}
public void setMaze() {
System.out.println("请输入迷宫路径");
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
mazenodes[i][j] = new MazeNode(scanner.nextInt(),i,j);
}
}
}
public void initWaystate() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
if (mazenodes[i][j].getValue()== Constant.way_Able) {
if (j+1 < colum && mazenodes[i][j+1].getValue() == Constant.way_Able) {
mazenodes[i][j].setWaystate(Constant.east,Constant.way_ISable);
}
if (j-1 >= 0 && mazenodes[i][j-1].getValue()== Constant.way_Able) {
mazenodes[i][j].setWaystate(Constant.west,Constant.way_ISable);
}
if (i+1 < row && mazenodes[i+1][j].getValue() == Constant.way_Able) {
mazenodes[i][j].setWaystate(Constant.south,Constant.way_ISable);
}
if (i-1 >= 0 && mazenodes[i-1][j].getValue() == Constant.way_Able) {
mazenodes[i][j].setWaystate(Constant.north,Constant.way_ISable);
}
}
}
}
}
public void goMaze() {
setMaze();
initWaystate();
if (mazenodes[0][0].getValue()!= 0) {
System.out.println("没有迷宫路径");
return;
}
stack.push(mazenodes[0][0]);
while(!stack.isEmpty()) {
MazeNode top = stack.peek();
int i = top.getI();
int j = top.getJ();
if (i == row - 1 && j == colum - 1) {
System.out.println("找到迷宫路径");
break;
}
if (top.getWaystate(Constant.east)) {
stack.push(mazenodes[i][j + 1]);
mazenodes[i][j].setWaystate(Constant.east, false);
mazenodes[i][j + 1].setWaystate(Constant.west, false);
continue;
}
if (top.getWaystate(Constant.west)) {
stack.push(mazenodes[i ][j-1]);
mazenodes[i][j].setWaystate(Constant.west, false);
mazenodes[i][j - 1].setWaystate(Constant.east, false);
continue;
}
if (top.getWaystate(Constant.south)) {
stack.push(mazenodes[i+1][j]);
mazenodes[i][j].setWaystate(Constant.south, false);
mazenodes[i+1][j].setWaystate(Constant.north, false);
continue;
}
if (top.getWaystate(Constant.north)) {
stack.push(mazenodes[i + 1][j]);
mazenodes[i][j].setWaystate(Constant.north, false);
mazenodes[i - 1][j].setWaystate(Constant.south, false);
continue;
}
if (!mazenodes[i][j].getWaystate(Constant.east) &&
(!mazenodes[i][j].getWaystate(Constant.west)) &&
(!mazenodes[i][j].getWaystate(Constant.south)) &&
(!mazenodes[i][j].getWaystate(Constant.north))) {
stack.pop();
}
}
showmaze();
}
public void showmaze() {
while (!stack.isEmpty()) {
MazeNode top = stack.peek();
top.setValue(Constant.road);
stack.pop();
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
System.out.print(mazenodes[i][j].getValue()+" ");
}
System.out.println();
}
}
}
public class MazeNode {
private int value;
private int i;
private int j;
private boolean[] way_state;
public MazeNode(int value, int i, int j) {
this.value = value;
this.i = i;
this.j = j;
this.way_state = new boolean[Constant.INITSIZE];
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public void setWaystate(int index,boolean isAble){
way_state[index] = isAble;
}
public boolean getWaystate(int index){
return way_state[index];
}
}
public class Constant {
public static final int INITSIZE=4;
public static final int east=0;
public static final int south=1;
public static final int west=2;
public static final int north=3;
public static final int way_Able=0;
public static final int road=2;
public static final boolean way_ISable=true;
}