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 #771

Merged
merged 28 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5f1b13e
Revert "Create 123456"
vockek 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
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
845af67
Merge branch 'minesweeper' of https://github.com/vockek/LEGUP into mi…
vockek Mar 12, 2024
b4c8ed9
temp
vockek 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
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
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
Expand Up @@ -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)
/**
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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();
}
}
Loading
Loading