利用二维数组制作关灯游戏

package com.lanou.homework0;


import java.util.Scanner;


public class GuanDeng {

public static void main(String[] args) {

/*

* 创建一个二维的数组,作为游戏的地图

* */

int [ ][ ]  map = {

{0,0,0,0,0},

{0,0,1,0,0},

{0,1,1,1,0},

{0,0,1,0,0},

{0,0,0,0,0}

};

/*-----------------------地图初始化结束-------------------------------------------*/

/*-----------------------关灯逻辑---------------------------------------------------*/

//输出一下这个二维的数组 

for(int i = 0 ; i < map.length ; i++){

for(int j = 0; j < map[i].length ; j++){

System.out.print(map[i][j] + "  ");

}

System.out.println();

}

Scanner scanner = new Scanner(System.in);

while(true){

System.out.println("请输入您要点击的行数");

int row = scanner.nextInt();

while(row > map.length-1 || row < 0){

System.out.println("您输入的行数有误,请重新输入您要点击的行数");

row = scanner.nextInt();

}

System.out.println("请输入您要点击的列数");

int col = scanner.nextInt();

while(col > map[row].length - 1|| col < 0){

System.out.println("您输入的行数有误,请重新输入您要点击的列数");

col = scanner.nextInt();

}

//自己这个位置在变化

if(map[row][col] == 1){

map[row][col] = 0;

}else{

map[row][col] = 1;

}

//头顶的位置的变化

if(row <= 0){

}else {

if(map[row - 1][col] == 1){

map[row - 1][col] = 0;

}else{

map[row - 1][col] = 1;

}

}

//脚底位置的变化

if(row >= map.length -1){

}else{

if(map[row + 1][col] == 0){

map[row + 1][col] = 1;

}else{

map[row + 1][col] = 0;

}

}

//左边位置的变化

if(col <= 0){

}else{

if(map[row][col - 1] ==1){

map[row][col-1] = 0;

}else{

map[row][col - 1] = 1;

}

}

//右边位置的变化

if(col >= map[row].length - 1){

}else{

if(map[row][col + 1] == 1){

map[row][col + 1] = 0;

}else{

map[row][col] = 1;

}

}

int sum = 0;

for(int i = 0 ; i < map.length ; i++){

for(int j = 0; j < map[i].length ; j++){

System.out.print(map[i][j] + "  ");

sum = sum + map[i][j];

}

System.out.println();

}

if(sum == 0){

System.out.println("哇哦~你真是个小天才");

return;

}

}

}

}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值