public class SweepMine {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random sj = new Random();
int[][] arr = new int[9][9];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = sj.nextInt(2) + 9;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
while (arr[i][j] == 9) {
int count = 0;
if (i < arr.length - 1 && j < arr[i].length - 1) {
if (arr[i][j + 1] == 10) {// →
count++;
}
if (arr[i + 1][j + 1] == 10) {// ↘
count++;
}
if (arr[i + 1][j] == 10) {// ↓
count++;
}
}
if (i < arr.length - 1 && j > 0) {
if (arr[i + 1][j - 1] == 10) {// ↙
count++;
}
}
if (i > 0 && j > 0) {
if (arr[i][j - 1] == 10) {// ←
count++;
}
if (arr[i - 1][j - 1] == 10) {// ↖
count++;
}
if (arr[i - 1][j] == 10) {// ↑
count++;
}
}
if (i > 0 && j < arr[i].length - 1) {
if (arr[i - 1][j + 1] == 10) {// ↗
count++;
}
}
arr[i][j] = count;
break;
}
}
}
int[][] arr2 = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
arr2[i][j] = 11;
}
}
while (true) {
showMap(arr2);
System.out.println("输入x");
int x = sc.nextInt();
System.out.println("输入y");
int y = sc.nextInt();
if (arr[x][y] != 10) {
arr2[x][y] = arr[x][y];
} else {
break;
}
}
System.out.println("游戏结束");
showMap(arr);
}
public static void showMap(int arr[][]) {
System.out.print(" ");
for (int i = 0; i < arr[0].length; i++) {
System.out.print(i);
}
System.out.println("");
for (int i = 0; i < arr.length; i++) {
System.out.print(i + " ");
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == 10) {
System.out.print("*");
} else if (arr[i][j] == 0) {
System.out.print(" ");
} else if (arr[i][j] == 11) {
System.out.print("?");
} else {
System.out.print(arr[i][j]);
}
}
System.out.println("");
}
}
}
|
|