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

Add bomb or filled case rule and update the config to add minesweeper #753

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -16,4 +16,21 @@ public MinesweeperBoard(int size) {
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
@@ -1,5 +1,6 @@
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;
Expand Down Expand Up @@ -58,5 +59,19 @@ public void drawElement(@NotNull Graphics2D graphics2D) {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@

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());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package edu.rpi.legup.puzzle.minesweeper.elements;

import edu.rpi.legup.model.elements.NonPlaceableElement;
public class Bomb extends NonPlaceableElement{
public Bomb() {
super("MINE-UNPL-0001", "Bomb", "A bomb", "edu/rpi/legup/images/nurikabe/tiles/NumberTile.png");
public class BombTile extends NonPlaceableElement{
public BombTile() {
super("MINE-UNPL-0001", "Bomb", "A bomb", "edu/rpi/legup/images/minesweeper/tiles/Bomb.png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.rpi.legup.puzzle.minesweeper.elements;

import edu.rpi.legup.model.elements.PlaceableElement;

public class EmptyTile extends PlaceableElement {

public EmptyTile() {
super("MINE-PLAC-0002", "Empty", "An empty tile", "edu/rpi/legup/images/minesweeper/tiles/Empty.png");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package edu.rpi.legup.puzzle.minesweeper.elements;

import edu.rpi.legup.model.elements.PlaceableElement;
public class Flag extends PlaceableElement{
public Flag() {
super("NURI-PLAC-0001", "Flag", "The flag", "edu/rpi/legup/images/nurikabe/tiles/BlackTile.png");
public class FlagTile extends PlaceableElement{
public FlagTile() {
super("MINE-PLAC-0001", "Flag", "The flag", "edu/rpi/legup/images/nurikabe/tiles/BlackTile.png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package edu.rpi.legup.puzzle.minesweeper.elements;

import edu.rpi.legup.model.elements.NonPlaceableElement;

public class UnsetTile extends NonPlaceableElement {

public UnsetTile() {
super("MINE-UNPL-0002", "Unset", "An unset tile", "edu/rpi/legup/images/minesweeper/tiles/Unset.png");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MINE-UNPL-0001 : BombTile
MINE-PLAC-0001 : FlagTile
MINE-PLAC-0002 : EmptyTile
MINE-UNPL-0002 : UnsetTile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.CaseRule;
import edu.rpi.legup.model.tree.TreeTransition;
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 {
Expand All @@ -18,16 +23,64 @@ public BombOrFilledCaseRule() {

@Override
public CaseBoard getCaseBoard(Board board) {
return null;
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.getData().isUnset()) {
caseBoard.addPickableElement(data);
}
}
return caseBoard;
}

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

Board case1 = board.copy();
MinesweeperCell cell1 = (MinesweeperCell) case1.getPuzzleElement(puzzleElement);
cell1.setData(MinesweeperTileData.bomb());
case1.addModifiedData(cell1);
cases.add(case1);

Board case2 = board.copy();
MinesweeperCell cell2 = (MinesweeperCell) case2.getPuzzleElement(puzzleElement);
cell2.setData(MinesweeperTileData.empty());
case2.addModifiedData(cell2);
cases.add(case2);
return cases;
}

@Override
public String checkRuleRaw(TreeTransition transition) {
List<TreeTransition> childTransitions = transition.getParents().get(0).getChildren();
if (childTransitions.size() != 2) {
return super.getInvalidUseOfRuleMessage() + ": This case rule must have 2 children.";
}

TreeTransition case1 = childTransitions.get(0);
TreeTransition case2 = childTransitions.get(1);
if (case1.getBoard().getModifiedData().size() != 1
|| case2.getBoard().getModifiedData().size() != 1) {
return super.getInvalidUseOfRuleMessage()
+ ": 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();
if (!mod1.getLocation().equals(mod2.getLocation())) {
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must modify the same cell for each case.";
}

if (!((mod1.getData().isBomb() && mod2.getData().isEmpty())
|| (mod2.getData().isBomb() && mod1.getData().isEmpty()))) {
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must an empty cell and a bomb cell.";
}

return null;
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/resources/edu/rpi/legup/legup/config
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@
qualifiedClassName="edu.rpi.legup.puzzle.skyscrapers.Skyscrapers"
fileType=".xml"
fileCreationDisabled="false"/>
<puzzle name="Minesweeper"
qualifiedClassName="edu.rpi.legup.puzzle.minesweeper.Minesweeper"
fileType=".xml"
fileCreationDisabled="false"/>
</puzzles>
</Legup>
Loading