Skip to content

Commit

Permalink
Merge pull request #828 from summerhenson/star-battle
Browse files Browse the repository at this point in the history
Star battle
  • Loading branch information
offline171 authored Jun 21, 2024
2 parents c0fc026 + ac95969 commit 4a1ab37
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
int rowCount = 0;
int columnCount = 0;
for (int i = 0; i < sbBoard.getSize(); ++i) {
if (sbBoard.getCell(row, i).getType() != StarBattleCellType.BLACK) {
if (sbBoard.getCell(i, row).getType() != StarBattleCellType.BLACK) {
++rowCount;
}
if (sbBoard.getCell(i, column).getType() != StarBattleCellType.BLACK) {
if (sbBoard.getCell(column, i).getType() != StarBattleCellType.BLACK) {
++columnCount;
}
}

if (rowCount < sbBoard.getPuzzleNumber() || columnCount < sbBoard.getPuzzleNumber()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ public void ClashingOrbitContradictionRule_FalseContradiction()

for (int i = 0; i < board.getHeight(); ++i) {
for (int j = 0; j < board.getWidth(); ++j) {
Point point = new Point(j,i);
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}
Expand Down
173 changes: 173 additions & 0 deletions src/test/java/puzzles/starbattle/rules/SurroundStarDirectRuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package puzzles.starbattle.rules;

import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.starbattle.StarBattle;
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;
import edu.rpi.legup.puzzle.starbattle.rules.SurroundStarDirectRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import java.awt.*;
import legup.MockGameBoardFacade;
import legup.TestUtilities;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class SurroundStarDirectRuleTest {

private static final SurroundStarDirectRule RULE = new SurroundStarDirectRule();
private static StarBattle starbattle;

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

@Test
public void SurroundStarDirectRule_CenterStarOneTile() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/SurroundStarDirectRule/CenterStar", starbattle);
TreeNode rootNode = starbattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell = board.getCell(0,1);
cell.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell);

Assert.assertNull(RULE.checkRule(transition));

Point location = new Point(0, 1);
for (int i = 0; i < board.getHeight(); i++) {
for (int k = 0; k < board.getWidth(); k++) {
Point point = new Point(k, i);
if (point.equals(location)) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
} else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
}
}
}

@Test
public void SurroundStarDirectRule_CenterStarOneTileDiagonal() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/SurroundStarDirectRule/CenterStar", starbattle);
TreeNode rootNode = starbattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell = board.getCell(0,0);
cell.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell);

Assert.assertNull(RULE.checkRule(transition));

Point location = new Point(0, 0);
for (int i = 0; i < board.getHeight(); i++) {
for (int k = 0; k < board.getWidth(); k++) {
Point point = new Point(k, i);
if (point.equals(location)) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
} else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
}
}
}

@Test
public void SurroundStarDirectRule_CenterStarAllTiles()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/SurroundStarDirectRule/CenterStar", starbattle);
TreeNode rootNode = starbattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
for (int i = 0; i < board.getWidth(); i++) {
for (int j = 0; j < board.getHeight(); j++) {
if (i != 1 || j != 1) {
StarBattleCell cell = board.getCell(i,j);
cell.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell);
}
}
}

Assert.assertNull(RULE.checkRule(transition));

Point location = new Point(1, 1);
for (int i = 0; i < board.getHeight(); i++) {
for (int k = 0; k < board.getWidth(); k++) {
Point point = new Point(k, i);
if (point.equals(location)) {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
} else {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
}
}
}

@Test
public void SurroundStarDirectRule_CornerStar() throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/SurroundStarDirectRule/CornerStar", starbattle);
TreeNode rootNode = starbattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell1 = board.getCell(0,1);
cell1.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell1);
StarBattleCell cell2 = board.getCell(1,0);
cell2.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell2);
StarBattleCell cell3 = board.getCell(1,1);
cell3.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell3);

Assert.assertNull(RULE.checkRule(transition));

Point location1 = new Point(0, 1);
Point location2 = new Point(1,0);
Point location3 = new Point(1,1);
for (int i = 0; i < board.getHeight(); i++) {
for (int k = 0; k < board.getWidth(); k++) {
Point point = new Point(k, i);
if (point.equals(location1) || point.equals(location2) || point.equals(location3)) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
} else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
}
}
}

@Test
public void SurroundStarDirectRule_FalseSurroundStar()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/SurroundStarDirectRule/CornerStar", starbattle);
TreeNode rootNode = starbattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell = board.getCell(2,0);
cell.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell);

Assert.assertNotNull(RULE.checkRule(transition));

