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

Lightup Documentation and Test Suite (Computability and Logic Project) #538

Merged
merged 14 commits into from
Apr 14, 2023
Merged
2 changes: 1 addition & 1 deletion bin/main/edu/rpi/legup/legup/config
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<puzzle name="LightUp"
qualifiedClassName="edu.rpi.legup.puzzle.lightup.LightUp"
fileType=".xml"
fileCreationDisabled="true"/>
fileCreationDisabled="false"/>
<puzzle name="Masyu"
qualifiedClassName="edu.rpi.legup.puzzle.masyu.Masyu"
fileType=".xml"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package puzzles.lightup.rules;

import edu.rpi.legup.puzzle.lightup.LightUpBoard;
import legup.MockGameBoardFacade;
import legup.TestUtilities;
import edu.rpi.legup.model.PuzzleImporter;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import org.junit.Assert;
Expand All @@ -17,13 +15,10 @@
public class BulbsInPathContradictionRuleTest {
private static final BulbsInPathContradictionRule RULE = new BulbsInPathContradictionRule();
private static LightUp lightUp;
private static PuzzleImporter importer;

@BeforeClass
public static void setUp() {
MockGameBoardFacade.getInstance();
lightUp = new LightUp();
importer = lightUp.getImporter();
}

@Test
Expand All @@ -34,6 +29,7 @@ public void BulbsInPathContradictionRule_LightInHorizontalPath() throws InvalidF
transition.setRule(RULE);

LightUpBoard board = (LightUpBoard) transition.getBoard();
//confirm there is a contradiction somewhere on the board
Assert.assertNull(RULE.checkContradiction(board));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(0, 0)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(2, 0)));
Expand All @@ -49,6 +45,7 @@ public void BulbsInPathContradictionRule_LightInVerticalPath() throws InvalidFil
transition.setRule(RULE);

LightUpBoard board = (LightUpBoard) transition.getBoard();
//confirm there is a contradiction somewhere on the board
Assert.assertNull(RULE.checkContradiction(board));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(0, 0)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(0, 2)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
package puzzles.lightup.rules;

import edu.rpi.legup.puzzle.lightup.LightUpBoard;
import legup.TestUtilities;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import org.junit.Assert;
import edu.rpi.legup.puzzle.lightup.LightUp;
import edu.rpi.legup.puzzle.lightup.rules.CannotLightACellContradictionRule;
import edu.rpi.legup.save.InvalidFileFormatException;

import org.junit.BeforeClass;
import org.junit.Test;
import edu.rpi.legup.puzzle.lightup.LightUp;

public class CannotLightACellContradictionRuleTest {
private static final CannotLightACellContradictionRule RULE = new CannotLightACellContradictionRule();
private static LightUp lightUp;

@BeforeClass
public static void setUp() {
lightUp = new LightUp();
}

@Test
public void simpleCaseTest() {
@Test
//extensive full testing of null and non-null in a 5x5 board
public void FullLightTest() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/lightup/rules/CannotLightACellContradictionRule/FullLightTest", lightUp);
TreeNode rootNode = lightUp.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

LightUpBoard board = (LightUpBoard) transition.getBoard();
//confirm there is a contradiction somewhere on the board
Assert.assertNull(RULE.checkContradiction(board));

//confirm it is impossible to light up these squares
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(1, 3)));
Assert.assertNull(RULE.checkContradictionAt(board, board.getCell(3, 3)));

//confirm these are not required to be lit because they are already lit or unable to be
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(0, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 1)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 0)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(3, 2)));
}

@Test
//simple contradiction testing for null and non-null in a 3x3 board
public void CannotLightMiddleTest() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/lightup/rules/CannotLightACellContradictionRule/CannotLight", lightUp);
TreeNode rootNode = lightUp.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

LightUpBoard board = (LightUpBoard) transition.getBoard();
//confirm there is a contradiction somewhere on the board
Assert.assertNull(RULE.checkContradiction(board));

