java简单版扫雷实现
- 实现原理
-
- 直接贴代码
- 成果展示
实现原理 扫雷实现第一步就是生成棋盘,第二步就要生成每个棋子,定义棋子属性是否是地雷,第三部就是给不是地雷的棋子赋值(附近多少地雷)。
直接贴代码
/**
* 扫雷棋盘
*/
public class DemoTest {
public static void main(String[] args) {
int[][] arrs = generate();
print(arrs);
}private static void print(int[][] arrs) {
if (arrs == null || arrs.length == 0) {
return;
}
for (int i = 0;
i < arrs.length;
i++) {
for (int j = 0;
j < arrs[i].length;
j++) {
if (arrs[i][j] == -2) {
System.out.print("P ");
}else {
System.out.print(arrs[i][j] + " ");
}
}
//换行打印
System.out.println();
}
}private static int[][] generate() {
Scanner in =new Scanner(System.in);
//设置棋盘的大小
System.out.println("请输入棋盘大小:");
int a = in.nextInt();
if (a <= 0) {
System.out.println("棋盘大小必须大于0");
return null;
}
//设置地雷的个数
System.out.println("请设置地雷的个数:");
int b = in.nextInt();
if (b < 0) {
System.out.println("地雷个数必须大于0");
return null;
}else if (b > a*a) {
System.out.println("地雷个数必须小于" + a*a);
return null;
}
int[][] arrs = new int[a][a];
//地雷标记位-2,其它标记为-1
Random r = new Random();
for (int i = 0;
i < arrs.length;
i++) {
for (int j = 0;
j < arrs[i].length;
j++) {
if (b > 0) {
boolean flag = r.nextBoolean();
if (flag) {
arrs[i][j] = -2;
b --;
}else {
arrs[i][j] = -1;
}
}else {
arrs[i][j] = -1;
}
}
}for (int i = 0;
i < arrs.length;
i++) {
for (int j = 0;
j < arrs[i].length;
j++) {
if (i == 0) {
if (j == 0) {
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i+1][j] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else if(j == arrs[i].length -1){
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else{
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}
}else if (i == arrs.length -1){
if (j == 0) {
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else if(j == arrs[i].length -1){
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i-1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else{
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}
}else {
if (j == 0) {
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else if(j == arrs[i].length -1){
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
arrs[i][j] = count;
}
}else{
if (arrs[i][j] == -1) {
int count = 0;
if (arrs[i][j-1] == -2) {
count ++;
}
if (arrs[i-1][j-1] == -2) {
count ++;
}
if (arrs[i-1][j] == -2) {
count ++;
}
if (arrs[i-1][j+1] == -2) {
count ++;
}
if (arrs[i][j+1] == -2) {
count ++;
}
if (arrs[i+1][j+1] == -2) {
count ++;
}
if (arrs[i+1][j] == -2) {
count ++;
}
if (arrs[i+1][j-1] == -2) {
count ++;
}
arrs[i][j] = count;
}
}
}
}
}
return arrs;
}
}
成果展示
文章图片
推荐阅读
- jar包依赖冲突
- 笔记|scala与java的一些不同点
- Spring Boot 2.7.0发布,2.5停止维护,节奏太快了吧
- 【JAVA并发编程】Condition的实现原理
- 在WebSphere中生成堆转储,Java核心和系统转储
- 为什么强烈不推荐使用stop、suspend方法来中断线程()
- 如何分析WebSphere Java堆转储以进行故障排除
- 如何使用Jconsole在WebSphere和Monitor中启用JMX()
- 算法|hash算法详解