java循环小游戏

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

		 
		 
	
	
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值