java写的一个迷宫小游戏 (累死我了)

java写的一个迷宫小游戏 (累死我了)

/**
 * @author: 
 * @date: Created in 10:41 2020/10/14
 */
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) {
    
    
                    //东边结点的value=0,当前结点可走。
                    if (j+1 < colum && mazenodes[i][j+1].getValue() == Constant.way_Able) {
    
    
                        mazenodes[i][j].setWaystate(Constant.east,Constant.way_ISable);
                    }
                    //西边结点的value=0,当前结点可走。
                    if (j-1 >= 0 && mazenodes[i][j-1].getValue()== Constant.way_Able) {
    
    
                        mazenodes[i][j].setWaystate(Constant.west,Constant.way_ISable);
                    }
                    //南边结点的value=0,当前结点可走。
                    if (i+1 < row && mazenodes[i+1][j].getValue() == Constant.way_Able) {
    
    
                        mazenodes[i][j].setWaystate(Constant.south,Constant.way_ISable);
                    }
                    //北边结点的value=0,当前结点可走。
                    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;
}

猜你喜欢

转载自blog.csdn.net/weixin_47198561/article/details/109497730