长春四环医药好吗:能帮忙写个JAVA的“迷宫游戏”的程序吗?

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 01:49:58
开发一个GUI界面的迷宫游戏,根据用户指定的迷宫的行数和列数生成一个迷宫。有一个代表走迷宫的组件,用鼠标单击组件可以将它在迷宫中移动。需要一个2维数组来定义迷宫,例如,数组中元素“#”代表“墙”,数组中的元素“*”代表“路”。
我要的是按题目上写的“需要一个2维数组来定义迷宫,例如,数组中元素“#”代表“墙”,数组中的元素“*”代表“路”。”啊,不是网上搜索到的呀~!

//主类

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");
}
}

//迷宫地图类

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

MiGong() {
int a = (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;
}
}

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

House() {

north = getStatus(north);
west = getStatus(west);
east = getStatus(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;
}
}

//主类

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");
}
}

//迷宫地图类

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

MiGong() {
int a = (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;
}
}

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

House() {

north = getStatus(north);
west = getStatus(west);
east = getStatus(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;
}
}

3000$rmb 我就写!