//confirm it is impossible to light 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(0, 2)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(1, 2)));
Assert.assertNotNull(RULE.checkContradictionAt(board, board.getCell(2, 1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
import org.junit.BeforeClass;
import org.junit.Test;
import edu.rpi.legup.puzzle.lightup.LightUp;
import edu.rpi.legup.puzzle.lightup.rules.EmptyCellinLightBasicRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.TestUtilities;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.lightup.LightUpBoard;
import edu.rpi.legup.puzzle.lightup.LightUpCell;
import edu.rpi.legup.puzzle.lightup.LightUpCellType;
import org.junit.Assert;

public class EmptyCellinLightBasicRuleTest {
private static final EmptyCellinLightBasicRule RULE = new EmptyCellinLightBasicRule();
private static LightUp lightUp;

@BeforeClass
Expand All @@ -13,7 +23,58 @@ public static void setUp() {
}

@Test
public void simpleCaseTest() {
//tests a 3x3 board with with a 0 black tile in the center and lightbulbs in top left and bototm right
//confirms the rest of the tiles must be empty
public void EmptyCellinLightBasicRule() throws InvalidFileFormatException{
TestUtilities.importTestBoard("puzzles/lightup/rules/EmptyCellinLightBasicRule/EmptyCells", lightUp);
TreeNode rootNode = lightUp.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

//get board state
LightUpBoard board = (LightUpBoard) transition.getBoard();

//change the board's cells considering the emptycellinlight rule
LightUpCell cell2 = board.getCell(1,0);
cell2.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell2);

LightUpCell cell3 = board.getCell(0,1);
cell3.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell3);

LightUpCell cell4 = board.getCell(2,0);
cell4.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell4);

LightUpCell cell5 = board.getCell(0,2);
cell5.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell5);

LightUpCell cell6 = board.getCell(1,2);
cell6.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell6);

LightUpCell cell7 = board.getCell(2,1);
cell7.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell7);

//confirm there is a logical following of the EmptyCellinLight rule
Assert.assertNull(RULE.checkRule(transition));

