-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinerBoardGenerator.java
48 lines (37 loc) · 1.49 KB
/
MinerBoardGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package ru.lesson.lessons.miner.v3;
import ru.lesson.lessons.miner.v3.MinerCell;
import java.util.Random;
/**
* Created by user1 on 01.03.2017.
*/
// êëàññ äëÿ ãåíåðàöèè ìàññèâà ÿ÷ååê äëÿ èãðû "Ñàïåð"
public class MinerBoardGenerator {
// ãåíåðèðóåì ìàññèâ MinerCell[][] (x,y - ðàçìåð ïîëÿ, percentMines - âåðîÿòíîñòü ïîÿâëåíèÿ ìèíû(0-100))
public MinerCell[][] generate(int x, int y, int percentMines){
Random rnd = new Random();
MinerCell[][] minerCellArray = new MinerCell[x][y];
int[][] cntMinesAround = new int[x][y];
for(int xValue=0; xValue<x; xValue++) {
for (int yValue = 0; yValue < y; yValue++) {
boolean isMine = rnd.nextInt(100) < percentMines; // âåðîÿòíîñòü ïîëó÷èòü ìèíó
minerCellArray[xValue][yValue]=new MinerCell(isMine);
if(isMine){
for(int i=-1; i<2; i++){
for(int j=-1; j<2; j++){
try{
cntMinesAround[xValue+i][yValue+j]++;
}catch(java.lang.ArrayIndexOutOfBoundsException e){
}
}
}
}
}
}
for(int xValue=0; xValue<x; xValue++) {
for (int yValue = 0; yValue < y; yValue++) {
minerCellArray[xValue][yValue].setCntMinesAround(cntMinesAround[xValue][yValue]);
}
}
return minerCellArray;
}
}