From 0e2ea58e5721989641caa2f74419a3a8390c86c2 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 12:56:43 +0100 Subject: [PATCH] Add bomb proximity with test --- src/main/java/Minesweeper/Cell.java | 2 +- .../java/Minesweeper/MinesweeperGame.java | 3 +- src/main/java/Minesweeper/Playfield.java | 62 ++++++++++++++++++- .../java/Minesweeper/MinesweeperGameTest.java | 12 ++++ 4 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 12a1935..36946f8 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/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; diff --git a/src/main/java/Minesweeper/MinesweeperGame.java b/src/main/java/Minesweeper/MinesweeperGame.java index df5d33c..fde06cd 100644 --- a/src/main/java/Minesweeper/MinesweeperGame.java +++ b/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); diff --git a/src/main/java/Minesweeper/Playfield.java b/src/main/java/Minesweeper/Playfield.java index aa843a6..a56dbc6 100644 --- a/src/main/java/Minesweeper/Playfield.java +++ b/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; @@ -10,7 +10,7 @@ public class Playfield { public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { MsG = _MsG; Size = _Size; - + cells = new Cell[Size][Size]; int[] bPlacement = new int[_bombAmount]; @@ -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++; + } + } } } diff --git a/src/test/java/Minesweeper/MinesweeperGameTest.java b/src/test/java/Minesweeper/MinesweeperGameTest.java index 2dce81d..4b61967 100644 --- a/src/test/java/Minesweeper/MinesweeperGameTest.java +++ b/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 testBombs(){ return Stream.of( @@ -38,4 +48,6 @@ class MinesweeperGameTest { ); } + + }