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
Changes from 29 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
Original file line number Diff line number Diff line change
@@ -18,6 +18,10 @@ public MinesweeperCell(@NotNull MinesweeperTileData value, @NotNull Point locati
return super.data.type();
}

public @NotNull int getTileNumber() {
return super.data.data();
}

@Override
@Contract(pure = false)
/**
@@ -56,6 +60,10 @@ public void setType(@NotNull Element element, @NotNull MouseEvent event) {
}
}

public void setCellType(MinesweeperTileData type){
this.data = type;
}

@Override
@Contract(pure = true)
public @NotNull MinesweeperCell copy() {
Original file line number Diff line number Diff line change
@@ -53,11 +53,18 @@ public void drawElement(@NotNull Graphics2D graphics2D) {
return;
}
if (type == MinesweeperTileType.EMPTY) {
graphics2D.setStroke(new BasicStroke(1));
graphics2D.setColor(Color.LIGHT_GRAY);
graphics2D.fillRect(location.x, location.y, size.width, size.height);
graphics2D.drawImage(
MinesweeperView.EMPTY_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);
graphics2D.setColor(Color.GRAY);
graphics2D.fillRect(location.x, location.y, size.width, size.height);
}
if (type == MinesweeperTileType.BOMB) {
graphics2D.setColor(Color.LIGHT_GRAY);
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.*;

public final class MinesweeperUtilities {

@@ -75,4 +76,66 @@ public int countNeededBombsFromFlag(MinesweeperBoard board, MinesweeperCell cell
return cell.getData().data() - countSurroundingBombs(board, cell);
}

public static boolean hasEmptyAdjacent(MinesweeperBoard board, MinesweeperCell cell) {
ArrayList<MinesweeperCell> adjCells = getAdjacentCells(board, cell);
for (MinesweeperCell adjCell : adjCells) {
if (adjCell.getTileType() == MinesweeperTileType.UNSET) {
return true;
}
}
return false;
}

public static ArrayList<MinesweeperCell> getAdjacentCells(MinesweeperBoard board, MinesweeperCell cell) {
ArrayList<MinesweeperCell> adjCells = new ArrayList<MinesweeperCell>();
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()) {
continue;
}
MinesweeperCell adjCell = (MinesweeperCell) board.getCell(cellLoc.x + i, cellLoc.y + j);
if (adjCell == null) {
continue;
}
adjCells.add(adjCell);
}
}
return adjCells;
}

public static ArrayList<boolean[]> getCombinations(int chosenNumItems, int totalNumItems) {
ArrayList<boolean[]> combinations = new ArrayList<boolean[]>();

// calculate all combinations
boolean[] array = new boolean[totalNumItems];
recurseCombinations(combinations, 0, chosenNumItems, 0, totalNumItems, array);

return combinations;
}

private static void recurseCombinations(ArrayList<boolean[]> result, int curIndex, int maxBlack, int numBlack, int len, boolean[] workingArray) {
if (curIndex == len) {
// complete, but not valid solution
if (numBlack != maxBlack) {
return;
}
// complete and valid solution
result.add(workingArray.clone());
return;
}
// there is no chance of completing the required number of solutions, so quit
if (len - curIndex < maxBlack - numBlack) {
return;
}

if (numBlack < maxBlack) {
workingArray[curIndex] = true;
recurseCombinations(result, curIndex+1, maxBlack, numBlack+1, len, workingArray);
}
workingArray[curIndex] = false;
recurseCombinations(result, curIndex+1, maxBlack, numBlack, len, workingArray);
}


}
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ public class MinesweeperView extends GridBoardView {
private static final Logger LOGGER = LogManager.getLogger(MinesweeperView.class.getName());
public static final Image BOMB_IMAGE;

public static final Image EMPTY_IMAGE;

static {
Image tempBombImage = null;
try {
@@ -31,6 +33,19 @@ public class MinesweeperView extends GridBoardView {
BOMB_IMAGE = tempBombImage;
}

static {
Image tempEmptyImage = null;
try {
tempEmptyImage =
ImageIO.read(
Objects.requireNonNull(ClassLoader.getSystemClassLoader()
.getResource("edu/rpi/legup/images/minesweeper/tiles/Empty.png")));
} catch (IOException e) {
LOGGER.error("Failed to open Minesweeper images");
}
EMPTY_IMAGE = tempEmptyImage;
}


public MinesweeperView(@NotNull MinesweeperBoard board) {
super(new BoardController(), new MinesweeperController(), board.getDimension());
Original file line number Diff line number Diff line change
@@ -16,9 +16,9 @@
public class BombOrFilledCaseRule extends CaseRule {

public BombOrFilledCaseRule() {
super("MINE-CASE-0000", "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");
}

@Override
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.rpi.legup.puzzle.minesweeper.rules;

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.minesweeper.MinesweeperBoard;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperCell;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperTileType;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperTileData;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperUtilities;
import java.util.ArrayList;

public class MoreBombsThanFlagContradictionRule extends ContradictionRule {

public MoreBombsThanFlagContradictionRule() {
super(
"MINE-CONT-0001",
"More Bombs Than Flag",
"There can not be more Bombs around a flag than the specified number\n",
"edu/rpi/legup/images/minesweeper/contradictions/Bomb_Surplus.jpg");
}

/**
* Checks whether the transition has a contradiction at the specific puzzleElement index using
* this rule
*
* @param board board to check contradiction
* @param puzzleElement equivalent puzzleElement
* @return null if the transition contains a contradiction at the specified puzzleElement,
* otherwise error message
*/
@Override
public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board;
MinesweeperCell cell = (MinesweeperCell) minesweeperBoard.getPuzzleElement(puzzleElement);

