java控制台扫雷_java控制台版扫雷

package com;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class BOOM {//x,y|x,y,z(z=确定有炸弹)

public static void main(String[] args) {

start();

}

public static void start(){

Map map = new HashMap();

String[][] boom = new String[9][9];

int bi = 0;

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

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

boom[i][j]="_";

}

}

while (true) {

int raC = (int)(Math.random()*9);

int raR = (int)(Math.random()*9);

if(boom[raR][raC].equals("_")){

boom[raR][raC]="9";

bi++;

if(bi==10){

break;

}

}

}

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

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

if(boom[i][j].equals("9")){

System.out.print("_");

}else{

System.out.print(boom[i][j]);

}

System.out.print("|");

}

System.out.println();

}

while(true){

int x = 0;

int y = 0;

Scanner sc = new Scanner(System.in);

String info = sc.nextLine();

String[] infos = info.split(",");

if(infos.length<2){

continue;

}

try {

x = Integer.parseInt(infos[0]);

y = Integer.parseInt(infos[1]);

} catch (Exception e) {

continue;

}

if(x>8||y>8||x<0||y<0){

continue;

}

if(infos.length==3){

int count = 0;

if(boom[x][y].equals("9")){

boom[x][y]= "x";

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

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

if(boom[i][j].equals("9")){

System.out.print("_");

}else{

if(boom[i][j].equals("x")){

count ++;

}

if(count==10){

System.out.println("You Win");

break;

}

System.out.print(boom[i][j]);

}

System.out.print("|");

}

System.out.println();

}

continue;

}else{

System.out.println("You Lost");

break;

}

}else{

if(boom[x][y].equals("9")){

System.out.println("You Lost");

break;

}

}

int count = 0;

boom[x][y] = booms(boom,x,y,map);

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

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

if(boom[i][j].equals("9")){

System.out.print("_");

}else{

if(boom[i][j].equals("x")){

count ++;

}

if(count==10){

System.out.println("You Win");

break;

}

System.out.print(boom[i][j]);

}

System.out.print("|");

}

System.out.println();

}

}

}

private static String booms(String[][] boom, int x, int y,Map map) {

int count = 0;

String b = "";

int i = 0;

int j = 0;

for (i = -1; i < 2; i++) {

for (j = -1; j < 2; j++) {

if(map.containsKey(""+(x+i)+(y+j))){

continue;

}

try {

b = boom[x+i][y+j];

if(b.equals("_")){

}

} catch (Exception e) {

b = "0" ;

}

count += b.equals("9")||b.equals("x")?1:0;

}

}

if(count==0){

map.put(""+x+y,"");

for (i = -1; i < 2; i++) {

for (j = -1; j < 2; j++) {

if(map.containsKey(""+(x+i)+(y+j))){

continue;

}

try {

b = boom[x+i][y+j];

if(b.equals("_")){

boom[x+i][y+j] = booms(boom,x+i,y+j,map);

}

} catch (Exception e) {

b = "0" ;

}

count += b.equals("9")?1:0;

}

}

}

return String.valueOf(count);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值