From f9d52a3eb0c5fe522beb33635816a2a3154c88ed Mon Sep 17 00:00:00 2001 From: Bram van Heuveln Date: Fri, 22 Mar 2024 21:48:49 +0000 Subject: [PATCH] Automated Java code formatting changes --- .../legup/puzzle/minesweeper/Minesweeper.java | 131 +++++----- .../puzzle/minesweeper/MinesweeperBoard.java | 72 ++--- .../puzzle/minesweeper/MinesweeperCell.java | 149 ++++++----- .../minesweeper/MinesweeperCellFactory.java | 211 +++++++-------- .../minesweeper/MinesweeperController.java | 120 ++++----- .../MinesweeperElementIdentifiers.java | 38 ++- .../minesweeper/MinesweeperElementView.java | 153 ++++++----- .../minesweeper/MinesweeperExporter.java | 88 +++---- .../minesweeper/MinesweeperImporter.java | 247 +++++++++--------- .../minesweeper/MinesweeperTileData.java | 213 +++++++-------- .../minesweeper/MinesweeperTileType.java | 38 +-- .../minesweeper/MinesweeperUtilities.java | 79 +++--- .../puzzle/minesweeper/MinesweeperView.java | 96 +++---- .../puzzle/minesweeper/elements/BombTile.java | 9 +- .../minesweeper/elements/EmptyTile.java | 6 +- .../puzzle/minesweeper/elements/FlagTile.java | 9 +- .../puzzle/minesweeper/elements/Unset.java | 8 +- .../minesweeper/elements/UnsetTile.java | 7 +- .../rules/BombOrFilledCaseRule.java | 12 +- .../LessBombsThanFlagContradictionRule.java | 17 +- .../rules/SatisfyFlagCaseRule.java | 42 +-- .../minesweeper/MinesweeperUtilitiesTest.java | 26 +- 22 files changed, 883 insertions(+), 888 deletions(-) diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/Minesweeper.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/Minesweeper.java index dd457f3d2..ed8066f39 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/Minesweeper.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/Minesweeper.java @@ -1,67 +1,64 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.model.Puzzle; -import edu.rpi.legup.model.gameboard.Board; -import edu.rpi.legup.model.gameboard.PuzzleElement; -import edu.rpi.legup.model.rules.ContradictionRule; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class Minesweeper extends Puzzle { - - public static final String NAME = "Minesweeper"; - - public Minesweeper() { - this.name = NAME; - this.importer = new MinesweeperImporter(this); - this.exporter = new MinesweeperExporter(this); - this.factory = MinesweeperCellFactory.INSTANCE; - } - - @Override - @Contract(pure = false) - public void initializeView() { - this.boardView = new MinesweeperView((MinesweeperBoard) this.currentBoard); - this.boardView.setBoard(this.currentBoard); - addBoardListener(boardView); - } - - @Override - @Contract("_ -> null") - public @Nullable Board generatePuzzle(int difficulty) { - return null; - } - - @Override - @Contract(pure = true) - public boolean isBoardComplete(@NotNull Board board) { - MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board; - - for (ContradictionRule rule : contradictionRules) { - if (rule.checkContradiction(minesweeperBoard) == null) { - return false; - } - } - for (PuzzleElement data : minesweeperBoard.getPuzzleElements()) { - final MinesweeperCell cell = (MinesweeperCell) data; - if (cell.getData().equals(MinesweeperTileData.empty())) { - return false; - } - } - return true; - } - - @Override - @Contract(pure = true) - public void onBoardChange(@NotNull Board board) { - - } - - @Override - @Contract(pure = true) - public boolean isValidDimensions(int rows, int columns) { - return rows >= 1 && columns >= 1; - } - -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.model.Puzzle; +import edu.rpi.legup.model.gameboard.Board; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import edu.rpi.legup.model.rules.ContradictionRule; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class Minesweeper extends Puzzle { + + public static final String NAME = "Minesweeper"; + + public Minesweeper() { + this.name = NAME; + this.importer = new MinesweeperImporter(this); + this.exporter = new MinesweeperExporter(this); + this.factory = MinesweeperCellFactory.INSTANCE; + } + + @Override + @Contract(pure = false) + public void initializeView() { + this.boardView = new MinesweeperView((MinesweeperBoard) this.currentBoard); + this.boardView.setBoard(this.currentBoard); + addBoardListener(boardView); + } + + @Override + @Contract("_ -> null") + public @Nullable Board generatePuzzle(int difficulty) { + return null; + } + + @Override + @Contract(pure = true) + public boolean isBoardComplete(@NotNull Board board) { + MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board; + + for (ContradictionRule rule : contradictionRules) { + if (rule.checkContradiction(minesweeperBoard) == null) { + return false; + } + } + for (PuzzleElement data : minesweeperBoard.getPuzzleElements()) { + final MinesweeperCell cell = (MinesweeperCell) data; + if (cell.getData().equals(MinesweeperTileData.empty())) { + return false; + } + } + return true; + } + + @Override + @Contract(pure = true) + public void onBoardChange(@NotNull Board board) {} + + @Override + @Contract(pure = true) + public boolean isValidDimensions(int rows, int columns) { + return rows >= 1 && columns >= 1; + } +} 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 ea6f560e1..bef317ab5 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperBoard.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperBoard.java @@ -1,36 +1,36 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.model.gameboard.GridBoard; - -public class MinesweeperBoard extends GridBoard { - - public MinesweeperBoard(int width, int height) { - super(width, height); - } - - public MinesweeperBoard(int size) { - super(size); - } - - @Override - public MinesweeperCell getCell(int x, int y) { - return (MinesweeperCell) super.getCell(x, y); - } - - - /** - * Performs a deep copy of the Board - * - * @return a new copy of the board that is independent of this one - */ - @Override - public MinesweeperBoard copy() { - MinesweeperBoard newMinesweeperBoard = new MinesweeperBoard(this.dimension.width, this.dimension.height); - for (int x = 0; x < this.dimension.width; x++) { - for (int y = 0; y < this.dimension.height; y++) { - newMinesweeperBoard.setCell(x, y, getCell(x, y).copy()); - } - } - return newMinesweeperBoard; - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.model.gameboard.GridBoard; + +public class MinesweeperBoard extends GridBoard { + + public MinesweeperBoard(int width, int height) { + super(width, height); + } + + public MinesweeperBoard(int size) { + super(size); + } + + @Override + public MinesweeperCell getCell(int x, int y) { + return (MinesweeperCell) super.getCell(x, y); + } + + /** + * Performs a deep copy of the Board + * + * @return a new copy of the board that is independent of this one + */ + @Override + public MinesweeperBoard copy() { + MinesweeperBoard newMinesweeperBoard = + new MinesweeperBoard(this.dimension.width, this.dimension.height); + for (int x = 0; x < this.dimension.width; x++) { + for (int y = 0; y < this.dimension.height; y++) { + newMinesweeperBoard.setCell(x, y, getCell(x, y).copy()); + } + } + return newMinesweeperBoard; + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCell.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCell.java index 6770e5263..325e42b7b 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCell.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCell.java @@ -1,76 +1,73 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.model.elements.Element; -import edu.rpi.legup.model.gameboard.GridCell; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.awt.*; -import java.awt.event.MouseEvent; - -public class MinesweeperCell extends GridCell { - - public MinesweeperCell(@NotNull MinesweeperTileData value, @NotNull Point location) { - super(value, location); - } - - public @NotNull MinesweeperTileType getTileType() { - return super.data.type(); - } - - public @NotNull int getTileNumber() { - return super.data.data(); - } - - @Override - @Contract(pure = false) - /** - * Sets this cell's data to the value specified by {@link Element#getElementID()} - */ - public void setType(@NotNull Element element, @NotNull MouseEvent event) { - switch (element.getElementID()) { - case MinesweeperElementIdentifiers.BOMB -> { - this.data = MinesweeperTileData.bomb(); - break; - } - case MinesweeperElementIdentifiers.FLAG -> { - final int currentData = super.data.data(); - switch (event.getButton()) { - case MouseEvent.BUTTON1 -> { - if (currentData >= 8) { - this.data = MinesweeperTileData.empty(); - return; - } - this.data = MinesweeperTileData.flag(currentData + 1); - return; - } - case MouseEvent.BUTTON2, MouseEvent.BUTTON3 -> { - if (currentData <= 0) { - this.data = MinesweeperTileData.empty(); - return; - } - this.data = MinesweeperTileData.flag(currentData - 1); - return; - } - } - } - default -> { - this.data = MinesweeperTileData.empty(); - } - } - } - - public void setCellType(MinesweeperTileData type){ - this.data = type; - } - - @Override - @Contract(pure = true) - public @NotNull MinesweeperCell copy() { - MinesweeperCell copy = new MinesweeperCell(data, (Point) location.clone()); - copy.setIndex(index); - copy.setModifiable(isModifiable); - copy.setGiven(isGiven); - return copy; - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.model.elements.Element; +import edu.rpi.legup.model.gameboard.GridCell; +import java.awt.*; +import java.awt.event.MouseEvent; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +public class MinesweeperCell extends GridCell { + + public MinesweeperCell(@NotNull MinesweeperTileData value, @NotNull Point location) { + super(value, location); + } + + public @NotNull MinesweeperTileType getTileType() { + return super.data.type(); + } + + public @NotNull int getTileNumber() { + return super.data.data(); + } + + @Override + @Contract(pure = false) + /** Sets this cell's data to the value specified by {@link Element#getElementID()} */ + public void setType(@NotNull Element element, @NotNull MouseEvent event) { + switch (element.getElementID()) { + case MinesweeperElementIdentifiers.BOMB -> { + this.data = MinesweeperTileData.bomb(); + break; + } + case MinesweeperElementIdentifiers.FLAG -> { + final int currentData = super.data.data(); + switch (event.getButton()) { + case MouseEvent.BUTTON1 -> { + if (currentData >= 8) { + this.data = MinesweeperTileData.empty(); + return; + } + this.data = MinesweeperTileData.flag(currentData + 1); + return; + } + case MouseEvent.BUTTON2, MouseEvent.BUTTON3 -> { + if (currentData <= 0) { + this.data = MinesweeperTileData.empty(); + return; + } + this.data = MinesweeperTileData.flag(currentData - 1); + return; + } + } + } + default -> { + this.data = MinesweeperTileData.empty(); + } + } + } + + public void setCellType(MinesweeperTileData type) { + this.data = type; + } + + @Override + @Contract(pure = true) + public @NotNull MinesweeperCell copy() { + MinesweeperCell copy = new MinesweeperCell(data, (Point) location.clone()); + copy.setIndex(index); + copy.setModifiable(isModifiable); + copy.setGiven(isGiven); + return copy; + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCellFactory.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCellFactory.java index 45957cb82..5fe6096a9 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCellFactory.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperCellFactory.java @@ -1,110 +1,101 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.model.gameboard.Board; -import edu.rpi.legup.model.gameboard.ElementFactory; -import edu.rpi.legup.model.gameboard.PuzzleElement; -import edu.rpi.legup.save.InvalidFileFormatException; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; - -import java.awt.*; - -public class MinesweeperCellFactory extends ElementFactory { - - /** - * The key of the data used in {@link NamedNodeMap} - */ - private static final String DATA_ATTRIBUTE = "data"; - /** - * The key of the x position used in {@link NamedNodeMap} - */ - private static final String X_ATTRIBUTE = "x"; - /** - * The key of the y position used in {@link NamedNodeMap} - */ - private static final String Y_ATTRIBUTE = "y"; - - - private MinesweeperCellFactory() { - } - - public static final MinesweeperCellFactory INSTANCE = new MinesweeperCellFactory(); - - /** - * - * @param node node that represents the puzzleElement - * @param board Board to use to verify the newly created {@link MinesweeperCell} - * is valid - * @return a new {@link MinesweeperCell} - * @throws InvalidFileFormatException If the node name is not "cell" - * @throws NumberFormatException If the {@link #X_ATTRIBUTE} or {@link #Y_ATTRIBUTE} - * is not a number - * @throws NullPointerException If one of {@link #DATA_ATTRIBUTE}, {@link #X_ATTRIBUTE} or {@link #Y_ATTRIBUTE} - * does not exist. - */ - @Override - @Contract(pure = false) - public @NotNull PuzzleElement importCell( - @NotNull Node node, - @NotNull Board board - ) throws InvalidFileFormatException { - try { - if (!node.getNodeName().equalsIgnoreCase("cell")) { - throw new InvalidFileFormatException("Minesweeper Factory: unknown puzzleElement puzzleElement"); - } - - MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board; - final int width = minesweeperBoard.getWidth(); - final int height = minesweeperBoard.getHeight(); - - final NamedNodeMap attributeList = node.getAttributes(); - final int value = Integer.parseInt(attributeList.getNamedItem(DATA_ATTRIBUTE).getNodeValue()); - final int x = Integer.parseInt(attributeList.getNamedItem(X_ATTRIBUTE).getNodeValue()); - final int y = Integer.parseInt(attributeList.getNamedItem(Y_ATTRIBUTE).getNodeValue()); - if (x >= width || y >= height) { - throw new InvalidFileFormatException("Minesweeper Factory: cell location out of bounds"); - } - if (value < -2) { - throw new InvalidFileFormatException("Minesweeper Factory: cell unknown value"); - } - final MinesweeperCell cell = new MinesweeperCell(MinesweeperTileData.fromData(value), new Point(x, y)); - cell.setIndex(y * height + x); - return cell; - } - catch (NumberFormatException e) { - throw new InvalidFileFormatException("Minesweeper Factory: unknown value where integer expected"); - } - catch (NullPointerException e) { - throw new InvalidFileFormatException("Minesweeper Factory: could not find attribute(s)"); - } - } - - /** - * - * @param document Document used to create the element - * @param puzzleElement PuzzleElement cell - * @return a {@link Element} that contains the {@link #DATA_ATTRIBUTE}, - * {@link #X_ATTRIBUTE}, and {@link #Y_ATTRIBUTE} - */ - @Override - @Contract(pure = false) - public @NotNull Element exportCell( - @NotNull Document document, - @SuppressWarnings("rawtypes") @NotNull PuzzleElement puzzleElement - ) { - org.w3c.dom.Element cellElement = document.createElement("cell"); - - MinesweeperCell cell = (MinesweeperCell) puzzleElement; - Point loc = cell.getLocation(); - - cellElement.setAttribute(DATA_ATTRIBUTE, String.valueOf(cell.getData())); - cellElement.setAttribute(X_ATTRIBUTE, String.valueOf(loc.x)); - cellElement.setAttribute(Y_ATTRIBUTE, String.valueOf(loc.y)); - - return cellElement; - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.model.gameboard.Board; +import edu.rpi.legup.model.gameboard.ElementFactory; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import edu.rpi.legup.save.InvalidFileFormatException; +import java.awt.*; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +public class MinesweeperCellFactory extends ElementFactory { + + /** The key of the data used in {@link NamedNodeMap} */ + private static final String DATA_ATTRIBUTE = "data"; + + /** The key of the x position used in {@link NamedNodeMap} */ + private static final String X_ATTRIBUTE = "x"; + + /** The key of the y position used in {@link NamedNodeMap} */ + private static final String Y_ATTRIBUTE = "y"; + + private MinesweeperCellFactory() {} + + public static final MinesweeperCellFactory INSTANCE = new MinesweeperCellFactory(); + + /** + * @param node node that represents the puzzleElement + * @param board Board to use to verify the newly created {@link MinesweeperCell} is valid + * @return a new {@link MinesweeperCell} + * @throws InvalidFileFormatException If the node name is not "cell" + * @throws NumberFormatException If the {@link #X_ATTRIBUTE} or {@link #Y_ATTRIBUTE} is not a + * number + * @throws NullPointerException If one of {@link #DATA_ATTRIBUTE}, {@link #X_ATTRIBUTE} or + * {@link #Y_ATTRIBUTE} does not exist. + */ + @Override + @Contract(pure = false) + public @NotNull PuzzleElement importCell( + @NotNull Node node, @NotNull Board board) throws InvalidFileFormatException { + try { + if (!node.getNodeName().equalsIgnoreCase("cell")) { + throw new InvalidFileFormatException( + "Minesweeper Factory: unknown puzzleElement puzzleElement"); + } + + MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board; + final int width = minesweeperBoard.getWidth(); + final int height = minesweeperBoard.getHeight(); + + final NamedNodeMap attributeList = node.getAttributes(); + final int value = + Integer.parseInt(attributeList.getNamedItem(DATA_ATTRIBUTE).getNodeValue()); + final int x = Integer.parseInt(attributeList.getNamedItem(X_ATTRIBUTE).getNodeValue()); + final int y = Integer.parseInt(attributeList.getNamedItem(Y_ATTRIBUTE).getNodeValue()); + if (x >= width || y >= height) { + throw new InvalidFileFormatException( + "Minesweeper Factory: cell location out of bounds"); + } + if (value < -2) { + throw new InvalidFileFormatException("Minesweeper Factory: cell unknown value"); + } + final MinesweeperCell cell = + new MinesweeperCell(MinesweeperTileData.fromData(value), new Point(x, y)); + cell.setIndex(y * height + x); + return cell; + } catch (NumberFormatException e) { + throw new InvalidFileFormatException( + "Minesweeper Factory: unknown value where integer expected"); + } catch (NullPointerException e) { + throw new InvalidFileFormatException( + "Minesweeper Factory: could not find attribute(s)"); + } + } + + /** + * @param document Document used to create the element + * @param puzzleElement PuzzleElement cell + * @return a {@link Element} that contains the {@link #DATA_ATTRIBUTE}, {@link #X_ATTRIBUTE}, + * and {@link #Y_ATTRIBUTE} + */ + @Override + @Contract(pure = false) + public @NotNull Element exportCell( + @NotNull Document document, + @SuppressWarnings("rawtypes") @NotNull PuzzleElement puzzleElement) { + org.w3c.dom.Element cellElement = document.createElement("cell"); + + MinesweeperCell cell = (MinesweeperCell) puzzleElement; + Point loc = cell.getLocation(); + + cellElement.setAttribute(DATA_ATTRIBUTE, String.valueOf(cell.getData())); + cellElement.setAttribute(X_ATTRIBUTE, String.valueOf(loc.x)); + cellElement.setAttribute(Y_ATTRIBUTE, String.valueOf(loc.y)); + + return cellElement; + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperController.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperController.java index 7289c349c..aaf061704 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperController.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperController.java @@ -1,63 +1,57 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.controller.ElementController; -import edu.rpi.legup.model.gameboard.PuzzleElement; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.awt.event.MouseEvent; - -public class MinesweeperController extends ElementController { - - /** - * If the button clicked was button 1, then {@link MinesweeperTileData#fromData(int)} - * is called with a value of {@code current.data() + 1}. - * If the button clicked was button 2 or 3, then {@link MinesweeperTileData#fromData(int)} - * is called with a value of {@code currentData() - 1} - * Otherwise {@link MinesweeperTileData#empty()} is returned. - * - * @param event The user's click data - * @param current The current data at the cell they clicked on - * @return A different cell data depending on what the current data is - */ - @Contract(pure = true) - public static @NotNull MinesweeperTileData getNewCellDataOnClick( - @NotNull MouseEvent event, - @NotNull MinesweeperTileData current - ) { - final int numberData = current.data(); - return switch (event.getButton()) { - case MouseEvent.BUTTON1 -> MinesweeperTileData.fromData(numberData + 1); - case MouseEvent.BUTTON2, - MouseEvent.BUTTON3 -> MinesweeperTileData.fromData(numberData - 1); - default -> MinesweeperTileData.empty(); - }; - } - - /** - * @see #getNewCellDataOnClick(MouseEvent, MinesweeperTileData) - * @param event The user's click data - * @param data The current data at the cell they clicked on - */ - @Override - @SuppressWarnings("unchecked") - @Contract(pure = false) - public void changeCell( - @NotNull MouseEvent event, - @SuppressWarnings("rawtypes") @NotNull PuzzleElement data - ) { - final MinesweeperCell cell = (MinesweeperCell) data; - if (event.isControlDown()) { - this.boardView.getSelectionPopupMenu().show( - boardView, - this.boardView.getCanvas().getX() + event.getX(), - this.boardView.getCanvas().getY() + event.getY() - ); - return; - } - - final MinesweeperTileData newData = getNewCellDataOnClick(event, cell.getData()); - data.setData(newData); - } -} - +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.controller.ElementController; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import java.awt.event.MouseEvent; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +public class MinesweeperController extends ElementController { + + /** + * If the button clicked was button 1, then {@link MinesweeperTileData#fromData(int)} is called + * with a value of {@code current.data() + 1}. If the button clicked was button 2 or 3, then + * {@link MinesweeperTileData#fromData(int)} is called with a value of {@code currentData() - 1} + * Otherwise {@link MinesweeperTileData#empty()} is returned. + * + * @param event The user's click data + * @param current The current data at the cell they clicked on + * @return A different cell data depending on what the current data is + */ + @Contract(pure = true) + public static @NotNull MinesweeperTileData getNewCellDataOnClick( + @NotNull MouseEvent event, @NotNull MinesweeperTileData current) { + final int numberData = current.data(); + return switch (event.getButton()) { + case MouseEvent.BUTTON1 -> MinesweeperTileData.fromData(numberData + 1); + case MouseEvent.BUTTON2, MouseEvent.BUTTON3 -> + MinesweeperTileData.fromData(numberData - 1); + default -> MinesweeperTileData.empty(); + }; + } + + /** + * @see #getNewCellDataOnClick(MouseEvent, MinesweeperTileData) + * @param event The user's click data + * @param data The current data at the cell they clicked on + */ + @Override + @SuppressWarnings("unchecked") + @Contract(pure = false) + public void changeCell( + @NotNull MouseEvent event, @SuppressWarnings("rawtypes") @NotNull PuzzleElement data) { + final MinesweeperCell cell = (MinesweeperCell) data; + if (event.isControlDown()) { + this.boardView + .getSelectionPopupMenu() + .show( + boardView, + this.boardView.getCanvas().getX() + event.getX(), + this.boardView.getCanvas().getY() + event.getY()); + return; + } + + final MinesweeperTileData newData = getNewCellDataOnClick(event, cell.getData()); + data.setData(newData); + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementIdentifiers.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementIdentifiers.java index 1b626a33b..77e490f7e 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementIdentifiers.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementIdentifiers.java @@ -1,22 +1,16 @@ -package edu.rpi.legup.puzzle.minesweeper; - -public class MinesweeperElementIdentifiers { - - /** - * ID for unset Minesweeper elements - */ - public static final String UNSET = "MINESWEEPER-UNSET"; - /** - * ID for bomb Minesweeper elements - */ - public static final String BOMB = "MINESWEEPER-BOMB"; - /** - * ID for empty Minesweeper elements - */ - public static final String EMPTY = "MINESWEEPER-EMPTY"; - /** - * ID for flag Minesweeper elements - */ - public static final String FLAG = "MINESWEEPER-FLAG"; - -} +package edu.rpi.legup.puzzle.minesweeper; + +public class MinesweeperElementIdentifiers { + + /** ID for unset Minesweeper elements */ + public static final String UNSET = "MINESWEEPER-UNSET"; + + /** ID for bomb Minesweeper elements */ + public static final String BOMB = "MINESWEEPER-BOMB"; + + /** ID for empty Minesweeper elements */ + public static final String EMPTY = "MINESWEEPER-EMPTY"; + + /** ID for flag Minesweeper elements */ + public static final String FLAG = "MINESWEEPER-FLAG"; +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementView.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementView.java index e9658a077..fd5d73928 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementView.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperElementView.java @@ -1,77 +1,76 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.puzzle.lightup.LightUpView; -import edu.rpi.legup.ui.boardview.GridElementView; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; - -import java.awt.*; - -public class MinesweeperElementView extends GridElementView { - - private static final Font FONT = new Font("TimesRoman", Font.BOLD, 16); - private static final Color FONT_COLOR = Color.BLACK; - - public MinesweeperElementView(@NotNull MinesweeperCell cell) { - super(cell); - } - - @Override - public @NotNull MinesweeperCell getPuzzleElement() { - return (MinesweeperCell) super.getPuzzleElement(); - } - - @Override - @SuppressWarnings("Duplicates") - @Contract(pure = true) - public void drawElement(@NotNull Graphics2D graphics2D) { - final MinesweeperCell cell = (MinesweeperCell) puzzleElement; - final MinesweeperTileType type = cell.getTileType(); - if (type == MinesweeperTileType.FLAG) { - graphics2D.setStroke(new BasicStroke(1)); - graphics2D.setColor(Color.WHITE); - graphics2D.fillRect(location.x, location.y, size.width, size.height); - - graphics2D.setColor(Color.BLACK); - graphics2D.drawRect(location.x, location.y, size.width, size.height); - - graphics2D.setColor(FONT_COLOR); - graphics2D.setFont(FONT); - final FontMetrics metrics = graphics2D.getFontMetrics(FONT); - final String value = String.valueOf(((MinesweeperCell) puzzleElement).getData().data()); - final int xText = location.x + (size.width - metrics.stringWidth(value)) / 2; - final int yText = location.y + ((size.height - metrics.getHeight()) / 2) + metrics.getAscent(); - graphics2D.drawString(value, xText, yText); - return; - } - if (type == MinesweeperTileType.UNSET) { - graphics2D.setStroke(new BasicStroke(1)); - graphics2D.setColor(Color.BLACK); - graphics2D.drawRect(location.x, location.y, size.width, size.height); - graphics2D.setColor(Color.DARK_GRAY); - graphics2D.fillRect(location.x, location.y, size.width, size.height); - return; - } - if (type == MinesweeperTileType.EMPTY) { - graphics2D.setStroke(new BasicStroke(1)); - graphics2D.setColor(Color.BLACK); - graphics2D.drawRect(location.x, location.y, size.width, size.height); - graphics2D.setColor(Color.GRAY); - graphics2D.fillRect(location.x, location.y, size.width, size.height); - } - if (type == MinesweeperTileType.BOMB) { - graphics2D.setColor(Color.LIGHT_GRAY); - graphics2D.fillRect(location.x, location.y, size.width, size.height); - graphics2D.drawImage( - MinesweeperView.BOMB_IMAGE, - location.x, - location.y, - size.width, - size.height, - Color.GRAY, - null); - graphics2D.setColor(Color.BLACK); - graphics2D.drawRect(location.x, location.y, size.width, size.height); - } - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.ui.boardview.GridElementView; +import java.awt.*; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +public class MinesweeperElementView extends GridElementView { + + private static final Font FONT = new Font("TimesRoman", Font.BOLD, 16); + private static final Color FONT_COLOR = Color.BLACK; + + public MinesweeperElementView(@NotNull MinesweeperCell cell) { + super(cell); + } + + @Override + public @NotNull MinesweeperCell getPuzzleElement() { + return (MinesweeperCell) super.getPuzzleElement(); + } + + @Override + @SuppressWarnings("Duplicates") + @Contract(pure = true) + public void drawElement(@NotNull Graphics2D graphics2D) { + final MinesweeperCell cell = (MinesweeperCell) puzzleElement; + final MinesweeperTileType type = cell.getTileType(); + if (type == MinesweeperTileType.FLAG) { + graphics2D.setStroke(new BasicStroke(1)); + graphics2D.setColor(Color.WHITE); + graphics2D.fillRect(location.x, location.y, size.width, size.height); + + graphics2D.setColor(Color.BLACK); + graphics2D.drawRect(location.x, location.y, size.width, size.height); + + graphics2D.setColor(FONT_COLOR); + graphics2D.setFont(FONT); + final FontMetrics metrics = graphics2D.getFontMetrics(FONT); + final String value = String.valueOf(((MinesweeperCell) puzzleElement).getData().data()); + final int xText = location.x + (size.width - metrics.stringWidth(value)) / 2; + final int yText = + location.y + ((size.height - metrics.getHeight()) / 2) + metrics.getAscent(); + graphics2D.drawString(value, xText, yText); + return; + } + if (type == MinesweeperTileType.UNSET) { + graphics2D.setStroke(new BasicStroke(1)); + graphics2D.setColor(Color.BLACK); + graphics2D.drawRect(location.x, location.y, size.width, size.height); + graphics2D.setColor(Color.DARK_GRAY); + graphics2D.fillRect(location.x, location.y, size.width, size.height); + return; + } + if (type == MinesweeperTileType.EMPTY) { + graphics2D.setStroke(new BasicStroke(1)); + graphics2D.setColor(Color.BLACK); + graphics2D.drawRect(location.x, location.y, size.width, size.height); + graphics2D.setColor(Color.GRAY); + graphics2D.fillRect(location.x, location.y, size.width, size.height); + } + if (type == MinesweeperTileType.BOMB) { + graphics2D.setColor(Color.LIGHT_GRAY); + graphics2D.fillRect(location.x, location.y, size.width, size.height); + graphics2D.drawImage( + MinesweeperView.BOMB_IMAGE, + location.x, + location.y, + size.width, + size.height, + Color.GRAY, + null); + graphics2D.setColor(Color.BLACK); + graphics2D.drawRect(location.x, location.y, size.width, size.height); + } + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperExporter.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperExporter.java index 0a4f69fd6..8ae9c5a9a 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperExporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperExporter.java @@ -1,44 +1,44 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.model.Puzzle; -import edu.rpi.legup.model.PuzzleExporter; -import edu.rpi.legup.model.gameboard.PuzzleElement; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -public class MinesweeperExporter extends PuzzleExporter { - - public MinesweeperExporter(@NotNull Puzzle puzzle) { - super(puzzle); - } - - @Override - @Contract(pure = true) - protected @NotNull Element createBoardElement(@NotNull Document newDocument) { - MinesweeperBoard board; - if (puzzle.getTree() != null) { - board = (MinesweeperBoard) puzzle.getTree().getRootNode().getBoard(); - } - else { - board = (MinesweeperBoard) puzzle.getBoardView().getBoard(); - } - - final org.w3c.dom.Element boardElement = newDocument.createElement("board"); - boardElement.setAttribute("width", String.valueOf(board.getWidth())); - boardElement.setAttribute("height", String.valueOf(board.getHeight())); - - final org.w3c.dom.Element cellsElement = newDocument.createElement("cells"); - for (PuzzleElement puzzleElement : board.getPuzzleElements()) { - final MinesweeperCell cell = (MinesweeperCell) puzzleElement; - if (!MinesweeperTileData.unset().equals(cell.getData())) { - final org.w3c.dom.Element cellElement = puzzle.getFactory().exportCell(newDocument, puzzleElement); - cellsElement.appendChild(cellElement); - } - } - - boardElement.appendChild(cellsElement); - return boardElement; - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.model.Puzzle; +import edu.rpi.legup.model.PuzzleExporter; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class MinesweeperExporter extends PuzzleExporter { + + public MinesweeperExporter(@NotNull Puzzle puzzle) { + super(puzzle); + } + + @Override + @Contract(pure = true) + protected @NotNull Element createBoardElement(@NotNull Document newDocument) { + MinesweeperBoard board; + if (puzzle.getTree() != null) { + board = (MinesweeperBoard) puzzle.getTree().getRootNode().getBoard(); + } else { + board = (MinesweeperBoard) puzzle.getBoardView().getBoard(); + } + + final org.w3c.dom.Element boardElement = newDocument.createElement("board"); + boardElement.setAttribute("width", String.valueOf(board.getWidth())); + boardElement.setAttribute("height", String.valueOf(board.getHeight())); + + final org.w3c.dom.Element cellsElement = newDocument.createElement("cells"); + for (PuzzleElement puzzleElement : board.getPuzzleElements()) { + final MinesweeperCell cell = (MinesweeperCell) puzzleElement; + if (!MinesweeperTileData.unset().equals(cell.getData())) { + final org.w3c.dom.Element cellElement = + puzzle.getFactory().exportCell(newDocument, puzzleElement); + cellsElement.appendChild(cellElement); + } + } + + boardElement.appendChild(cellsElement); + return boardElement; + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperImporter.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperImporter.java index 8dc7037f9..419a69247 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperImporter.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperImporter.java @@ -1,119 +1,128 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.model.PuzzleImporter; -import edu.rpi.legup.save.InvalidFileFormatException; -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import java.awt.*; - -public class MinesweeperImporter extends PuzzleImporter { - - public MinesweeperImporter(@NotNull Minesweeper minesweeper) { - super(minesweeper); - } - - @Override - @Contract(pure = true, value = "-> true") - public boolean acceptsRowsAndColumnsInput() { - return true; - } - - @Override - @Contract(pure = true, value = "-> false") - public boolean acceptsTextInput() { - return false; - } - - @Override - @Contract(pure = false) - public void initializeBoard(int rows, int columns) { - MinesweeperBoard minesweeperBoard = new MinesweeperBoard(columns, rows); - - for (int y = 0; y < rows; y++) { - for (int x = 0; x < columns; x++) { - MinesweeperCell cell = new MinesweeperCell(MinesweeperTileData.unset(), new Point(x, y)); - cell.setIndex(y * columns + x); - cell.setModifiable(true); - minesweeperBoard.setCell(x, y, cell); - } - } - puzzle.setCurrentBoard(minesweeperBoard); - } - - @Override - @Contract(pure = false) - public void initializeBoard(@NotNull Node node) throws InvalidFileFormatException { - try { - if (!node.getNodeName().equalsIgnoreCase("board")) { - throw new InvalidFileFormatException("Minesweeper Importer: cannot find board puzzleElement"); - } - final Element boardElement = (Element) node; - if (boardElement.getElementsByTagName("cells").getLength() == 0) { - throw new InvalidFileFormatException("Minesweeper Importer: no puzzleElement found for board"); - } - final Element dataElement = (Element) boardElement.getElementsByTagName("cells").item(0); - final NodeList elementDataList = dataElement.getElementsByTagName("cell"); - - final MinesweeperBoard minesweeperBoard = getMinesweeperBoard(boardElement); - - final int width = minesweeperBoard.getWidth(); - final int height = minesweeperBoard.getHeight(); - - for (int i = 0; i < elementDataList.getLength(); i++) { - final MinesweeperCell cell = (MinesweeperCell) puzzle.getFactory().importCell(elementDataList.item(i), minesweeperBoard); - final Point loc = cell.getLocation(); - if (MinesweeperTileData.unset().equals(cell.getData())) { - cell.setModifiable(false); - cell.setGiven(true); - } - minesweeperBoard.setCell(loc.x, loc.y, cell); - } - - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - if (minesweeperBoard.getCell(x, y) == null) { - final MinesweeperCell cell = new MinesweeperCell(MinesweeperTileData.unset(), new Point(x, y)); - cell.setIndex(y * height + x); - cell.setModifiable(true); - minesweeperBoard.setCell(x, y, cell); - } - } - } - puzzle.setCurrentBoard(minesweeperBoard); - } - catch (NumberFormatException e) { - throw new InvalidFileFormatException("Minesweeper Importer: unknown value where integer expected"); - } - } - - @Contract(pure = true) - private static @NotNull MinesweeperBoard getMinesweeperBoard(@NotNull Element boardElement) throws InvalidFileFormatException { - MinesweeperBoard minesweeperBoard = null; - if (!boardElement.getAttribute("size").isEmpty()) { - final int size = Integer.parseInt(boardElement.getAttribute("size")); - minesweeperBoard = new MinesweeperBoard(size); - } - else { - if (!boardElement.getAttribute("width").isEmpty() && !boardElement.getAttribute("height").isEmpty()) { - final int width = Integer.parseInt(boardElement.getAttribute("width")); - final int height = Integer.parseInt(boardElement.getAttribute("height")); - minesweeperBoard = new MinesweeperBoard(width, height); - } - } - - if (minesweeperBoard == null) { - throw new InvalidFileFormatException("Minesweeper Importer: invalid board dimensions"); - } - return minesweeperBoard; - } - - @Override - @Contract(value = "_ -> fail", pure = false) - public void initializeBoard(@NotNull String[] statements) throws UnsupportedOperationException, IllegalArgumentException { - throw new UnsupportedOperationException("Minesweeper does not support text input."); - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.model.PuzzleImporter; +import edu.rpi.legup.save.InvalidFileFormatException; +import java.awt.*; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class MinesweeperImporter extends PuzzleImporter { + + public MinesweeperImporter(@NotNull Minesweeper minesweeper) { + super(minesweeper); + } + + @Override + @Contract(pure = true, value = "-> true") + public boolean acceptsRowsAndColumnsInput() { + return true; + } + + @Override + @Contract(pure = true, value = "-> false") + public boolean acceptsTextInput() { + return false; + } + + @Override + @Contract(pure = false) + public void initializeBoard(int rows, int columns) { + MinesweeperBoard minesweeperBoard = new MinesweeperBoard(columns, rows); + + for (int y = 0; y < rows; y++) { + for (int x = 0; x < columns; x++) { + MinesweeperCell cell = + new MinesweeperCell(MinesweeperTileData.unset(), new Point(x, y)); + cell.setIndex(y * columns + x); + cell.setModifiable(true); + minesweeperBoard.setCell(x, y, cell); + } + } + puzzle.setCurrentBoard(minesweeperBoard); + } + + @Override + @Contract(pure = false) + public void initializeBoard(@NotNull Node node) throws InvalidFileFormatException { + try { + if (!node.getNodeName().equalsIgnoreCase("board")) { + throw new InvalidFileFormatException( + "Minesweeper Importer: cannot find board puzzleElement"); + } + final Element boardElement = (Element) node; + if (boardElement.getElementsByTagName("cells").getLength() == 0) { + throw new InvalidFileFormatException( + "Minesweeper Importer: no puzzleElement found for board"); + } + final Element dataElement = + (Element) boardElement.getElementsByTagName("cells").item(0); + final NodeList elementDataList = dataElement.getElementsByTagName("cell"); + + final MinesweeperBoard minesweeperBoard = getMinesweeperBoard(boardElement); + + final int width = minesweeperBoard.getWidth(); + final int height = minesweeperBoard.getHeight(); + + for (int i = 0; i < elementDataList.getLength(); i++) { + final MinesweeperCell cell = + (MinesweeperCell) + puzzle.getFactory() + .importCell(elementDataList.item(i), minesweeperBoard); + final Point loc = cell.getLocation(); + if (MinesweeperTileData.unset().equals(cell.getData())) { + cell.setModifiable(false); + cell.setGiven(true); + } + minesweeperBoard.setCell(loc.x, loc.y, cell); + } + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (minesweeperBoard.getCell(x, y) == null) { + final MinesweeperCell cell = + new MinesweeperCell(MinesweeperTileData.unset(), new Point(x, y)); + cell.setIndex(y * height + x); + cell.setModifiable(true); + minesweeperBoard.setCell(x, y, cell); + } + } + } + puzzle.setCurrentBoard(minesweeperBoard); + } catch (NumberFormatException e) { + throw new InvalidFileFormatException( + "Minesweeper Importer: unknown value where integer expected"); + } + } + + @Contract(pure = true) + private static @NotNull MinesweeperBoard getMinesweeperBoard(@NotNull Element boardElement) + throws InvalidFileFormatException { + MinesweeperBoard minesweeperBoard = null; + if (!boardElement.getAttribute("size").isEmpty()) { + final int size = Integer.parseInt(boardElement.getAttribute("size")); + minesweeperBoard = new MinesweeperBoard(size); + } else { + if (!boardElement.getAttribute("width").isEmpty() + && !boardElement.getAttribute("height").isEmpty()) { + final int width = Integer.parseInt(boardElement.getAttribute("width")); + final int height = Integer.parseInt(boardElement.getAttribute("height")); + minesweeperBoard = new MinesweeperBoard(width, height); + } + } + + if (minesweeperBoard == null) { + throw new InvalidFileFormatException("Minesweeper Importer: invalid board dimensions"); + } + return minesweeperBoard; + } + + @Override + @Contract(value = "_ -> fail", pure = false) + public void initializeBoard(@NotNull String[] statements) + throws UnsupportedOperationException, IllegalArgumentException { + throw new UnsupportedOperationException("Minesweeper does not support text input."); + } +} 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 79666c243..5296cf057 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileData.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileData.java @@ -1,106 +1,107 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import org.jetbrains.annotations.Contract; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Objects; - -public record MinesweeperTileData(MinesweeperTileType type, int data) { - - - public static final int UNSET_DATA = -2; - public static final int BOMB_DATA = -1; - public static final int EMPTY_DATA = 0; - - /** - * Always has a type of {@link MinesweeperTileType#UNSET}, and a data value of - * {@value UNSET_DATA} - */ - private static final MinesweeperTileData UNSET = new MinesweeperTileData(MinesweeperTileType.UNSET, UNSET_DATA); - /** - * Always has a type of {@link MinesweeperTileType#BOMB}, and a data value of - * {@value BOMB_DATA} - */ - private static final MinesweeperTileData BOMB = new MinesweeperTileData(MinesweeperTileType.BOMB, BOMB_DATA); - /** - * Always has a type of {@link MinesweeperTileType#EMPTY}, and a data value of - * {@value EMPTY_DATA} - */ - private static final MinesweeperTileData EMPTY = new MinesweeperTileData(MinesweeperTileType.EMPTY, EMPTY_DATA); - - /** - * @param count how many bombs are near the flag - * @return a new {@link MinesweeperTileData} with a {@link MinesweeperTileData#type} of {@link MinesweeperTileType#FLAG} - * and a {@link MinesweeperTileData#data} of {@code count} - */ - @Contract(pure = true) - public static @NotNull MinesweeperTileData flag(int count) { - return new MinesweeperTileData(MinesweeperTileType.FLAG, count); - } - - /** - * - * @param data Determines what type of {@link MinesweeperTileData} to return. - * @return If {@code data} is one of {@link MinesweeperTileData#UNSET_DATA}, - * {@link MinesweeperTileData#BOMB_DATA}, or {@link MinesweeperTileData#EMPTY_DATA}, it will return - * that data. If {@code data} is less than any of the values, or greater than 8, it will return - * {@link MinesweeperTileData#UNSET_DATA}. Otherwise, it returns {@link MinesweeperTileData#flag(int)} - * and passes {@code data} as the parameter. - */ - @Contract(pure = true) - public static @NotNull MinesweeperTileData fromData(int data) { - return switch (data) { - case UNSET_DATA -> unset(); - case BOMB_DATA -> bomb(); - case EMPTY_DATA -> empty(); - default -> { - if (data <= -2 || data > 8) { - yield unset(); - } - yield flag(data); - } - }; - } - - public static @NotNull MinesweeperTileData unset() { - return UNSET; - } - - public static @NotNull MinesweeperTileData bomb() { - return BOMB; - } - - public static @NotNull MinesweeperTileData empty() { - 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; - if (o == null || getClass() != o.getClass()) return false; - MinesweeperTileData that = (MinesweeperTileData) o; - return data == that.data && type == that.type; - } - - @Override - public int hashCode() { - return Objects.hash(type, data); - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import java.util.Objects; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public record MinesweeperTileData(MinesweeperTileType type, int data) { + + public static final int UNSET_DATA = -2; + public static final int BOMB_DATA = -1; + public static final int EMPTY_DATA = 0; + + /** + * Always has a type of {@link MinesweeperTileType#UNSET}, and a data value of {@value + * UNSET_DATA} + */ + private static final MinesweeperTileData UNSET = + new MinesweeperTileData(MinesweeperTileType.UNSET, UNSET_DATA); + + /** + * Always has a type of {@link MinesweeperTileType#BOMB}, and a data value of {@value BOMB_DATA} + */ + private static final MinesweeperTileData BOMB = + new MinesweeperTileData(MinesweeperTileType.BOMB, BOMB_DATA); + + /** + * Always has a type of {@link MinesweeperTileType#EMPTY}, and a data value of {@value + * EMPTY_DATA} + */ + private static final MinesweeperTileData EMPTY = + new MinesweeperTileData(MinesweeperTileType.EMPTY, EMPTY_DATA); + + /** + * @param count how many bombs are near the flag + * @return a new {@link MinesweeperTileData} with a {@link MinesweeperTileData#type} of {@link + * MinesweeperTileType#FLAG} and a {@link MinesweeperTileData#data} of {@code count} + */ + @Contract(pure = true) + public static @NotNull MinesweeperTileData flag(int count) { + return new MinesweeperTileData(MinesweeperTileType.FLAG, count); + } + + /** + * @param data Determines what type of {@link MinesweeperTileData} to return. + * @return If {@code data} is one of {@link MinesweeperTileData#UNSET_DATA}, {@link + * MinesweeperTileData#BOMB_DATA}, or {@link MinesweeperTileData#EMPTY_DATA}, it will return + * that data. If {@code data} is less than any of the values, or greater than 8, it will + * return {@link MinesweeperTileData#UNSET_DATA}. Otherwise, it returns {@link + * MinesweeperTileData#flag(int)} and passes {@code data} as the parameter. + */ + @Contract(pure = true) + public static @NotNull MinesweeperTileData fromData(int data) { + return switch (data) { + case UNSET_DATA -> unset(); + case BOMB_DATA -> bomb(); + case EMPTY_DATA -> empty(); + default -> { + if (data <= -2 || data > 8) { + yield unset(); + } + yield flag(data); + } + }; + } + + public static @NotNull MinesweeperTileData unset() { + return UNSET; + } + + public static @NotNull MinesweeperTileData bomb() { + return BOMB; + } + + public static @NotNull MinesweeperTileData empty() { + 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; + if (o == null || getClass() != o.getClass()) return false; + MinesweeperTileData that = (MinesweeperTileData) o; + return data == that.data && type == that.type; + } + + @Override + public int hashCode() { + return Objects.hash(type, data); + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileType.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileType.java index 9d5dabd69..a682da5e5 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileType.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperTileType.java @@ -1,24 +1,14 @@ -package edu.rpi.legup.puzzle.minesweeper; - -public enum MinesweeperTileType { - - - /** - * A cell with nothing - */ - UNSET, - - /** - * Represents a cell with no bombs in it - */ - EMPTY, - /** - * A flag has values 1-8 representing how many bombs are touching it - */ - FLAG, - /** - * A bomb tile that should be marked by nearby flags - */ - BOMB - -} +package edu.rpi.legup.puzzle.minesweeper; + +public enum MinesweeperTileType { + + /** A cell with nothing */ + UNSET, + + /** Represents a cell with no bombs in it */ + EMPTY, + /** A flag has values 1-8 representing how many bombs are touching it */ + FLAG, + /** A bomb tile that should be marked by nearby flags */ + BOMB +} 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 e74979e19..375692095 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperUtilities.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperUtilities.java @@ -1,17 +1,18 @@ package edu.rpi.legup.puzzle.minesweeper; import java.awt.*; +import java.util.*; import java.util.Objects; import java.util.stream.IntStream; import java.util.stream.Stream; -import java.util.*; 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) { + public static Stream getSurroundingCells( + MinesweeperBoard board, MinesweeperCell cell) { final Point loc = cell.getLocation(); final int height = board.getHeight(); final int width = board.getWidth(); @@ -25,27 +26,31 @@ public static Stream getSurroundingCells(MinesweeperBoard board 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); - }) + .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 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) { @@ -65,9 +70,8 @@ public static int countSurroundingFlags(MinesweeperBoard board, MinesweeperCell } /** - * - * @return how many bombs are left that need to be placed - * around {@code cell} which must be a 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()) { @@ -86,15 +90,20 @@ public static boolean hasEmptyAdjacent(MinesweeperBoard board, MinesweeperCell c return false; } - public static ArrayList getAdjacentCells(MinesweeperBoard board, MinesweeperCell cell) { + public static ArrayList getAdjacentCells( + MinesweeperBoard board, MinesweeperCell cell) { ArrayList adjCells = new ArrayList(); Point cellLoc = cell.getLocation(); - for (int i=-1; i <= 1; i++) { - for (int j=-1; j <= 1; j++) { - if (cellLoc.getX() + i < 0 || cellLoc.y + j < 0 || cellLoc.x + i >= board.getWidth() || cellLoc.y + j >= board.getHeight()) { + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (cellLoc.getX() + i < 0 + || cellLoc.y + j < 0 + || cellLoc.x + i >= board.getWidth() + || cellLoc.y + j >= board.getHeight()) { continue; } - MinesweeperCell adjCell = (MinesweeperCell) board.getCell(cellLoc.x + i, cellLoc.y + j); + MinesweeperCell adjCell = + (MinesweeperCell) board.getCell(cellLoc.x + i, cellLoc.y + j); if (adjCell == null) { continue; } @@ -114,7 +123,13 @@ public static ArrayList getCombinations(int chosenNumItems, int total return combinations; } - private static void recurseCombinations(ArrayList result, int curIndex, int maxBlack, int numBlack, int len, boolean[] workingArray) { + private static void recurseCombinations( + ArrayList result, + int curIndex, + int maxBlack, + int numBlack, + int len, + boolean[] workingArray) { if (curIndex == len) { // complete, but not valid solution if (numBlack != maxBlack) { @@ -131,11 +146,9 @@ private static void recurseCombinations(ArrayList result, int curInde if (numBlack < maxBlack) { workingArray[curIndex] = true; - recurseCombinations(result, curIndex+1, maxBlack, numBlack+1, len, workingArray); + recurseCombinations(result, curIndex + 1, maxBlack, numBlack + 1, len, workingArray); } workingArray[curIndex] = false; - recurseCombinations(result, curIndex+1, maxBlack, numBlack, len, workingArray); + recurseCombinations(result, curIndex + 1, maxBlack, numBlack, len, workingArray); } - - } diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperView.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperView.java index ca9214416..f18fdab82 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperView.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/MinesweeperView.java @@ -1,48 +1,48 @@ -package edu.rpi.legup.puzzle.minesweeper; - -import edu.rpi.legup.controller.BoardController; -import edu.rpi.legup.model.gameboard.PuzzleElement; -import edu.rpi.legup.puzzle.lightup.LightUpView; -import edu.rpi.legup.ui.boardview.GridBoardView; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.jetbrains.annotations.NotNull; - -import javax.imageio.ImageIO; -import java.awt.*; -import java.io.IOException; -import java.util.Objects; - -public class MinesweeperView extends GridBoardView { - - private static final Logger LOGGER = LogManager.getLogger(MinesweeperView.class.getName()); - public static final Image BOMB_IMAGE; - - static { - Image tempBombImage = null; - try { - tempBombImage = - ImageIO.read( - Objects.requireNonNull(ClassLoader.getSystemClassLoader() - .getResource("edu/rpi/legup/images/minesweeper/tiles/Bomb.png"))); - } catch (IOException e) { - LOGGER.error("Failed to open Minesweeper images"); - } - BOMB_IMAGE = tempBombImage; - } - - - public MinesweeperView(@NotNull MinesweeperBoard board) { - super(new BoardController(), new MinesweeperController(), board.getDimension()); - - for (PuzzleElement puzzleElement : board.getPuzzleElements()) { - final MinesweeperCell cell = (MinesweeperCell) puzzleElement; - final Point loc = cell.getLocation(); - final MinesweeperElementView elementView = new MinesweeperElementView(cell); - elementView.setIndex(cell.getIndex()); - elementView.setSize(elementSize); - elementView.setLocation(new Point(loc.x * elementSize.width, loc.y * elementSize.height)); - elementViews.add(elementView); - } - } -} +package edu.rpi.legup.puzzle.minesweeper; + +import edu.rpi.legup.controller.BoardController; +import edu.rpi.legup.model.gameboard.PuzzleElement; +import edu.rpi.legup.ui.boardview.GridBoardView; +import java.awt.*; +import java.io.IOException; +import java.util.Objects; +import javax.imageio.ImageIO; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +public class MinesweeperView extends GridBoardView { + + private static final Logger LOGGER = LogManager.getLogger(MinesweeperView.class.getName()); + public static final Image BOMB_IMAGE; + + static { + Image tempBombImage = null; + try { + tempBombImage = + ImageIO.read( + Objects.requireNonNull( + ClassLoader.getSystemClassLoader() + .getResource( + "edu/rpi/legup/images/minesweeper/tiles/Bomb.png"))); + } catch (IOException e) { + LOGGER.error("Failed to open Minesweeper images"); + } + BOMB_IMAGE = tempBombImage; + } + + public MinesweeperView(@NotNull MinesweeperBoard board) { + super(new BoardController(), new MinesweeperController(), board.getDimension()); + + for (PuzzleElement puzzleElement : board.getPuzzleElements()) { + final MinesweeperCell cell = (MinesweeperCell) puzzleElement; + final Point loc = cell.getLocation(); + final MinesweeperElementView elementView = new MinesweeperElementView(cell); + elementView.setIndex(cell.getIndex()); + elementView.setSize(elementSize); + elementView.setLocation( + new Point(loc.x * elementSize.width, loc.y * elementSize.height)); + elementViews.add(elementView); + } + } +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/BombTile.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/BombTile.java index 90bd8fb90..78a5d320c 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/BombTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/BombTile.java @@ -1,8 +1,13 @@ package edu.rpi.legup.puzzle.minesweeper.elements; import edu.rpi.legup.model.elements.NonPlaceableElement; -public class BombTile extends NonPlaceableElement{ + +public class BombTile extends NonPlaceableElement { public BombTile() { - super("MINE-UNPL-0001", "Bomb", "A bomb", "edu/rpi/legup/images/minesweeper/tiles/Bomb.png"); + super( + "MINE-UNPL-0001", + "Bomb", + "A bomb", + "edu/rpi/legup/images/minesweeper/tiles/Bomb.png"); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/EmptyTile.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/EmptyTile.java index dad1593ca..7149bfa6a 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/EmptyTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/EmptyTile.java @@ -5,6 +5,10 @@ public class EmptyTile extends PlaceableElement { public EmptyTile() { - super("MINE-PLAC-0002", "Empty", "An empty tile", "edu/rpi/legup/images/minesweeper/tiles/Empty.png"); + super( + "MINE-PLAC-0002", + "Empty", + "An empty tile", + "edu/rpi/legup/images/minesweeper/tiles/Empty.png"); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/FlagTile.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/FlagTile.java index b6d44d11a..0bbca81f9 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/FlagTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/FlagTile.java @@ -1,8 +1,13 @@ package edu.rpi.legup.puzzle.minesweeper.elements; import edu.rpi.legup.model.elements.PlaceableElement; -public class FlagTile extends PlaceableElement{ + +public class FlagTile extends PlaceableElement { public FlagTile() { - super("MINE-PLAC-0001", "Flag", "The flag", "edu/rpi/legup/images/nurikabe/tiles/BlackTile.png"); + super( + "MINE-PLAC-0001", + "Flag", + "The flag", + "edu/rpi/legup/images/nurikabe/tiles/BlackTile.png"); } } diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/Unset.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/Unset.java index 62f5b824f..71ce1881f 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/Unset.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/Unset.java @@ -4,6 +4,10 @@ public class Unset extends NonPlaceableElement { public Unset() { - super("MINE-UNPL-0002", "Unset", "A blank tile", "edu/rpi/legup/images/fillapix/tiles/UnknownTile.png"); + super( + "MINE-UNPL-0002", + "Unset", + "A blank tile", + "edu/rpi/legup/images/fillapix/tiles/UnknownTile.png"); } -} \ No newline at end of file +} diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/UnsetTile.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/UnsetTile.java index 1899a4fd5..447e2840c 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/UnsetTile.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/elements/UnsetTile.java @@ -5,7 +5,10 @@ public class UnsetTile extends NonPlaceableElement { public UnsetTile() { - super("MINE-UNPL-0002", "Unset", "An unset tile", "edu/rpi/legup/images/minesweeper/tiles/Unset.png"); + super( + "MINE-UNPL-0002", + "Unset", + "An unset tile", + "edu/rpi/legup/images/minesweeper/tiles/Unset.png"); } - } 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 4bba7ff32..a1ef97928 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 @@ -8,15 +8,15 @@ import edu.rpi.legup.puzzle.minesweeper.MinesweeperBoard; import edu.rpi.legup.puzzle.minesweeper.MinesweeperCell; import edu.rpi.legup.puzzle.minesweeper.MinesweeperTileData; -import edu.rpi.legup.puzzle.minesweeper.MinesweeperTileType; - import java.util.ArrayList; import java.util.List; public class BombOrFilledCaseRule extends CaseRule { public BombOrFilledCaseRule() { - super("MINE-CASE-0001", "Bomb Or Filled", + super( + "MINE-CASE-0001", + "Bomb Or Filled", "A cell can either be a bomb or filled.\n", "edu/rpi/legup/images/minesweeper/cases/BombOrFilled.jpg"); } @@ -68,8 +68,10 @@ public String checkRuleRaw(TreeTransition transition) { + ": This case rule must have 1 modified cell for each case."; } - MinesweeperCell mod1 = (MinesweeperCell) case1.getBoard().getModifiedData().iterator().next(); - MinesweeperCell mod2 = (MinesweeperCell) case2.getBoard().getModifiedData().iterator().next(); + MinesweeperCell mod1 = + (MinesweeperCell) case1.getBoard().getModifiedData().iterator().next(); + MinesweeperCell mod2 = + (MinesweeperCell) case2.getBoard().getModifiedData().iterator().next(); if (!mod1.getLocation().equals(mod2.getLocation())) { return super.getInvalidUseOfRuleMessage() + ": This case rule must modify the same cell for each case."; diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/LessBombsThanFlagContradictionRule.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/LessBombsThanFlagContradictionRule.java index 0cc1f6324..76212f6da 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/LessBombsThanFlagContradictionRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/LessBombsThanFlagContradictionRule.java @@ -3,20 +3,16 @@ import edu.rpi.legup.model.gameboard.Board; import edu.rpi.legup.model.gameboard.PuzzleElement; import edu.rpi.legup.model.rules.ContradictionRule; -import edu.rpi.legup.puzzle.nurikabe.NurikabeBoard; -import edu.rpi.legup.puzzle.nurikabe.NurikabeCell; -import edu.rpi.legup.puzzle.nurikabe.NurikabeType; -import edu.rpi.legup.puzzle.nurikabe.NurikabeUtilities; -import edu.rpi.legup.utility.DisjointSets; - -import java.util.Set; public class LessBombsThanFlagContradictionRule extends ContradictionRule { - private final String NO_CONTRADICTION_MESSAGE = "Does not contain a contradiction at this index"; + private final String NO_CONTRADICTION_MESSAGE = + "Does not contain a contradiction at this index"; private final String INVALID_USE_MESSAGE = "Contradiction must be a region"; public LessBombsThanFlagContradictionRule() { - super("MINE-CONT-0000", "Less Bombs Than Flag", + super( + "MINE-CONT-0000", + "Less Bombs Than Flag", "There can not be less then the number of Bombs around a flag then the specified number\n", "edu/rpi/legup/images/nurikabe/contradictions/NoNumber.png"); } @@ -26,7 +22,4 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) { return null; } - - } - diff --git a/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/SatisfyFlagCaseRule.java b/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/SatisfyFlagCaseRule.java index cb2250ddf..cecd5a588 100644 --- a/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/SatisfyFlagCaseRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/SatisfyFlagCaseRule.java @@ -7,14 +7,14 @@ import edu.rpi.legup.model.tree.TreeNode; import edu.rpi.legup.model.tree.TreeTransition; import edu.rpi.legup.puzzle.minesweeper.*; - import java.awt.*; import java.util.*; import java.util.List; -public class SatisfyFlagCaseRule extends CaseRule{ +public class SatisfyFlagCaseRule extends CaseRule { public SatisfyFlagCaseRule() { - super("MINE-CASE-0002", + super( + "MINE-CASE-0002", "Satisfy Flag", "Create a different path for each valid way to mark bombs and filled cells around a flag", "edu/rpi/legup/images/minesweeper/cases/SatisfyFlag.jpg"); @@ -27,7 +27,9 @@ public CaseBoard getCaseBoard(Board board) { minesweeperBoard.setModifiable(false); for (PuzzleElement data : minesweeperBoard.getPuzzleElements()) { MinesweeperCell cell = (MinesweeperCell) data; - if (cell.getTileNumber() >= 0 && cell.getTileNumber() <= 8 && MinesweeperUtilities.hasEmptyAdjacent(minesweeperBoard, cell)) { + if (cell.getTileNumber() >= 0 + && cell.getTileNumber() <= 8 + && MinesweeperUtilities.hasEmptyAdjacent(minesweeperBoard, cell)) { caseBoard.addPickableElement(data); } } @@ -50,7 +52,8 @@ public ArrayList getCases(Board board, PuzzleElement puzzleElement) { int cellNumBomb = 0; int cellNumUnset = 0; ArrayList unsetCells = new ArrayList(); - ArrayList adjCells = MinesweeperUtilities.getAdjacentCells(minesweeperBoard, cell); + ArrayList adjCells = + MinesweeperUtilities.getAdjacentCells(minesweeperBoard, cell); for (MinesweeperCell adjCell : adjCells) { if (adjCell.getTileType() == MinesweeperTileType.BOMB) { cellNumBomb++; @@ -67,16 +70,16 @@ public ArrayList getCases(Board board, PuzzleElement puzzleElement) { // generate all cases as boolean expressions ArrayList combinations; - combinations = MinesweeperUtilities.getCombinations(cellMaxBlack - cellNumBomb, cellNumUnset); + combinations = + MinesweeperUtilities.getCombinations(cellMaxBlack - cellNumBomb, cellNumUnset); - for (int i=0; i < combinations.size(); i++) { + for (int i = 0; i < combinations.size(); i++) { Board case_ = board.copy(); - for (int j=0; j < combinations.get(i).length; j++) { + for (int j = 0; j < combinations.get(i).length; j++) { cell = (MinesweeperCell) case_.getPuzzleElement(unsetCells.get(j)); if (combinations.get(i)[j]) { cell.setCellType(MinesweeperTileData.bomb()); - } - else { + } else { cell.setCellType(MinesweeperTileData.empty()); } case_.addModifiedData(cell); @@ -105,14 +108,12 @@ public String checkRuleRaw(TreeTransition transition) { * If all the above is verified, then the transition is valid */ - /* ensure there are modified cells */ Set modCells = transition.getBoard().getModifiedData(); if (modCells.size() <= 0) { return super.getInvalidUseOfRuleMessage(); } - /* ensure modified cells occur within a 3X3 square */ int minVertLoc = Integer.MAX_VALUE, maxVertLoc = Integer.MIN_VALUE; int minHorzLoc = Integer.MAX_VALUE, maxHorzLoc = Integer.MIN_VALUE; @@ -135,13 +136,13 @@ public String checkRuleRaw(TreeTransition transition) { return super.getInvalidUseOfRuleMessage(); } - /* get the center of all possible 3X3 squares, * and collect all that have numbers */ MinesweeperBoard board = (MinesweeperBoard) transition.getParents().get(0).getBoard(); ArrayList possibleCenters = new ArrayList(); for (PuzzleElement modCell : modCells) { - ArrayList adjacentCells = MinesweeperUtilities.getAdjacentCells(board, (MinesweeperCell) modCell); + ArrayList adjacentCells = + MinesweeperUtilities.getAdjacentCells(board, (MinesweeperCell) modCell); for (MinesweeperCell cell : adjacentCells) { possibleCenters.add(cell); } @@ -153,14 +154,14 @@ public String checkRuleRaw(TreeTransition transition) { return super.getInvalidUseOfRuleMessage(); } - /* Now go through the remaining centers, and check if their combinations * match the transitions */ for (MinesweeperCell possibleCenter : possibleCenters) { int numBlack = 0; int numEmpty = 0; int maxBlack = possibleCenter.getTileNumber(); - for (MinesweeperCell adjCell : MinesweeperUtilities.getAdjacentCells(board, possibleCenter)) { + for (MinesweeperCell adjCell : + MinesweeperUtilities.getAdjacentCells(board, possibleCenter)) { if (adjCell.getTileType() == MinesweeperTileType.BOMB) { numBlack++; } @@ -173,7 +174,8 @@ public String checkRuleRaw(TreeTransition transition) { continue; } - ArrayList combinations = MinesweeperUtilities.getCombinations(maxBlack - numBlack, numEmpty); + ArrayList combinations = + MinesweeperUtilities.getCombinations(maxBlack - numBlack, numEmpty); if (combinations.size() != childTransitions.size()) { // not this center because combinations do not match transitions continue; @@ -189,11 +191,10 @@ public String checkRuleRaw(TreeTransition transition) { } boolean[] translatedModCells = new boolean[transModCells.size()]; - for (int i=0; i < transModCells.size(); i++) { + for (int i = 0; i < transModCells.size(); i++) { if (transModCells.get(i).getTileType() == MinesweeperTileType.BOMB) { translatedModCells[i] = true; - } - else { + } else { translatedModCells[i] = false; } } @@ -223,7 +224,6 @@ public String checkRuleRaw(TreeTransition transition) { return super.getInvalidUseOfRuleMessage(); } - @Override public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) { return null; diff --git a/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java b/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java index 4b1b450c8..0e0112040 100644 --- a/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java +++ b/src/test/java/puzzles/minesweeper/MinesweeperUtilitiesTest.java @@ -5,14 +5,13 @@ import edu.rpi.legup.puzzle.minesweeper.MinesweeperCell; import edu.rpi.legup.puzzle.minesweeper.MinesweeperUtilities; import edu.rpi.legup.save.InvalidFileFormatException; +import java.util.stream.Stream; 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; @@ -24,11 +23,10 @@ public static void setUp() { } @Test - public void getSurroundingCellsSizeThreeByThreeAtOneXOneTest() throws InvalidFileFormatException { + public void getSurroundingCellsSizeThreeByThreeAtOneXOneTest() + throws InvalidFileFormatException { - TestUtilities.importTestBoard( - "puzzles/minesweeper/utilities/3x3test", - minesweeper); + TestUtilities.importTestBoard("puzzles/minesweeper/utilities/3x3test", minesweeper); final MinesweeperBoard board = (MinesweeperBoard) minesweeper.getCurrentBoard(); MinesweeperCell cell = board.getCell(1, 1); @@ -40,11 +38,10 @@ public void getSurroundingCellsSizeThreeByThreeAtOneXOneTest() throws InvalidFil } @Test - public void getSurroundingCellsSizeThreeByThreeAtZeroXZeroTest() throws InvalidFileFormatException { + public void getSurroundingCellsSizeThreeByThreeAtZeroXZeroTest() + throws InvalidFileFormatException { - TestUtilities.importTestBoard( - "puzzles/minesweeper/utilities/3x3test", - minesweeper); + TestUtilities.importTestBoard("puzzles/minesweeper/utilities/3x3test", minesweeper); final MinesweeperBoard board = (MinesweeperBoard) minesweeper.getCurrentBoard(); MinesweeperCell cell = board.getCell(0, 0); @@ -56,11 +53,10 @@ public void getSurroundingCellsSizeThreeByThreeAtZeroXZeroTest() throws InvalidF } @Test - public void getSurroundingCellsSizeThreeByThreeAtZeroXOneTest() throws InvalidFileFormatException { + public void getSurroundingCellsSizeThreeByThreeAtZeroXOneTest() + throws InvalidFileFormatException { - TestUtilities.importTestBoard( - "puzzles/minesweeper/utilities/3x3test", - minesweeper); + TestUtilities.importTestBoard("puzzles/minesweeper/utilities/3x3test", minesweeper); final MinesweeperBoard board = (MinesweeperBoard) minesweeper.getCurrentBoard(); MinesweeperCell cell = board.getCell(0, 1); @@ -70,6 +66,4 @@ public void getSurroundingCellsSizeThreeByThreeAtZeroXOneTest() throws InvalidFi final long count = cells.count(); Assert.assertEquals(count, 5); } - - }