扫雷模型(非完全一样)

 1 package top.dreamcenter.core;
 2 
 3 import java.util.Random;
 4 
 5 public class Saolei {
 6     
 7     /**
 8      * 获得矩阵
 9      * @param width    矩阵尺寸
10      * @param num    矩阵雷数
11      */
12     public static int[][] get(int width,int num) {
13         //+2是为了简化后面检测雷数,坐标x,y都要+1
14         //0-9 显示的数字,-1雷
15         int space[][] = new int[width+2][width+2];
16         //0掩盖,1揭露
17 //        int cover[][] = new int[width+2][width+2];
18         
19         //随机类对象
20         Random rm = new Random();
21         
22         //部署雷
23         for (int i = 0; i < num; i++) {
24             int x = rm.nextInt(width);
25             int y = rm.nextInt(width);
26             if(space[x+1][y+1] != -1) {
27                 space[x+1][y+1] = -1;
28             }else {
29                 i--;
30             }
31         }
32         
33         //检测雷数量
34         for (int i = 0; i < width; i++) {
35             for (int j = 0; j < width; j++) {
36                 if(space[i+1][j+1]!=-1) {
37                     //小范围检测
38                     for(int sy = 0;sy <3;sy++) {
39                         for(int sx = 0;sx <3;sx++) {
40                             if(space[i+sy][j+sx]==-1) {
41                                 space[i+1][j+1]++;
42                             }
43                         }
44                     }
45                 }
46             }
47         }
48         
49         return space;
50     }
51 
52 }
程序宅男
原文地址:https://www.cnblogs.com/dreamcenter/p/12753741.html