for (int i = 0; i < board.getHeight(); i++) {
for (int k = 0; k < board.getWidth(); k++) {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;
import edu.rpi.legup.puzzle.starbattle.rules.TooFewStarsContradictionRule;
import edu.rpi.legup.save.InvalidFileFormatException;
import java.awt.*;
import legup.MockGameBoardFacade;
Expand All @@ -16,12 +17,123 @@
import org.junit.Test;

public class TooFewStarsContradictionRuleTest {
private static final TooFewStarsContradictionRule RULE = new TooFewStarsContradictionRule();
private static StarBattle starBattle;

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

/*Too few stars in column */
@Test
public void TooFewStarsContradictionRule_Column()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/TooFewStarsContradictionRule/Column", starBattle);
TreeNode rootNode = starBattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell1 = board.getCell(0,0);
StarBattleCell cell2 = board.getCell(0,1);
StarBattleCell cell3 = board.getCell(0,2);
StarBattleCell cell4 = board.getCell(0,3);

Assert.assertNull(RULE.checkContradiction((StarBattleBoard) transition.getBoard()));
for (int i = 0; i < board.getHeight(); ++i) {
for (int j = 0; j < board.getWidth(); ++j) {
Point point = new Point(j,i);
if (point.equals(cell1.getLocation()) || point.equals(cell2.getLocation()) ||
point.equals(cell3.getLocation()) || point.equals(cell4.getLocation())) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}

}

/*Too few stars in row*/
@Test
public void TooFewStarsContradictionRule_Row()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/TooFewStarsContradictionRule/Row", starBattle);
TreeNode rootNode = starBattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell1 = board.getCell(0,0);
StarBattleCell cell2 = board.getCell(1,0);
StarBattleCell cell3 = board.getCell(2,0);
StarBattleCell cell4 = board.getCell(3,0);

Assert.assertNull(RULE.checkContradiction((StarBattleBoard) transition.getBoard()));
for (int i = 0; i < board.getHeight(); ++i) {
for (int j = 0; j < board.getWidth(); ++j) {
Point point = new Point(j,i);
if (point.equals(cell1.getLocation()) || point.equals(cell2.getLocation()) ||
point.equals(cell3.getLocation()) || point.equals(cell4.getLocation())) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}
}

/*Too few stars in region*/
@Test
public void TooFewStarsContradictionRule_Region()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/TooFewStarsContradictionRule/Region", starBattle);
TreeNode rootNode = starBattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell1 = board.getCell(0,0);
StarBattleCell cell2 = board.getCell(0,1);
StarBattleCell cell3 = board.getCell(1,0);
StarBattleCell cell4 = board.getCell(1,1);

Assert.assertNull(RULE.checkContradiction((StarBattleBoard) transition.getBoard()));
for (int i = 0; i < board.getHeight(); ++i) {
for (int j = 0; j < board.getWidth(); ++j) {
Point point = new Point(j,i);
if (point.equals(cell1.getLocation()) || point.equals(cell2.getLocation()) ||
point.equals(cell3.getLocation()) || point.equals(cell4.getLocation())) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}
}

/*False contradiction*/
@Test
public void TooFewStarsContradictionRule_FalseContradiction()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/TooFewStarsContradictionRule/FalseContradiction", starBattle);
TreeNode rootNode = starBattle.getTree().getRootNode();
TreeTransition transition = rootNode.getChildren().get(0);
transition.setRule(RULE);

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

Assert.assertNotNull(RULE.checkContradiction((StarBattleBoard) transition.getBoard()));
for (int i = 0; i < board.getHeight(); ++i) {
for (int j = 0; j < board.getWidth(); ++j) {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Legup version="2.0.0">
<puzzle name="StarBattle">
<board size="3" puzzle_num="1">
<region>
<cells>
<cell value="0" x="0" y="0"/>
<cell value="0" x="0" y="1"/>
<cell value="0" x="0" y="2"/>
</cells>
</region>
<region>
<cells>
<cell value="0" x="1" y="0"/>
<cell value="-2" x="1" y="1"/>
<cell value="0" x="1" y="2"/>
</cells>
</region>
<region>
<cells>
<cell value="0" x="2" y="0"/>
<cell value="0" x="2" y="1"/>
<cell value="0" x="2" y="2"/>
</cells>
</region>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Legup version="2.0.0">
<puzzle name="StarBattle">
<board size="3" puzzle_num="1">
<region>
<cells>
<cell value="-2" x="0" y="0"/>
<cell value="0" x="0" y="1"/>
<cell value="0" x="0" y="2"/>
</cells>
</region>
<region>
<cells>
<cell value="0" x="1" y="0"/>
<cell value="0" x="1" y="1"/>
<cell value="0" x="1" y="2"/>
</cells>
</region>
<region>
<cells>
<cell value="0" x="2" y="0"/>
<cell value="0" x="2" y="1"/>
<cell value="0" x="2" y="2"/>
</cells>
</region>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
Loading

0 comments on commit 4a1ab37

Please sign in to comment.