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<String,String> map = new HashMap<String,String>();
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);
}
}
java控制台版扫雷
最新推荐文章于 2023-03-16 18:26:31 发布