Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minesweeper #815

Merged
merged 63 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f6f2a62
Initial Minesweeper implementation work
FisherLuba Jan 28, 2024
354cb78
Merge pull request #709 from FisherLuba/dev
FisherLuba Jan 30, 2024
a3a2a80
Add Unset tile type
FisherLuba Jan 30, 2024
8ad5acb
Merge pull request #712 from FisherLuba/dev
FisherLuba Jan 30, 2024
3030805
Init elements
philippark Feb 10, 2024
d592e32
Make clicking tiles cycle between numbers, bomb, and empty tiles.
FisherLuba Feb 13, 2024
75e2ca7
Fix checkstyle errors
FisherLuba Feb 13, 2024
89fbe94
Added
vockek Feb 13, 2024
f8497f7
Revert "Added"
vockek Feb 16, 2024
194eb8b
Added
vockek Feb 16, 2024
ace7122
Create 123456
vockek Feb 16, 2024
5f1b13e
Revert "Create 123456"
vockek Feb 16, 2024
5b4a883
Merge branch 'minesweeper' into dev
Chase-Grajeda Feb 16, 2024
e4d8566
Merge pull request #732 from FisherLuba/dev
Chase-Grajeda Feb 16, 2024
eb5c8a0
Created Bomb or Filled Case Rule
vockek Feb 16, 2024
c5654d3
Merge branch 'Bram-Hub:minesweeper' into minesweeper
vockek Feb 16, 2024
f521daa
Update BombOrFilledCaseRule.java
vockek Feb 16, 2024
d662452
Various additions
vockek Feb 16, 2024
ba56e23
Merge branch 'minesweeper' of https://github.com/vockek/LEGUP into mi…
vockek Feb 16, 2024
99d0ecf
Revert "Create 123456"
vockek Feb 16, 2024
e151aaa
Merge remote-tracking branch 'upstream/dev' into dev
FisherLuba Feb 23, 2024
1d4cb59
Merge remote-tracking branch 'upstream/dev' into dev
FisherLuba Feb 23, 2024
38a4c24
Update Minesweeper classes to use newer Java features
FisherLuba Feb 23, 2024
fcea59a
Add documentation to Minesweeper classes
FisherLuba Feb 23, 2024
294814f
Added helper functions to utilities class
vockek Feb 23, 2024
6b1f812
Added function to retrieve a tile's number
vockek Feb 23, 2024
69aaf17
Create SatisfyFlagCaseRule.java
vockek Feb 23, 2024
e59a94e
Fixed "bomb or filled" case rule's picture
vockek Feb 23, 2024
7989d80
Fixed "satisfy flag" case rule's picture
vockek Feb 23, 2024
36920cb
Merge pull request #745 from FisherLuba/dev
FisherLuba Feb 25, 2024
5d57b42
Merge remote-tracking branch 'upstream/minesweeper' into dev
FisherLuba Feb 27, 2024
a919b16
Merge remote-tracking branch 'upstream/minesweeper' into dev
FisherLuba Feb 27, 2024
bca8755
Add some methods to MinesweeperUtilities and created some tests to ve…
FisherLuba Feb 27, 2024
3651b0d
Merge pull request #749 from FisherLuba/dev
FisherLuba Feb 27, 2024
845af67
Merge branch 'minesweeper' of https://github.com/vockek/LEGUP into mi…
vockek Mar 12, 2024
b4c8ed9
temp
vockek Mar 12, 2024
7dcdfc0
Added BombOrFilledCaseRule
FisherLuba Mar 12, 2024
ee4b707
Revert "temp"
vockek Mar 12, 2024
d8e27aa
Update minesweeperUtilities.java
vockek Mar 12, 2024
4e63e71
Merge branch 'minesweeper-upstream' into minesweeper
vockek Mar 12, 2024
0e89c11
Add bomb image
FisherLuba Mar 12, 2024
b121d28
Added reference sheet for Minesweeper tiles for puzzle editor
FisherLuba Mar 12, 2024
27d831e
Merge pull request #753 from FisherLuba/dev
FisherLuba Mar 15, 2024
23ef262
Merge branch 'minesweeper2' of https://github.com/vockek/LEGUP into m…
vockek Mar 19, 2024
e3d78ff
Merge branch 'minesweeper2' of https://github.com/vockek/LEGUP into m…
vockek Mar 19, 2024
3a1cee2
Added Minesweeper Utility Functions
vockek Mar 19, 2024
3022d86
Fixed "satisfy flag" case rule
vockek Mar 22, 2024
f9d52a3
Automated Java code formatting changes
Bram28 Mar 22, 2024
3164d44
Merge pull request #1 from vockek/minesweeper2
vockek Mar 22, 2024
62b3c71
Revert "Automated Java code formatting changes"
vockek Mar 22, 2024
9bffc7f
Added "More Bombs Than Flag" Contradiction Rule
vockek Mar 29, 2024
5a52070
Delete Unset (replaced by UnsetTile)
vockek Mar 29, 2024
5e461ec
Added dot to empty tile image
vockek Mar 29, 2024
a45ed95
Fixed "Satisfy Flag" Case Rule Picture
vockek Mar 31, 2024
6230b7b
Merge pull request #771 from vockek/minesweeper
vockek Mar 31, 2024
ef36851
Fixed "satisfy flag" bug
vockek Apr 2, 2024
bbf799b
Added "Finish With Bombs" Direct Rule
vockek Apr 16, 2024
8c653d8
Update FinishWithBombsDirectRule.java
vockek Apr 16, 2024
4a288ee
Merge pull request #813 from vockek/minesweeper
vockek Apr 16, 2024
8b486aa
Merge branch 'dev' into minesweeper
vockek Apr 16, 2024
30b6e5e
Automated Java code formatting changes
Bram28 Apr 16, 2024
ecfbff6
Merge branch 'dev' into minesweeper
charlestian23 May 5, 2024
9e623a0
Automated Java code formatting changes
Bram28 May 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions puzzles files/minesweeper/5x5 Minesweeper Easy/123456
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="Minesweeper">
<board height="5" width="5">
<cells>

</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
27 changes: 13 additions & 14 deletions src/main/java/edu/rpi/legup/model/PuzzleImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import edu.rpi.legup.model.rules.Rule;
import edu.rpi.legup.model.tree.*;
import edu.rpi.legup.save.InvalidFileFormatException;

import java.lang.reflect.Array;
import java.util.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -132,18 +130,19 @@ public void initializePuzzle(Node node) throws InvalidFileFormatException {
public abstract void initializeBoard(String[] statements)
throws UnsupportedOperationException, IllegalArgumentException;

/**
* Used to check that elements in the proof tree are saved properly.
* <p> Make sure the list elements are lowercase
*
* @return A list of elements that will change when solving the puzzle
* * e.g. {"cell"}, {"cell", "line"}
*/
public List<String> getImporterElements() {
List<String> elements = new ArrayList<>();
elements.add("cell");
return elements;
}
/**
* Used to check that elements in the proof tree are saved properly.
*
* <p>Make sure the list elements are lowercase
*
* @return A list of elements that will change when solving the puzzle * e.g. {"cell"}, {"cell",
* "line"}
*/
public List<String> getImporterElements() {
List<String> elements = new ArrayList<>();
elements.add("cell");
return elements;
}

/**
* Creates the proof for building
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/minesweeper/Minesweeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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<MinesweeperTileData> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +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 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<MinesweeperTileData> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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);
}
}
Loading