栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA实现迷宫小游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;


                }

            }

        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号