Browse Source

Add bomb proximity with test

feature_Minesweeper_Playfield
kfkama 3 years ago
parent
commit
0e2ea58e57
  1. 2
      src/main/java/Minesweeper/Cell.java
  2. 3
      src/main/java/Minesweeper/MinesweeperGame.java
  3. 60
      src/main/java/Minesweeper/Playfield.java
  4. 12
      src/test/java/Minesweeper/MinesweeperGameTest.java

2
src/main/java/Minesweeper/Cell.java

@ -8,7 +8,7 @@ public class Cell extends JButton {
private static final long serialVersionUID = 1L;
public CellType type;
public boolean flagged = false;
public int value = -1;
public int value = 0;
public Cell(CellType _type) {
type = _type;

3
src/main/java/Minesweeper/MinesweeperGame.java

@ -8,7 +8,6 @@ public class MinesweeperGame extends JPanel {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 600, HEIGTH = 600;
public Playfield playfield;
public MinesweeperGame(int _playfieldSize, int _bombAmount) {
@ -19,7 +18,7 @@ public class MinesweeperGame extends JPanel {
public static void main(String[] args) {
JFrame f = new JFrame();
MinesweeperGame ttt = new MinesweeperGame(8, 10);
MinesweeperGame ttt = new MinesweeperGame(3, 0);
f.add(ttt);
f.setSize(WIDTH, HEIGTH);

60
src/main/java/Minesweeper/Playfield.java

@ -2,7 +2,7 @@ package Minesweeper;
public class Playfield {
private static final int CELLSIZE = 40;
private static final int CELLSIZE = 50;
private int Size;
private MinesweeperGame MsG;
public Cell[][] cells;
@ -41,6 +41,64 @@ public class Playfield {
}
}
}
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
if (cells[i][j].type == CellType.Number) {
calculateBombProximity(i, j);
}
}
}
}
public void calculateBombProximity(int row, int column) {
if (row > 0) {
if (column > 0) {
if (cells[row - 1][column - 1].type == CellType.Bomb) {
cells[row][column].value++;
}
}
if (cells[row - 1][column].type == CellType.Bomb) {
cells[row][column].value++;
}
if (column < cells.length - 1) {
if (cells[row - 1][column + 1].type == CellType.Bomb) {
cells[row][column].value++;
}
}
}
if (row < cells.length - 1) {
if (column > 0) {
if (cells[row + 1][column - 1].type == CellType.Bomb) {
cells[row][column].value++;
}
}
if (cells[row + 1][column].type == CellType.Bomb) {
cells[row][column].value++;
}
if (column < cells.length - 1) {
if (cells[row + 1][column + 1].type == CellType.Bomb) {
cells[row][column].value++;
}
}
}
if (column > 0) {
if (cells[row][column - 1].type == CellType.Bomb) {
cells[row][column].value++;
}
}
if (column < cells.length - 1) {
if (cells[row][column + 1].type == CellType.Bomb) {
cells[row][column].value++;
}
}
}
}

12
src/test/java/Minesweeper/MinesweeperGameTest.java

@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.*;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@ -27,6 +28,15 @@ class MinesweeperGameTest {
assertEquals(_bombAmount, bombCounter);
}
@Test
void testProximityPoints() {
MinesweeperGame m = new MinesweeperGame(3, 0);
m.playfield.cells[0][0].type = CellType.Bomb;
m.playfield.calculateBombProximity(1, 1);
assertEquals(1, m.playfield.cells[1][1].value);
}
private static Stream<Arguments> testBombs(){
return Stream.of(
@ -38,4 +48,6 @@ class MinesweeperGameTest {
);
}
}
Loading…
Cancel
Save