diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperBoard.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperBoard.java index 199805b6d..696d42113 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperBoard.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperBoard.java @@ -11,4 +11,9 @@ public MinesweeperBoard(int width, int height) { public MinesweeperBoard(int size) { super(size); } + + @Override + public MinesweeperCell getCell(int x, int y) { + return (MinesweeperCell) super.getCell(x, y); + } } diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileData.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileData.java index 0e86ff63e..79666c243 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileData.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileData.java @@ -75,6 +75,22 @@ public record MinesweeperTileData(MinesweeperTileType type, int data) { return EMPTY; } + public boolean isUnset() { + return this.data == UNSET_DATA; + } + + public boolean isBomb() { + return this.data == BOMB_DATA; + } + + public boolean isEmpty() { + return this.data == EMPTY_DATA; + } + + public boolean isFlag() { + return this.data > 0 && this.data <= 8; + } + @Override public boolean equals(@Nullable Object o) { if (this == o) return true; diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperUtilities.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperUtilities.java index 6cf9e3959..26e2704f1 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperUtilities.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperUtilities.java @@ -1,4 +1,78 @@ package edu.rpi.legup.puzzle.minesweeper; +import java.awt.*; +import java.util.Objects; +import java.util.stream.IntStream; +import java.util.stream.Stream; + public final class MinesweeperUtilities { + + private static final int SURROUNDING_CELL_MIN_INDEX = 0; + private static final int SURROUNDING_CELL_MAX_INDEX = 9; + + public static Stream getSurroundingCells(MinesweeperBoard board, MinesweeperCell cell) { + final Point loc = cell.getLocation(); + final int height = board.getHeight(); + final int width = board.getWidth(); + final int x = (int) loc.getX(); + final int y = (int) loc.getY(); + // IntStream of 0-9 to represent 2D matrix of surrounding elements, + // this maps from 0,0 to 2,2 so everything needs to be shifted + // left 1 and up 1 to become + // -1,1 to 1,1 + // and 5 is skipped because we want to ignore 1,1 + return IntStream.range(SURROUNDING_CELL_MIN_INDEX, SURROUNDING_CELL_MAX_INDEX) + // skip 0,0 element + .filter(i -> i != (SURROUNDING_CELL_MAX_INDEX - SURROUNDING_CELL_MIN_INDEX) / 2) + .mapToObj(index -> { + final int newX = index / 3 - 1 + x; + final int newY = index % 3 - 1 + y; + // only keep valid locations + if (newX < 0 || newY < 0 || newX >= width || newY >= height) { + return null; + } + return board.getCell(newX, newY); + }) + .filter(Objects::nonNull); + } + + public static int countSurroundingType(MinesweeperBoard board, MinesweeperCell cell, MinesweeperTileType type) { + final Stream stream = getSurroundingCells(board, cell) + .map(MinesweeperCell::getData); + return (int) (switch (type) { + case UNSET -> stream.filter(MinesweeperTileData::isUnset); + case BOMB -> stream.filter(MinesweeperTileData::isBomb); + case EMPTY -> stream.filter(MinesweeperTileData::isEmpty); + case FLAG -> stream.filter(MinesweeperTileData::isFlag); + }).count(); + } + + public static int countSurroundingBombs(MinesweeperBoard board, MinesweeperCell cell) { + return countSurroundingType(board, cell, MinesweeperTileType.BOMB); + } + + public static int countSurroundingUnset(MinesweeperBoard board, MinesweeperCell cell) { + return countSurroundingType(board, cell, MinesweeperTileType.UNSET); + } + + public static int countSurroundingEmpty(MinesweeperBoard board, MinesweeperCell cell) { + return countSurroundingType(board, cell, MinesweeperTileType.EMPTY); + } + + public static int countSurroundingFlags(MinesweeperBoard board, MinesweeperCell cell) { + return countSurroundingType(board, cell, MinesweeperTileType.FLAG); + } + + /** + * + * @return how many bombs are left that need to be placed + * around {@code cell} which must be a flag + */ + public int countNeededBombsFromFlag(MinesweeperBoard board, MinesweeperCell cell) { + if (!cell.getData().isFlag()) { + throw new IllegalArgumentException("Bombs are only needed surrounding flags"); + } + return cell.getData().data() - countSurroundingBombs(board, cell); + } + } diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/BombOrFilledCaseRule.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/BombOrFilledCaseRule.java index 65faef5bd..f7c42dc6f 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/BombOrFilledCaseRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/BombOrFilledCaseRule.java @@ -1,4 +1,38 @@ package edu.rpi.legup.puzzle.minesweeper.rules; -public class BombOrFilledCaseRule { +import edu.rpi.legup.model.gameboard.Board; +import edu.rpi.legup.model.gameboard.CaseBoard; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import edu.rpi.legup.model.rules.CaseRule; +import edu.rpi.legup.model.tree.TreeTransition; + +import java.util.List; + +public class BombOrFilledCaseRule extends CaseRule { + + public BombOrFilledCaseRule() { + super("MINE-CASE-0000", "Bomb Or Filled", + "A cell can either be a bomb or filled.\n", + ""); + } + + @Override + public CaseBoard getCaseBoard(Board board) { + return null; + } + + @Override + public List getCases(Board board, PuzzleElement puzzleElement) { + return null; + } + + @Override + public String checkRuleRaw(TreeTransition transition) { + return null; + } + + @Override + public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) { + return null; + } } diff --git a/src/test/java/legup/TestRunner.java b/src/test/java/legup/TestRunner.java index 40cc5fa6c..95f6f4f8a 100644 --- a/src/test/java/legup/TestRunner.java +++ b/src/test/java/legup/TestRunner.java @@ -3,10 +3,11 @@ import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; -import puzzles.battleship.rules.*; +import puzzles.battleship.rules.AdjacentShipsContradictionRuleTest; +import puzzles.battleship.rules.FinishWithShipsDirectRuleTests; import puzzles.lightup.rules.*; +import puzzles.minesweeper.MinesweeperUtilitiesTest; import puzzles.nurikabe.rules.*; -import puzzles.skyscrapers.rules.*; import puzzles.treetent.rules.*; /** This class runs all of the tests for the project without needing to run build scripts. */ @@ -91,6 +92,10 @@ public static void main(String[] args) { printTestResults(result35); Result result36 = JUnitCore.runClasses(TentOrGrassCaseRuleTest.class); printTestResults(result36); + + // Minesweeper + Result result37 = JUnitCore.runClasses(MinesweeperUtilitiesTest.class); + printTestResults(result37); } private static void printTestResults(Result result) { diff --git a/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java b/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java new file mode 100644 index 000000000..4b1b450c8 --- /dev/null +++ b/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java @@ -0,0 +1,75 @@ +package puzzles.minesweeper; + +import edu.rpi.legup.puzzle.minesweeper.Minesweeper; +import edu.rpi.legup.puzzle.minesweeper.MinesweeperBoard; +import edu.rpi.legup.puzzle.minesweeper.MinesweeperCell; +import edu.rpi.legup.puzzle.minesweeper.MinesweeperUtilities; +import edu.rpi.legup.save.InvalidFileFormatException; +import legup.MockGameBoardFacade; +import legup.TestUtilities; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.stream.Stream; + +public class MinesweeperUtilitiesTest { + + private static Minesweeper minesweeper; + + @BeforeClass + public static void setUp() { + MockGameBoardFacade.getInstance(); + minesweeper = new Minesweeper(); + } + + @Test + public void getSurroundingCellsSizeThreeByThreeAtOneXOneTest() throws InvalidFileFormatException { + + TestUtilities.importTestBoard( + "puzzles/minesweeper/utilities/3x3test", + minesweeper); + + final MinesweeperBoard board = (MinesweeperBoard) minesweeper.getCurrentBoard(); + MinesweeperCell cell = board.getCell(1, 1); + + final Stream cells = MinesweeperUtilities.getSurroundingCells(board, cell); + + final long count = cells.count(); + Assert.assertEquals(count, 8); + } + + @Test + public void getSurroundingCellsSizeThreeByThreeAtZeroXZeroTest() throws InvalidFileFormatException { + + TestUtilities.importTestBoard( + "puzzles/minesweeper/utilities/3x3test", + minesweeper); + + final MinesweeperBoard board = (MinesweeperBoard) minesweeper.getCurrentBoard(); + MinesweeperCell cell = board.getCell(0, 0); + + final Stream cells = MinesweeperUtilities.getSurroundingCells(board, cell); + + final long count = cells.count(); + Assert.assertEquals(count, 3); + } + + @Test + public void getSurroundingCellsSizeThreeByThreeAtZeroXOneTest() throws InvalidFileFormatException { + + TestUtilities.importTestBoard( + "puzzles/minesweeper/utilities/3x3test", + minesweeper); + + final MinesweeperBoard board = (MinesweeperBoard) minesweeper.getCurrentBoard(); + MinesweeperCell cell = board.getCell(0, 1); + + final Stream cells = MinesweeperUtilities.getSurroundingCells(board, cell); + + final long count = cells.count(); + Assert.assertEquals(count, 5); + } + + +} diff --git a/src/test/resources/puzzles/minesweeper/utilities/3x3test b/src/test/resources/puzzles/minesweeper/utilities/3x3test new file mode 100644 index 000000000..2bf4f5c3b --- /dev/null +++ b/src/test/resources/puzzles/minesweeper/utilities/3x3test @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file