Skip to content

Commit

Permalink
Test suites for case rules
Browse files Browse the repository at this point in the history
Created the test suites for both of the case rules. Also some minor changes
  • Loading branch information
willyoung366 committed Nov 27, 2024
1 parent fafd7d6 commit 8c7ebf8
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public CaseBoard getCaseBoard(Board board) {
}

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

Board case1 = board.copy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.awt.*;

public class FinishWithEmptyTest {
public class FinishWithEmptyDirectRuleTest {

public static final FinishWithEmpty RULE = new FinishWithEmpty();
private static Minesweeper minesweeper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.awt.*;

public class FinishWithMinesTest {
public class FinishWithMinesDirectRuleTest {

public static final FinishWithMines RULE = new FinishWithMines();
private static Minesweeper minesweeper;
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/puzzles/minesweeper/MineOrEmptyCaseRuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package puzzles.minesweeper;

import edu.rpi.legup.model.gameboard.Board;
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.MinesweeperCell;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperTileData;
import edu.rpi.legup.puzzle.minesweeper.rules.MineOrEmptyCaseRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.MockGameBoardFacade;
import legup.TestUtilities;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.awt.*;
import java.util.ArrayList;

public class MineOrEmptyCaseRuleTest {

private static final MineOrEmptyCaseRule RULE = new MineOrEmptyCaseRule();
private static Minesweeper minesweeper;

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

/**
* Tests the Mine or Empty case rule by ensuring that it results in two children, that contain
* a modified cell that is either black or white
*/
@Test
public void MineOrEmptyCaseRuleTest1()
throws InvalidFileFormatException {
TestUtilities.importTestBoard(
"puzzles/minesweeper/rules/MineOrEmpty.txt", minesweeper);
TreeNode rootNode = minesweeper.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

MinesweeperBoard board = (MinesweeperBoard) transition.getBoard();
MinesweeperCell cell = board.getCell(0, 0);
ArrayList<Board> cases = RULE.getCases(board, cell);

Assert.assertEquals(2, cases.size());

MinesweeperBoard caseBoard = (MinesweeperBoard) cases.get(0);
MinesweeperBoard caseBoard2 = (MinesweeperBoard) cases.get(1);

MinesweeperTileData board1Type = caseBoard.getCell(0, 0).getData();
MinesweeperTileData board2Type = caseBoard2.getCell(0, 0).getData();

Assert.assertTrue(
((board1Type.equals(MinesweeperTileData.mine()) || board1Type.equals(MinesweeperTileData.empty())))
&& (board2Type.equals(MinesweeperTileData.mine()) || board2Type.equals(MinesweeperTileData.empty())));
Assert.assertFalse(board1Type.equals(board2Type));

Assert.assertEquals(caseBoard.getHeight(), caseBoard2.getHeight(), board.getHeight());
Assert.assertEquals(caseBoard.getWidth(), caseBoard2.getWidth(), board.getWidth());

for (int i = 0; i < caseBoard.getHeight(); i++) {
for (int k = 0; k < caseBoard.getWidth(); k++) {
Point point = new Point(k, i);
if (point.equals(caseBoard.getCell(k, i).getLocation())) {
continue;
}
Assert.assertTrue(caseBoard.getCell(k, i).equals(caseBoard2.getCell(k, i)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.awt.*;

public class NonTouchingSharedEmptyTest {
public class NonTouchingSharedEmptyDirectRuleTest {

public static final NonTouchingSharedEmpty RULE = new NonTouchingSharedEmpty();
private static Minesweeper minesweeper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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.rules.FinishWithEmpty;
import edu.rpi.legup.puzzle.minesweeper.rules.NonTouchingSharedMine;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.MockGameBoardFacade;
Expand All @@ -17,7 +16,7 @@

import java.awt.*;

public class NonTouchingSharedMineTest {
public class NonTouchingSharedMineDirectRuleTest {

public static final NonTouchingSharedMine RULE = new NonTouchingSharedMine();
private static Minesweeper minesweeper;
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/puzzles/minesweeper/SatisfyNumberCaseRuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package puzzles.minesweeper;

import edu.rpi.legup.model.gameboard.Board;
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.MinesweeperCell;
import edu.rpi.legup.puzzle.minesweeper.MinesweeperTileData;
import edu.rpi.legup.puzzle.minesweeper.rules.SatisfyNumberCaseRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.MockGameBoardFacade;
import legup.TestUtilities;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.awt.*;
import java.util.ArrayList;

public class SatisfyNumberCaseRuleTest {

private static final SatisfyNumberCaseRule RULE = new SatisfyNumberCaseRule();
private static Minesweeper minesweeper;

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

/**
* Tests the Satisfy Number case rule by ensuring that it results in all possibilities
* for the number. This case tests a number 2 with three unset cells around it, so each
* case must a different arrangement of two bombs and one empty.
*/
@Test
public void SatisfyNumberCaseRuleTest1()
throws InvalidFileFormatException {
TestUtilities.importTestBoard(
"puzzles/minesweeper/rules/SatisfyNumber.txt", minesweeper);
TreeNode rootNode = minesweeper.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

MinesweeperBoard board = (MinesweeperBoard) transition.getBoard();
MinesweeperCell cell = board.getCell(1, 1);
ArrayList<Board> cases = RULE.getCases(board, cell);

Assert.assertEquals(3, cases.size());

MinesweeperBoard caseBoard = (MinesweeperBoard) cases.get(0);
MinesweeperBoard caseBoard2 = (MinesweeperBoard) cases.get(1);
MinesweeperBoard caseBoard3 = (MinesweeperBoard) cases.get(2);

MinesweeperTileData board1Tile1 = caseBoard.getCell(0, 1).getData();
MinesweeperTileData board1Tile2 = caseBoard.getCell(2, 1).getData();
MinesweeperTileData board1Tile3 = caseBoard.getCell(1, 2).getData();
MinesweeperTileData board2Tile1 = caseBoard2.getCell(0, 1).getData();
MinesweeperTileData board2Tile2 = caseBoard2.getCell(2, 1).getData();
MinesweeperTileData board2Tile3 = caseBoard2.getCell(1, 2).getData();
MinesweeperTileData board3Tile1 = caseBoard3.getCell(0, 1).getData();
MinesweeperTileData board3Tile2 = caseBoard3.getCell(2, 1).getData();
MinesweeperTileData board3Tile3 = caseBoard3.getCell(1, 2).getData();

Assert.assertTrue((board1Tile1.equals(MinesweeperTileData.mine()) && board1Tile2.equals(MinesweeperTileData.mine())
&& board1Tile3.equals(MinesweeperTileData.empty()))
|| (board1Tile1.equals(MinesweeperTileData.mine()) && board1Tile2.equals(MinesweeperTileData.empty()) && board1Tile3.equals(MinesweeperTileData.mine()))
|| ((board1Tile1.equals(MinesweeperTileData.empty()) && board1Tile2.equals(MinesweeperTileData.mine()) && board1Tile3.equals(MinesweeperTileData.mine()))));

Assert.assertTrue((board2Tile1.equals(MinesweeperTileData.mine()) && board2Tile2.equals(MinesweeperTileData.mine())
&& board2Tile3.equals(MinesweeperTileData.empty()))
|| (board2Tile1.equals(MinesweeperTileData.mine()) && board2Tile2.equals(MinesweeperTileData.empty()) && board2Tile3.equals(MinesweeperTileData.mine()))
|| ((board2Tile1.equals(MinesweeperTileData.empty()) && board2Tile2.equals(MinesweeperTileData.mine()) && board2Tile3.equals(MinesweeperTileData.mine()))));

Assert.assertTrue((board3Tile1.equals(MinesweeperTileData.mine()) && board3Tile2.equals(MinesweeperTileData.mine())
&& board3Tile3.equals(MinesweeperTileData.empty()))
|| (board3Tile1.equals(MinesweeperTileData.mine()) && board3Tile2.equals(MinesweeperTileData.empty()) && board3Tile3.equals(MinesweeperTileData.mine()))
|| ((board3Tile1.equals(MinesweeperTileData.empty()) && board3Tile2.equals(MinesweeperTileData.mine()) && board3Tile3.equals(MinesweeperTileData.mine()))));

Assert.assertFalse(board1Tile1.equals(board2Tile1) && board1Tile1.equals(board3Tile1));
Assert.assertFalse(board1Tile2.equals(board2Tile2) && board1Tile2.equals(board3Tile2));
Assert.assertFalse(board1Tile3.equals(board2Tile3) && board1Tile3.equals(board3Tile3));

Assert.assertEquals(caseBoard.getHeight(), caseBoard2.getHeight(), caseBoard3.getHeight());
Assert.assertEquals(caseBoard.getHeight(), board.getHeight());
Assert.assertEquals(caseBoard.getWidth(), caseBoard2.getWidth(), caseBoard3.getWidth());
Assert.assertEquals(caseBoard.getWidth(), board.getWidth());

for (int i = 0; i < caseBoard.getHeight(); i++) {
for (int k = 0; k < caseBoard.getWidth(); k++) {
Point point = new Point(k, i);
if (point.equals(caseBoard.getCell(k, i).getLocation())) {
continue;
}
Assert.assertTrue(caseBoard.getCell(k, i).equals(caseBoard2.getCell(k, i)));
}
}
}
}
10 changes: 10 additions & 0 deletions src/test/resources/puzzles/minesweeper/rules/MineOrEmpty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="Minesweeper">
<board height="3" width="3">
<cells>
</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
19 changes: 19 additions & 0 deletions src/test/resources/puzzles/minesweeper/rules/SatisfyNumber.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="-2" x="0" y="1"/>
<cell value="2" x="1" y="1"/>
<cell value="-2" x="2" y="1"/>
<cell value="0" x="0" y="2"/>
<cell value="-2" x="1" y="2"/>
<cell value="0" x="2" y="2"/>
</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>

0 comments on commit 8c7ebf8

Please sign in to comment.