⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamestart.java

📁 迷宫游戏
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class GameStart {
  public static void main(String[] args) {
    int num=0;//判断方向
    System.out.println("迷宫游戏开始:");
    int[][] array = new MiGong().getMiGong();//创建迷宫
    //System.out.println(array.length);
    for(int i=0;i<array.length;i++)//开始走迷宫
    {
      System.out.println("现在你在第"+(i+1)+"个房间!请选择方向:");

      boolean b=true;
      while(b)//判断选择的方向
      {
        System.out.println("--------------------");
        System.out.println("|                  |");
        System.out.println("|                  |");
        System.out.println("|                  |");
        System.out.println("|        00        |");

        System.out.println("1:向上走。");
        System.out.println("2:向左走。");
        System.out.println("3:向右走。");

        try {
          String s=new BufferedReader(new InputStreamReader(System.in)).readLine();
          try
          {
            num=Integer.parseInt(s);
            if(num<0||num>3)
            {
              System.out.println("选择错误!请重试:");
              continue;
            }
            if(array[i][num-1]==2)//判断选择的是否是墙
            {
              switch(num)
              {
                  case 1:System.out.println("向上走是堵墙!请重选择:");break;
                  case 2:System.out.println("向左走是堵墙!请重选择:");break;
                  case 3:System.out.println("向右走是堵墙!请重选择:");break;
              }
              continue;
            }
            b=false;
          }
          catch (NumberFormatException ex)
          {
            System.out.println("非数字,请重试:");
            continue;
          }
        }
        catch (IOException ex)
        {
          System.out.println(ex);
        }
      }
      if(array[i][num-1]==3)//判断是否是炸弹
      {
        switch(num)
              {
                  case 1:System.out.println("向上走是炸弹!GAME OVER!");break;
                  case 2:System.out.println("向左走是炸弹!GAME OVER!");break;
                  case 3:System.out.println("向右走是炸弹!GAME OVER!");break;
              }
        break;
      }
      if (i==array.length-1)
      {
        System.out.println("恭喜你走出迷宫!");
      }
    }
    System.out.println("88");
  }
}

 

//迷宫地图类


class MiGong {
  static int[][] array;//迷宫中房间的数量和房间中3面墙的状态

  MiGong() {
    int a =3;//(int) (Math.random() * 90) + 10;//随机得到房间数
    array = new int[a][3];
    for (int i = 0; i < a; i++) {//为每个房间取得墙的状态
      array[i] = new House().getHouse();
    }
  }

  static int[][] getMiGong() {//输出迷宫
    return array;
  }
}


 

//创建房间类
class House {
  private static int north;//上方
  private static int west;//左方
  private static int east;//右方

  House() {

    north = getStatus(north);System.out.print(north);
    west = getStatus(west);//System.out.print(west);
    east = getStatus(east);//System.out.print(east);
    if (!panDuan()) {
      new House();
    }
  }

  private static int getStatus(int qiang) {//随机得到墙的状态,1为门,2为墙,3为炸弹
    qiang = (int) (Math.random() * 4+1);
    return qiang;
  }

  private static boolean panDuan() {//判断是否有且只有一个门
    if (north == 1 && west != 1 && east != 1) {
      return true;
    }
    if (north != 1 && west == 1 && east != 1) {
      return true;
    }
    if (north != 1 && west != 1 && east == 1) {
      return true;
    }
    return false;
  }

  int[] getHouse() {//输出房间
    int[] a = new int[3];

    a[0] = north;
    a[1] = west;
    a[2] = east;
    return a;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -