package com.hui.扫雷;
import java.util.Scanner;
public class 扫雷 {
public static void main(String[] args) {
扫雷 user = new 扫雷();
int map[][] = new int[15][15];
Scanner input = new Scanner(System.in);
user.produceThunder(map, 20); // 地雷的个数
user.initMap(map);
int x, y;
do {
System.out.println("请输入X,Y坐标[1-15]:");
x = input.nextInt();
y = input.nextInt();
} while (user.judge(x, y, map));
System.out.println("----------------结束-------------");
input.close();
}
/**
* 判断输,
* @param x
* @param y
* @param map
* @return
*/
public boolean judge(int x, int y, int map[][]) {
if (x > map.length || y > map.length || x < 1 || y < 1) {
System.out.println("----坐标超界-----");
return true;
} else if (map[x - 1][y - 1] == -1) {
System.out.println("-----------你输了----------");
printThunder(map);
return false;
} else {
showResult(x, y, map);
return true;
}
}
/**
* 每次输入的结果打印
* @param x坐标
* @param y坐标
* @param a 地图
*/
public void showResult(int x, int y, int a[][]) {
if (a[x - 1][y - 1] == 0)
searchBlank(x - 1, y - 1, a); // 显示空白块
else if (a[x - 1][y - 1] > 0 && a[x - 1][y - 1] < 10)
a[x - 1][y - 1] += 10; // 标识 地雷个数 块,加10 区分
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == -2) // 标识空白块
System.out.print("- ");
else if (a[i][j] > 10)
System.out.print(a[i][j] - 10 + " "); // 显示周围地雷个数
else
System.out.print("= ");
}
System.out.println();
}
}
/**
* 扫描显示空白块
* @param x
* @param y
* @param a
*/
public void searchBlank(int x, int y, int a[][]) {
for (int i = x - 1; i < x + 2; i++) {
for (int j = y - 1; j < y + 2; j++) {
if (i >= 0 && j >= 0 && i < a.length && j < a.length && a[i][j] == 0) {
a[i][j] = -2; // 标识空白块
searchBlank(i, j, a); // 继续向外扫描
} else if (i >= 0 && j >= 0 && i < a.length && j < a.length && a[i][j] > 0 && a[i][j] < 10)
a[i][j] += 10; // 标识 地雷个数 块
else if (i >= 0 && j >= 0 && i < a.length && j < a.length && a[i][j] == -2)
continue; // 跳过已扫描的空白块
else if (i >= 0 && j >= 0 && i < a.length && j < a.length && a[i][j] > 10)
continue; // 跳过已标识的地雷个数块
}
}
}
/**
* 生成地雷
* @param map 地图
* @param n个数
*/
public void produceThunder(int map[][], int n) {
int x, y;
for (int i = 0; i < n; i++) {
x = (int) (Math.random() * map.length);
y = (int) (Math.random() * map[0].length);
if (map[x][y] == 0)
map[x][y] = -1;
else
i--;
}
produceNum(map);
}
/**
* 输的时候显示所有地雷
* @param a
*/
public void printThunder(int a[][]) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == -2)
System.out.print("- ");
else if (a[i][j] > 10)
System.out.print(a[i][j] - 10 + " ");
else if (a[i][j] == -1)
System.out.print("▲ ");
else
System.out.print("= ");
}
System.out.println();
}
}
/**
* 显示所有地雷和数字,调试用
* @param a
*/
public void printThunderAll(int a[][]) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == -2 || a[i][j] == 0)
System.out.print("- ");
else if (a[i][j] > 10)
System.out.print(a[i][j] - 10 + " ");
else if (a[i][j] == -1)
System.out.print("▲ ");
else
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
/**
* 初始化地图
* @param a
*/
public void initMap(int a[][]) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print("= ");
}
System.out.println();
}
}
/**
* 生成地雷周围的数字
* @param a
*/
public void produceNum(int a[][]) {
for (int x = 0; x < a.length; x++) {
for (int y = 0; y < a[x].length; y++) {
if (a[x][y] == -1) {
sizhou(x, y, a);
}
}
}
}
/**
* 扫描每个地雷的一周
* @param x
* @param y
* @param a
*/
public void sizhou(int x, int y, int a[][]) {
for (int i = x - 1; i < x + 2; i++) {
for (int j = y - 1; j < y + 2; j++) {
if (i >= 0 && j >= 0 && i < a.length && j < a.length && a[i][j] >= 0)
a[i][j]++;
}
}
}
}
控制台小游戏 - 扫雷 的简单实现
最新推荐文章于 2024-07-23 22:06:00 发布