import java.util.Objects;
import java.util.Scanner;
//扫雷简易版//待实现中
public class gameTest {
public static void main(String[] args) {
int rayTotal = 0;//雷数
int total = 0;//数量来判断终止
int number = 100 - rayTotal;//获胜数量
// boolean flag = true;//终止条件
Scanner scan = new Scanner(System.in);
String[][] map = new String[14][14];//隐式地图
String[][] map1 = new String[10][10];//显示地图
for (int i = 1; i < map.length - 1; i++) {
for (int j = 1; j < map.length - 1; j++) {
map[i][j] = "O";//赋值
// System.out.print(" " + map[i][j]);
}
// System.out.println();
}
for (int i = 0; i < 14; i++) {
map[0][i] = "O";
map[i][0] = "O";
}
for (int i = 1; i < 14; i++) {
map[1][i] = "O";
map[i][1] = "O";
}
//给雷的位置
map[2][4] = "*";
map[2][3] = "*";
map[2][2] = "*";
for (int i = 1; i < map.length - 1; i++) {
for (int j = 1; j < map.length - 1; j++) {
if (map[i][j].equals("*")) {
rayTotal++;
}
// System.out.print(" " + map[i][j]);
}
// System.out.println();
}
//显示地图
for (int i = 0; i < map1.length; i++) {
for (int j = 0; j < map1.length; j++) {
map1[i][j] = "O";//赋值
System.out.print(" " + map1[i][j]);
}
System.out.println();
}
while (true) {//循环打印界面 flag为中止条件
System.out.println("请输入扫雷的x位置: ");
int x = 0;
int y = 0;
try {
x = scan.nextInt();//玩家输入的x坐标
// if (x < 1 || x > 10) {//防止空指针
// System.out.println("输入错误,重新输入");
// continue;
// }
x -= 1;
System.out.println("请输入扫雷的y位置: ");
y = scan.nextInt();//玩家输入的y坐标
// if (y < 1 || y > 10) {//防止空指针
// System.out.println("输入错误,重新输入");
// continue;
// }
y -= 1;
map1[x][y] = String.valueOf(gameTest.ifRay(map, x + 1, y + 1));//String.valueOF()将int型转换为String类型//用隐式地图map判断
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("输入错误,重新输入");
continue;
}catch(RuntimeException e){//对于踩雷的返回的异常的处理
//Test//如果相同则最后打印一次 地图并显示雷的位置
//显示雷的位置
map1[1][4] = "*";
map1[1][3] = "*";
map1[1][2] = "*";
for (int i = 0; i < map1.length; i++) {
for (int j = 0; j < map1.length; j++) {
System.out.print(" " + map1[i][j]);
}
System.out.println();
}
System.out.println(e.getMessage());
System.exit(0);
}
//调用ifRay方法来判断周围的雷数
//Test
for (int i = 0; i < map1.length; i++) {
for (int j = 0; j < map1.length; j++) {
System.out.print(" " + map1[i][j]);
}
System.out.println();
}
//Test
for (int i = 0; i < map1.length; i++) {
for (int j = 0; j < map1.length; j++) {
if (map1[i][j] != "O") {
continue;
}
total++;
}
}
if (total == number) {
System.out.println("你赢了,别玩了");
break;
}
}
}
public static int ifRay(String m[][], int x, int y) throws RuntimeException {//判断方法 传入一个数组,数组下标(x,y),判断九宫格有几个雷
int count = 0;//记录雷数量
if(m[x][y].equals("*")){//刚好踩到地雷
throw new RuntimeException("游戏结束,你的四肢被地雷炸碎了,尸骨无存。");
}
if (m[x + 1][y].equals("*")) {//下方是否有雷
count++;
}
if (m[x - 1][y].equals("*")) {//上方是否有雷
count++;
}
if(m[x][y+1].equals("*")){//左方是否有雷
count++;
}
if(m[x][y-1].equals("*")) {//右方是否有雷
count++;
}
if(m[x-1][y-1].equals("*")) {//左上
count++;
}
if(m[x+1][y+1].equals("*")) {//右下
count++;
}
if(m[x-1][y+1].equals("*")) {//右上
count++;
}
if(m[x+1][y-1].equals("*")){//左下
count++;
}
return count;
}
}