int cellNum = cell.getTileNumber();
if (cellNum < 0 || cellNum >= 10) {
return super.getNoContradictionMessage();
}
int numBlack = 0;
ArrayList<MinesweeperCell> adjCells = MinesweeperUtilities.getAdjacentCells(minesweeperBoard, cell);
for (MinesweeperCell adjCell : adjCells) {
if (adjCell.getTileType() == MinesweeperTileType.BOMB) {
numBlack++;
}
}
if (numBlack > cellNum) {
return null;
}

return super.getNoContradictionMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package edu.rpi.legup.puzzle.minesweeper.rules;

import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.CaseRule;
import edu.rpi.legup.model.tree.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 SatisfyFlagCaseRule() {
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/Satisfy_Flag.png");
}

@Override
public CaseBoard getCaseBoard(Board board) {
MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board.copy();
CaseBoard caseBoard = new CaseBoard(minesweeperBoard, this);
minesweeperBoard.setModifiable(false);
for (PuzzleElement data : minesweeperBoard.getPuzzleElements()) {
MinesweeperCell cell = (MinesweeperCell) data;
if (cell.getTileNumber() >= 0 && cell.getTileNumber() <= 8 && MinesweeperUtilities.hasEmptyAdjacent(minesweeperBoard, cell)) {
caseBoard.addPickableElement(data);
}
}
return caseBoard;
}

@Override
public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement) {
ArrayList<Board> cases = new ArrayList<Board>();

// get value of cell
MinesweeperBoard minesweeperBoard = (MinesweeperBoard) board.copy();
MinesweeperCell cell = (MinesweeperCell) minesweeperBoard.getPuzzleElement(puzzleElement);
int cellMaxBlack = cell.getTileNumber();
if (cellMaxBlack <= 0 || cellMaxBlack > 8) { // cell is not valid cell
return null;
}

// find number of black & unset squares
int cellNumBomb = 0;
int cellNumUnset = 0;
ArrayList<MinesweeperCell> unsetCells = new ArrayList<MinesweeperCell>();
ArrayList<MinesweeperCell> adjCells = MinesweeperUtilities.getAdjacentCells(minesweeperBoard, cell);
for (MinesweeperCell adjCell : adjCells) {
if (adjCell.getTileType() == MinesweeperTileType.BOMB) {
cellNumBomb++;
}
if (adjCell.getTileType() == MinesweeperTileType.UNSET) {
cellNumUnset++;
unsetCells.add(adjCell);
}
}
// no cases if no empty or if too many black already
if (cellNumBomb >= cellMaxBlack || cellNumUnset == 0) {
return cases;
}

// generate all cases as boolean expressions
ArrayList<boolean[]> combinations;
combinations = MinesweeperUtilities.getCombinations(cellMaxBlack - cellNumBomb, cellNumUnset);

for (int i=0; i < combinations.size(); i++) {
Board case_ = board.copy();
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 {
cell.setCellType(MinesweeperTileData.empty());
}
case_.addModifiedData(cell);
}
cases.add(case_);
}

