package com.just.test;
import java.util.Scanner;
public class T3mazeGame {
public static void main(String[] args) {
//定义一个二维数组充当地图
int [][] maze={
{1,1,1,1,1,1,1},
{5,0,0,0,0,0,1},
{1,0,1,0,1,0,1},
{1,0,0,1,0,1,1},
{1,1,0,1,0,1,1},
{1,0,0,0,0,0,1},
{1,1,1,1,1,0,1},
};
//打印地图
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
System.out.println("游戏规则:4向左,6向右,8向上,2向下");
System.out.println("开始运动");
System.out.println("⏬⏬⏬⏬⏬⏬");
//定义初始位置 1,0
int x =1;
int y =0;
Scanner scanner =new Scanner(System.in);
while (true) {
int a = scanner.nextInt();
switch (a) {
//向上移动
case 8:
if (maze[x-1][y]==0) {
int temp=maze[x][y];
maze[x][y]=maze[x-1][y];
maze[x-1][y]=temp;
x--;
}
break;
//向下移动
case 2:
if (maze[x+1][y]==0) {
int temp=maze[x][y];
maze[x][y]=maze[x+1][y];
maze[x+1][y]=temp;
x++;
}
break;
//向右移动
case 6:
if (maze[x][y+1]==0) {
int temp=maze[x][y];
maze[x][y]=maze[x][y+1];
maze[x][y+1]=temp;
y++;
}
break;
//向左移动
case 4:
if (maze[x][y-1]==0) {
int temp=maze[x][y];
maze[x][y]=maze[x][y-1];
maze[x][y-1]=temp;
y--;
}
break;
default:
break;
}
//打印实时变化的地图
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(maze[i][j]+" ");
}
System.out.println();
}
if (x==6&&y==5){
System.out.println("you win ");
break;
}
}
}
}
java循环小游戏
最新推荐文章于 2023-09-08 11:15:21 发布