importjava.util.Scanner;public classgame1 {static int score = 0; //分数
static int blood = 3; //生命
static int step = 0; //步数//& 玩家//@ 终点
static String[][] arr ={//0 1 2 3 4 5 6 7 8 9
{"#","#","#","#","#","#","#","#","#","#"},//0
{"#","@"," ","#","#"," "," ","&","4","#"},//1
{"#","#"," "," ","#"," ","#","#"," ","#"},//2
{"#","#","#"," "," ","3","#"," "," ","#"},//3
{"#","#","#","#","#","#","#"," "," ","#"},//4
{"#","#","#","#","#","#","#","#","#","#"},//5
};public static void print(String arr1[][]){ //打印地图的方法
for(int i =0;i
System.out.print(arr1[i][j]+"\t");
}
System.out.println();
}
}public static voidmain(String[] args){
Scanner sc= newScanner(System.in);int x=1; //起始位置
int y=7;int ex=1; //终点位置
int ey=1;
System.out.println("迷宫小游戏");
System.out.println("使用WASD移动小人");
System.out.println("只能移动20步 只有三条命");
System.out.println("***************************");
System.out.printf("当前分数:"+score+"\t");
System.out.printf("当前生命:"+blood+"\t");
System.out.println("当前步数:"+step+"\t");
print(arr);while(arr[ex][ey] != "&" & step<=20 & blood !=0){ //到达终点 步数超过20步 生命为0 则跳出循环
String str =sc.nextLine();switch(str){case "w":if (arr[x - 1][y] == "#") { //如果下一步为墙则判断是否还有命
if(blood==0){ break;} //有命则扣血,同时步数增加,无命则跳出循环,宣告死亡
else {blood-=1;step++;break;}
}else if (arr[x - 1][y] == " "){ //如果下一步为路 则将下一步位置设置为小人
arr[x][y] = " "; //同时将小人先前的位置设置为路
x--;
arr[x][y]= "&";
step++;break;}else if (arr[x - 1][y] == "3"){ //如果下一步为金币,则将金币修改为小人
arr[x][y] = " "; //将小人先前位置设置为路
x--; //同时金币数增加,步数加1
arr[x][y] = "&";
score+=3;step++;break;}else if (arr[x - 1][y] == "4"){
arr[x][y]= " ";
x--;
arr[x][y]= "&";
score+=4;step++;break;}else if (arr[x - 1][y] == "@"){ //如果小人下一步为终点,则将终点修改为小人
arr[x][y] = " "; //将小人之前的位置设置为路
x--; //同时步数加1
arr[x][y] = "&";step++;break;}case "a":if (arr[x][y-1] == "#") {if(blood==0){ break;}else {blood-=1;step++;break;}
}else if (arr[x][y-1] == " "){
arr[x][y]= " ";
y--;
arr[x][y]= "&";step++;break;}else if (arr[x][y-1] == "3"){
arr[x][y]= " ";
y--;
arr[x][y]= "&";
score+=3;step++;break;}else if (arr[x][y-1] == "4"){
arr[x][y]= " ";
y--;
arr[x][y]= "&";
score+=4;step++;break;}else if (arr[x][y-1] == "@"){
arr[x][y]= " ";
y--;
arr[x][y]= "&";step++;break;}case "s":if (arr[x+1][y] == "#") {if(blood==0){ break;}else {blood-=1;step++;break;}
}else if (arr[x+1][y] == " "){
arr[x][y]= " ";
x++;
arr[x][y]= "&";step++;break;}else if (arr[x+1][y] == "3"){
arr[x][y]= " ";
x++;
arr[x][y]= "&";
score+=3;break;}else if (arr[x+1][y] == "4"){
arr[x][y]= " ";
x++;
arr[x][y]= "&";
score+=4;step++;break;}else if (arr[x+1][y] == "@"){
arr[x][y]= " ";
x++;
arr[x][y]= "&";step++;break;}case "d":if (arr[x][y+1] == "#") {if(blood==0){ break;}else {blood-=1;step++;break;}
}else if (arr[x][y+1] == " "){
arr[x][y]= " ";
y++;
arr[x][y]= "&";step++;break;}else if (arr[x][y+1] == "3"){
arr[x][y]= " ";
y++;
arr[x][y]= "&";
score+=3;step++;break;}else if (arr[x][y+1] == "4"){
arr[x][y]= " ";
y++;
arr[x][y]= "&";
score+=4;step++;break;}else if (arr[x][y+1] == "@"){
arr[x][y]= " ";
y++;
arr[x][y]= "&";step++;break;}default:break;
}
System.out.println("***************************");
System.out.printf("当前分数:"+score+"\t");
System.out.printf("当前生命:"+blood+"\t");
System.out.println("当前步数:"+step+"\t");
print(arr);}if(step>20){System.out.println("步数超过20,游戏结束");}else if(blood==0) {System.out.println("没有生命了,游戏结束");}else{System.out.println("恭喜你成功闯关");}
}
}