return cases;
}

@Override
public String checkRuleRaw(TreeTransition transition) {
TreeNode parent = transition.getParents().get(0);
List<TreeTransition> childTransitions = parent.getChildren();

/*
* In order for the transition to be valid, it can only be applied to
* one cell, thus:
* * there must be modified cells
* * all modified cells must share at least one common adjacent
* cell
* * all modified cells must fit within a 3X3 square
* * the center of one of the possible squares must be a cell
* with a number
* * that cells possible combinations must match the transitions
* If all the above is verified, then the transition is valid
*/


/* ensure there are modified cells */
Set<PuzzleElement> 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;
for (PuzzleElement modCell : modCells) {
Point loc = ((MinesweeperCell) modCell).getLocation();
if (loc.x < minHorzLoc) {
minHorzLoc = loc.x;
}
if (loc.x > maxHorzLoc) {
maxHorzLoc = loc.x;
}
if (loc.y < minVertLoc) {
minVertLoc = loc.y;
}
if (loc.y > maxVertLoc) {
maxVertLoc = loc.y;
}
}
if (maxVertLoc - minVertLoc > 3 || maxHorzLoc - minHorzLoc > 3) {
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<MinesweeperCell> possibleCenters = new ArrayList<MinesweeperCell>();
for (PuzzleElement modCell : modCells) {
ArrayList<MinesweeperCell> adjacentCells = MinesweeperUtilities.getAdjacentCells(board, (MinesweeperCell) modCell);
for (MinesweeperCell cell : adjacentCells) {
possibleCenters.add(cell);
}
}

// removing all elements without a valid number
possibleCenters.removeIf(x -> x.getTileNumber() <= 0 || x.getTileNumber() >= 9);
if (possibleCenters.isEmpty()) {
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)) {
if (adjCell.getTileType() == MinesweeperTileType.BOMB) {
numBlack++;
}
if (adjCell.getTileType() == MinesweeperTileType.UNSET) {
numEmpty++;
}
}
if (numEmpty <= 0 || numBlack > maxBlack) {
// this cell has no cases (no empty) or is already broken (too many black)
continue;
}

ArrayList<boolean[]> combinations = MinesweeperUtilities.getCombinations(maxBlack - numBlack, numEmpty);
if (combinations.size() != childTransitions.size()) {
// not this center because combinations do not match transitions
continue;
}
boolean quitEarly = false;
for (TreeTransition trans : childTransitions) {
/* convert the transition board into boolean format, so that it
* can be compared to the combinations */
MinesweeperBoard transBoard = (MinesweeperBoard) trans.getBoard();
ArrayList<MinesweeperCell> transModCells = new ArrayList<MinesweeperCell>();
for (PuzzleElement modCell : modCells) {
transModCells.add((MinesweeperCell) transBoard.getPuzzleElement(modCell));
}

boolean[] translatedModCells = new boolean[transModCells.size()];
for (int i=0; i < transModCells.size(); i++) {
if (transModCells.get(i).getTileType() == MinesweeperTileType.BOMB) {
translatedModCells[i] = true;
}
else {
translatedModCells[i] = false;
}
}

// try to find the above state in the combinations, remove if found
boolean removed = false;
for (boolean[] combination : combinations) {
if (Arrays.equals(combination, translatedModCells)) {
combinations.remove(combination);
removed = true;
break;
}
}
// if combination not found, no need to check further, just quit
if (!removed) {
quitEarly = true;
break;
}
}

/* we found a center that is valid */
if (combinations.isEmpty() && !quitEarly) {
return null;
}
}

return super.getInvalidUseOfRuleMessage();
}


@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
return null;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.