Browse Source

Moved playfield logic to new class

feature_Minesweeper_Playfield
kfkama 3 years ago
parent
commit
aa04d679bd
  1. 46
      src/main/java/Minesweeper/MinesweeperGame.java
  2. 46
      src/main/java/Minesweeper/Playfield.java
  3. 2
      src/test/java/Minesweeper/MinesweeperGameTest.java

46
src/main/java/Minesweeper/MinesweeperGame.java

@ -7,54 +7,14 @@ import javax.swing.JPanel;
public class MinesweeperGame extends JPanel {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600, HEIGTH = 600;
private static final int CELLSIZE = 40;
private int playfieldSize;
private int bombAmount;
public static final int WIDTH = 600, HEIGTH = 600;
public Cell[][] playfield;
public Playfield playfield;
public MinesweeperGame(int _playfieldSize, int _bombAmount) {
this.setSize(WIDTH, HEIGTH);
playfieldSize = _playfieldSize;
bombAmount = _bombAmount;
setLayout(null);
initPlayfield();
}
private void initPlayfield() {
playfield = new Cell[playfieldSize][playfieldSize];
int[] bPlacment = new int[bombAmount];
for (int i = 0; i < bPlacment.length; i++) {
bPlacment[i] = (int) (Math.random() * playfieldSize * playfieldSize);
for (int j = 0; j < i; j++) {
if (bPlacment[i] == bPlacment[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 + (WIDTH / 2 - playfieldSize * CELLSIZE / 2),
i * CELLSIZE + (HEIGTH / 2 - playfieldSize * CELLSIZE / 2), CELLSIZE, CELLSIZE);
add(playfield[i][j]);
for (int k = 0; k < bPlacment.length; k++) {
if (bPlacment[k] == i * playfieldSize + j) {
playfield[i][j].type = CellType.Bomb;
break;
}
}
}
}
playfield = new Playfield(this, _playfieldSize, _bombAmount );
}
public static void main(String[] args) {

46
src/main/java/Minesweeper/Playfield.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;
}
}
}
}
}
}

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

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

Loading…
Cancel
Save