kfkama
3 years ago
3 changed files with 50 additions and 44 deletions
-
46src/main/java/Minesweeper/MinesweeperGame.java
-
46src/main/java/Minesweeper/Playfield.java
-
2src/test/java/Minesweeper/MinesweeperGameTest.java
@ -0,0 +1,46 @@ |
|||
package Minesweeper; |
|||
|
|||
public class Playfield { |
|||
|
|||
private static final int CELLSIZE = 40; |
|||
private int playfieldSize; |
|||
private MinesweeperGame MsG; |
|||
public Cell[][] playfield; |
|||
|
|||
public Playfield(MinesweeperGame _MsG, int _playfieldSize, int _bombAmount) { |
|||
MsG = _MsG; |
|||
playfieldSize = _playfieldSize; |
|||
|
|||
playfield = new Cell[playfieldSize][playfieldSize]; |
|||
|
|||
int[] bPlacement = new int[_bombAmount]; |
|||
for (int i = 0; i < bPlacement.length; i++) { |
|||
bPlacement[i] = (int) (Math.random() * playfieldSize * playfieldSize); |
|||
|
|||
for (int j = 0; j < i; j++) { |
|||
if (bPlacement[i] == bPlacement[j]) { |
|||
i--; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
for (int i = 0; i < playfieldSize; i++) { |
|||
for (int j = 0; j < playfieldSize; j++) { |
|||
playfield[i][j] = new Cell(CellType.Number); |
|||
|
|||
playfield[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - playfieldSize * CELLSIZE / 2), |
|||
i * CELLSIZE + (MsG.HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE, CELLSIZE); |
|||
MsG.add(playfield[i][j]); |
|||
|
|||
for (int k = 0; k < bPlacement.length; k++) { |
|||
if (bPlacement[k] == i * playfieldSize + j) { |
|||
playfield[i][j].type = CellType.Bomb; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue