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;
}
}
}
}
}
JAVA实现迷宫小游戏
最新推荐文章于 2024-09-05 05:24:46 发布