//cells (0,0) and (2,2) are not empty because they have lightbulbs, and (1,1)
//because it is a black tile. Confirm the rest are empty
LightUpCell c;
for (int i = 0; i < board.getHeight(); i++) {
for (int j = 0; j < board.getWidth(); j++) {
c = board.getCell(j, i);
if ((i == 0 && j == 0) || (i == 2 && j == 2) || (i == 1 && j == 1)){
Assert.assertNotNull(RULE.checkRuleAt(transition, c));
}
else {
Assert.assertNull(RULE.checkRuleAt(transition, c));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,61 @@
import org.junit.BeforeClass;
import org.junit.Test;
import edu.rpi.legup.puzzle.lightup.LightUp;
import edu.rpi.legup.puzzle.lightup.rules.EmptyCornersBasicRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.TestUtilities;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.lightup.LightUpBoard;
import edu.rpi.legup.puzzle.lightup.LightUpCell;
import edu.rpi.legup.puzzle.lightup.LightUpCellType;
import org.junit.Assert;

public class EmptyCornersBasicRuleTest {
private static final EmptyCornersBasicRule RULE = new EmptyCornersBasicRule();
private static LightUp lightUp;


@BeforeClass
public static void setUp() {
lightUp = new LightUp();
}

@Test
public void simpleCaseTest() {
public void EmptyCornersTest() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/lightup/rules/EmptyCornersBasicRule/EmptyCorners", lightUp);
TreeNode rootNode = lightUp.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

//get board state
LightUpBoard board = (LightUpBoard) transition.getBoard();

//change the board's cells considering the EmptyCorners rule to empty
LightUpCell cell1 = board.getCell(2,2);
cell1.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell1);

LightUpCell cell2 = board.getCell(0,2);
cell2.setData(LightUpCellType.EMPTY.value);
board.addModifiedData(cell2);

//confirm there is a logical following of the EmptyCorners rule
Assert.assertNull(RULE.checkRule(transition));

//confirm the two expected cells are emptied USING THE RULE
// and none of the rest are (others can be empty just not by the same rule)
LightUpCell c;
for (int i = 0; i < board.getHeight(); i++) {
for (int j = 0; j < board.getWidth(); j++) {
c = board.getCell(j, i);
if ((i == 2 && j == 0) || (i == 2 && j == 2)){
Assert.assertNull(RULE.checkRuleAt(transition, c));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, c));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
import org.junit.BeforeClass;
import org.junit.Test;
import edu.rpi.legup.puzzle.lightup.LightUp;
import edu.rpi.legup.puzzle.lightup.rules.FinishWithBulbsBasicRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import legup.TestUtilities;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.lightup.LightUpBoard;
import edu.rpi.legup.puzzle.lightup.LightUpCell;
import edu.rpi.legup.puzzle.lightup.LightUpCellType;
import org.junit.Assert;

public class FinishWithBulbsBasicRuleTest {
private static final FinishWithBulbsBasicRule RULE = new FinishWithBulbsBasicRule();
private static LightUp lightUp;

@BeforeClass
Expand All @@ -13,7 +23,81 @@ public static void setUp() {
}

@Test
public void simpleCaseTest() {
public void FinishBulbTest() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/lightup/rules/FinishWithBulbsBasicRule/FinishWithBulbs", lightUp);
TreeNode rootNode = lightUp.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

//get board state
LightUpBoard board = (LightUpBoard) transition.getBoard();

//change the board's cells considering the FinishWithBulbs rule to empty
LightUpCell cell1 = board.getCell(1,0);
cell1.setData(LightUpCellType.BULB.value);
board.addModifiedData(cell1);

//confirm there is a logical following of the FinishWithBulbs rule
Assert.assertNull(RULE.checkRule(transition));

//check every square except the top center (2,0)
LightUpCell c;
for (int i = 0; i < board.getHeight(); i++) {
for (int j = 0; j < board.getWidth(); j++) {
c = board.getCell(j, i);
if (i == 0 && j == 1){
//logically follows
Assert.assertNull(RULE.checkRuleAt(transition, c));
}
else {
//does not use the rule to logically follow
Assert.assertNotNull(RULE.checkRuleAt(transition, c));
}
}
}
}

//even though this test isnt a completely filled board because it is unsolveable, it tests FinishBulbs properly
@Test
public void FinishBulbTestWithThree() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/lightup/rules/FinishWithBulbsBasicRule/FinishWithBulbsWithThree", lightUp);
TreeNode rootNode = lightUp.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

//get board state
LightUpBoard board = (LightUpBoard) transition.getBoard();

//change the board's cells considering the FinishWithBulbs rule to empty
LightUpCell cell1 = board.getCell(1,2);
cell1.setData(LightUpCellType.BULB.value);
board.addModifiedData(cell1);

LightUpCell cell2 = board.getCell(0,1);
cell2.setData(LightUpCellType.BULB.value);
board.addModifiedData(cell2);

LightUpCell cell3 = board.getCell(2,1);
cell3.setData(LightUpCellType.BULB.value);
board.addModifiedData(cell3);

//confirm there is a logical following of the FinishWithBulbs rule
Assert.assertNull(RULE.checkRule(transition));

//check every square for logical following
LightUpCell c;
for (int i = 0; i < board.getHeight(); i++) {
for (int j = 0; j < board.getWidth(); j++) {
c = board.getCell(j, i);
if ((i == 1 && j == 2) || (i == 2 && j == 1) || (i == 1 && j == 0)){
//logically follows
Assert.assertNull(RULE.checkRuleAt(transition, c));
}
else {
//does not use the rule to logically follow
Assert.assertNotNull(RULE.checkRuleAt(transition, c));
}
}
}
}
}
Loading