直接上代码
[Java] 纯文本查看 复制代码 //随机生成地雷数
int numOfMines=10;
//地图尺寸
int mapSize=9;
Random r=new Random();
//用二位数组做地图
int [][] map=new int[mapSize][mapSize];
//地雷周围的偏移量
int[]around={-1,0,1};
//开始生成
for (int i=0;i<numOfMines;i++){
int x,y;
do {
x=r.nextInt(mapSize);
y=r.nextInt(mapSize);
}while(map[x][y]>=100);
//埋雷
map[x][y]=100;
//周围的提示
for (int dy:around){
for (int dx:around){
if (dx==0 && dy==0){
continue;
}
if ((x+dx)>=0
&& (x+dx)<mapSize
&& (y+dy)>=0
&& (y+dy)<mapSize){
try {
map[x+dx][y+dy]++;
} catch (Exception e) {
System.out.println(x+dx+" "+y+dy);
e.printStackTrace();
}
}
}
}
}
for (int y=0;y<mapSize;y++){
for (int x=0;x<mapSize;x++){
if (map[x][y]>=100){
System.out.print("[*]");
}else if (map[x][y]==0){
System.out.print("[ ]");
}else {
System.out.print("["+map[x][y]+"]");
}
}
System.out.println();
}
|