-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New rule isolatemine - a mine must see a number cell. Added the rule and test suite for the rule.
- Loading branch information
1 parent
94d74f0
commit 67e5143
Showing
7 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/edu/rpi/legup/puzzle/minesweeper/rules/IsolateMineContradictionRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Binary file added
BIN
+1.52 KB
src/main/resources/edu/rpi/legup/images/minesweeper/contradictions/IsolateMine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 95 additions & 0 deletions
95
src/test/java/puzzles/minesweeper/IsolateMineContradictionRuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/test/resources/puzzles/minesweeper/rules/IsolateMine1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/test/resources/puzzles/minesweeper/rules/IsolateMine2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/test/resources/puzzles/minesweeper/rules/IsolateMine3.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |