Browse Source

Refactore Playfield

feature_Minesweeper_Playfield
kfkama 3 years ago
parent
commit
380e848e38
  1. 28
      src/main/java/Minesweeper/Playfield.java
  2. 2
      src/test/java/Minesweeper/MinesweeperGameTest.java

28
src/main/java/Minesweeper/Playfield.java

@ -3,19 +3,19 @@ package Minesweeper;
public class Playfield { public class Playfield {
private static final int CELLSIZE = 40; private static final int CELLSIZE = 40;
private int playfieldSize;
private int Size;
private MinesweeperGame MsG; private MinesweeperGame MsG;
public Cell[][] playfield;
public Cell[][] cells;
public Playfield(MinesweeperGame _MsG, int _playfieldSize, int _bombAmount) {
public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) {
MsG = _MsG; MsG = _MsG;
playfieldSize = _playfieldSize;
Size = _Size;
playfield = new Cell[playfieldSize][playfieldSize];
cells = new Cell[Size][Size];
int[] bPlacement = new int[_bombAmount]; int[] bPlacement = new int[_bombAmount];
for (int i = 0; i < bPlacement.length; i++) { for (int i = 0; i < bPlacement.length; i++) {
bPlacement[i] = (int) (Math.random() * playfieldSize * playfieldSize);
bPlacement[i] = (int) (Math.random() * Size * Size);
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
if (bPlacement[i] == bPlacement[j]) { if (bPlacement[i] == bPlacement[j]) {
@ -25,17 +25,17 @@ public class Playfield {
} }
} }
for (int i = 0; i < playfieldSize; i++) {
for (int j = 0; j < playfieldSize; j++) {
playfield[i][j] = new Cell(CellType.Number);
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
cells[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]);
cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2),
i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE);
MsG.add(cells[i][j]);
for (int k = 0; k < bPlacement.length; k++) { for (int k = 0; k < bPlacement.length; k++) {
if (bPlacement[k] == i * playfieldSize + j) {
playfield[i][j].type = CellType.Bomb;
if (bPlacement[k] == i * Size + j) {
cells[i][j].type = CellType.Bomb;
break; break;
} }
} }

2
src/test/java/Minesweeper/MinesweeperGameTest.java

@ -17,7 +17,7 @@ class MinesweeperGameTest {
int bombCounter = 0; int bombCounter = 0;
for(Cell[] row : m.playfield.playfield){
for(Cell[] row : m.playfield.cells){
for (Cell c : row) { for (Cell c : row) {
if(c.type == CellType.Bomb) { if(c.type == CellType.Bomb) {
bombCounter++; bombCounter++;

Loading…
Cancel
Save