Skip to content

Commit

Permalink
new rule + test suite for the rule
Browse files Browse the repository at this point in the history
New rule isolatemine - a mine must see a number cell. Added the rule and test suite for the rule.
  • Loading branch information
willyoung366 committed Dec 7, 2024
1 parent 94d74f0 commit 67e5143
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class MinesweeperController extends ElementController {
final int numberData = current.data();
switch (event.getButton()) { //git?
case MouseEvent.BUTTON1:

if(numberData >= 1 && numberData <= 8) {
return MinesweeperTileData.fromData(numberData);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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.MinesweeperUtilities;

import java.util.ArrayList;

public class IsolateMineContradictionRule extends ContradictionRule {

public IsolateMineContradictionRule() {
super(
"MINE-CONT-0002",
"Isolate Mine",
"A mine cell must see a number cell",
"edu/rpi/legup/images/minesweeper/contradictions/IsolateMine.png");
}

/**
* 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);

if (cell.getTileNumber() != -1) {
return super.getNoContradictionMessage();
}
ArrayList<MinesweeperCell> adjCells =
MinesweeperUtilities.getAdjacentCells(minesweeperBoard, cell);
for (MinesweeperCell adjCell : adjCells) {
if(adjCell.getTileType() == MinesweeperTileType.NUMBER) {
return super.getNoContradictionMessage();
}
}
return null;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package puzzles.minesweeper;

import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.minesweeper.Minesweeper;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperBoard;
import edu.rpi.legup.puzzle.minesweeper.rules.IsolateMineContradictionRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.TestUtilities;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class IsolateMineContradictionRuleTest {
private static final IsolateMineContradictionRule RULE = new IsolateMineContradictionRule();
private static Minesweeper minesweeper;

@BeforeClass
public static void setUp() {
minesweeper = new Minesweeper();
}

@Test
// tests a 3x3 board with a mine in the center surrounded by empty cells
public void IsolateMineTest1() throws InvalidFileFormatException {
TestUtilities.importTestBoard(
"puzzles/minesweeper/rules/IsolateMine1.txt", minesweeper);
TreeNode rootNode = minesweeper.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

MinesweeperBoard board = (MinesweeperBoard) transition.getBoard();

// confirm it is impossible to satisfy up the center square
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(1, 1)));

// every square except the center
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 1)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 1)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 2)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 2)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 2)));
}

@Test
// tests a 3x3 board with a mine in the center surrounded by unset cells
public void IsolateMineTest2() throws InvalidFileFormatException {
TestUtilities.importTestBoard(
"puzzles/minesweeper/rules/IsolateMine2.txt", minesweeper);
TreeNode rootNode = minesweeper.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

MinesweeperBoard board = (MinesweeperBoard) transition.getBoard();

// confirm it is impossible to satisfy up the center square
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(1, 1)));

// every square except the center
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 1)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 1)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 2)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 2)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 2)));
}

@Test
// tests a 3x3 board full of mines only
public void IsolateMineTest3() throws InvalidFileFormatException {
TestUtilities.importTestBoard(
"puzzles/minesweeper/rules/IsolateMine3.txt", minesweeper);
TreeNode rootNode = minesweeper.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

MinesweeperBoard board = (MinesweeperBoard) transition.getBoard();

// confirm it is impossible to satisfy any of the squares
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(1, 1)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(0, 0)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(1, 0)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(2, 0)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(0, 1)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(2, 1)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(0, 2)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(1, 2)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(2, 2)));
}
}
19 changes: 19 additions & 0 deletions src/test/resources/puzzles/minesweeper/rules/IsolateMine1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="Minesweeper">
<board height="3" width="3">
<cells>
<cell value="0" x="0" y="0"/>
<cell value="0" x="1" y="0"/>
<cell value="0" x="2" y="0"/>
<cell value="0" x="0" y="1"/>
<cell value="-1" x="1" y="1"/>
<cell value="0" x="2" y="1"/>
<cell value="0" x="0" y="2"/>
<cell value="0" x="1" y="2"/>
<cell value="0" x="2" y="2"/>
</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
19 changes: 19 additions & 0 deletions src/test/resources/puzzles/minesweeper/rules/IsolateMine2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="Minesweeper">
<board height="3" width="3">
<cells>
<cell value="-2" x="0" y="0"/>
<cell value="-2" x="1" y="0"/>
<cell value="-2" x="2" y="0"/>
<cell value="-2" x="0" y="1"/>
<cell value="-1" x="1" y="1"/>
<cell value="-2" x="2" y="1"/>
<cell value="-2" x="0" y="2"/>
<cell value="-2" x="1" y="2"/>
<cell value="-2" x="2" y="2"/>
</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
19 changes: 19 additions & 0 deletions src/test/resources/puzzles/minesweeper/rules/IsolateMine3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="Minesweeper">
<board height="3" width="3">
<cells>
<cell value="-1" x="0" y="0"/>
<cell value="-1" x="1" y="0"/>
<cell value="-1" x="2" y="0"/>
<cell value="-1" x="0" y="1"/>
<cell value="-1" x="1" y="1"/>
<cell value="-1" x="2" y="1"/>
<cell value="-1" x="0" y="2"/>
<cell value="-1" x="1" y="2"/>
<cell value="-1" x="2" y="2"/>
</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>

0 comments on commit 67e5143

Please sign in to comment.