JAVA实现迷宫小游戏

import java.util.Scanner;

public class migong {
    public static void main(String[] args) {

        String[][] a = {
                {"#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"},
                {"#", "0", "#", " ", " ", " ", "#", " ", " ", "#", "#", "#"},
                {"#", " ", "#", " ", "#", " ", " ", " ", "#", "#", " ", " "},
                {"#", " ", "#", " ", "#", " ", "#", "#", "#", "#", " ", "#"},
                {"#", " ", " ", " ", "#", " ", " ", "#", "#", " ", " ", "#"},
                {"#", " ", "#", "#", "#", " ", " ", " ", "#", " ", "#", "#"},
                {"#", " ", "#", "#", "#", "#", "#", " ", "#", " ", "#", "#"},
                {"#", " ", "#", " ", " ", " ", "#", " ", "#", " ", "#", "#"},
                {"#", " ", "#", " ", "#", "#", " ", " ", "#", " ", "#", "#"},
                {"#", " ", "#", " ", "#", "#", " ", "#", "#", " ", "#", "#"},
                {"#", " ", " ", " ", "#", "#", " ", " ", " ", " ", " ", "#"},
                {"#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"}
        };//定义迷宫的形状
        Show(a);

    }


    public static void Show(String[][] a) {
        System.out.println("用w,a,s,d,移动");
        display(a);//第一次展示迷宫
        
        Scanner s = new Scanner(System.in);

        do {
            char choice = s.next().charAt(0);
            switch (choice) {

                case 'w':
                    shang(a);
                    break;
                case 's':
                    xia(a);
                    break;
                case 'a':
                    zuo(a);
                    break;
                case 'd':
                    you(a);
                    break;
            }


        } while (!("0".equals(a[2][11])));//判断玩家是否走到终点
        System.out.println();
        System.out.println("成功");
    }

    public static void display(String[][] a) {//第一次打印迷宫形状

        for (int i = 0; i < a.length; i++) {
            System.out.println();
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]);
            }
        }
    }

    public static void xia(String[][] a) {//定义向下移动的方法
        jump://定义跳出点
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if ("0".equals(a[i][j])) {

                    if ("#".equals(a[i + 1][j])) {

                        System.out.println("无法前进");
                        break jump;
                    }
                        a[i][j] = " ";

                        a[i + 1][j] = "0";
                        display(a);
                        break jump;


                }

            }

        }

    }
    public static void shang(String[][] a) {//定义想上移动的方法
        jump:
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if ("0".equals(a[i][j])) {

                    if ("#".equals(a[i - 1][j])) {

                        System.out.println("无法前进");
                        break jump;
                    }
                    a[i][j] = " ";
                    a[i - 1][j] = "0";
                    display(a);
                    break jump;


                }

            }

        }

    }
    public static void zuo(String[][] a) {//定义向左移动的方法
        jump:
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if ("0".equals(a[i][j])) {

                    if ("#".equals(a[i][j -1])) {

                        System.out.println("无法前进");
                        break jump;
                    }
                    a[i][j] = " ";
                    a[i][j-1] = "0";
                    display(a);
                    break jump;


                }

            }

        }

    }
    public static void you(String[][] a) {定义向右移动的方法
        jump:
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if ("0".equals(a[i][j])) {

                    if ("#".equals(a[i][j+1])) {

                        System.out.println("无法前进");
                        break jump;
                    }
                    a[i][j] = " ";
                    a[i][j+1] = "0";
                    display(a);
                    break jump;


                }

            }

        }

    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值