java
package com.wind;
import java.util.Random;
import java.util.Scanner;
public class ResultLei {
static Random random = new Random();
public static void main(String[] args) {
boolean end = true;
while (end) {
System.out.println("请输入你选择的难度对应的数字:" + '\n' + "简单:1" + '\n' + "中等:2" + '\n' + "困难:3" + '\n' + "退出:4");
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
switch (t) {
case 1:
result(9, 9, 10);
break;
case 2:
result(16, 16, 40);
break;
case 3:
result(16, 30, 100);
break;
case 4:
end = false;
break;
default:
break;
}
}
}
public static void result(int q, int w, int e) {
// 定义一个二维数组用来存储地雷的数据
int[][] arr = new int[e][e];
// 定义一个变量存储随机出来的数据比较唯一性
int x, y;
// 随机出雷的数据并保证唯一性
for (int i = 0; i < e; i++) {
boolean one = true;
x = random.nextInt(q) + 1;
y = random.nextInt(w) + 1;
for (int j = 0; j <= i; j++) {
if (x == arr[1][j] && y == arr[2][j]) {
one = false;
i--;
break;
}
}
if (one) {
arr[1][i] = x;
arr[2][i] = y;
}
}
// 计算非雷格的数字,并将数据输出
for (int i = 1; i <= q; i++) {
for (int j = 1; j <= w; j++) {
boolean lei = true;
for (int t = 0; t < e; t++) {
if (i == arr[1][t] && j == arr[2][t]) {
System.out.print("* ");
lei = false;
}
}
if (lei) {
int count = 0;
for (int t = 0; t < e; t++) {
if (i - 1 == arr[1][t] && j - 1 == arr[2][t]) {
count++;
}
if (i - 1 == arr[1][t] && j == arr[2][t]) {
count++;
}
if (i - 1 == arr[1][t] && j + 1 == arr[2][t]) {
count++;
}
if (i == arr[1][t] && j - 1 == arr[2][t]) {
count++;
}
if (i == arr[1][t] && j + 1 == arr[2][t]) {
count++;
}
if (i + 1 == arr[1][t] && j - 1 == arr[2][t]) {
count++;
}
if (i + 1 == arr[1][t] && j == arr[2][t]) {
count++;
}
if (i + 1 == arr[1][t] && j + 1 == arr[2][t]) {
count++;
}
}
System.out.print(count + " ");
}
}
System.out.println();
}
}
}