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

这是一个使用Java编写的迷宫小游戏,通过栈来实现迷宫路径的寻找。玩家可以输入迷宫的大小及路径,程序将自动判断并展示迷宫的可行路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值