Skip to content

Commit

Permalink
Merge pull request #845 from offline171/star-battle
Browse files Browse the repository at this point in the history
Star battle Empty Adjacent Rule (wip)
  • Loading branch information
summerhenson authored Jul 26, 2024
2 parents 57f09be + f99a1d1 commit 9abd9c0
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package edu.rpi.legup.puzzle.starbattle.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.model.rules.DirectRule;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.nurikabe.NurikabeCell;
import edu.rpi.legup.puzzle.nurikabe.NurikabeType;
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;

public class EmptyAdjacentDirectRule extends DirectRule {

public EmptyAdjacentDirectRule() {
super(
"STBL-BASC-0010",
"Empty Adjacent",
"Tiles next to other tiles that need to contain a star to reach the puzzle number for their region/row/column need to be blacked out.",
"edu/rpi/legup/images/starbattle/rules/EmptyAdjacentDirectRule.png");
}

/**
* Checks whether the child node logically follows from the parent node at the specific
* puzzleElement index using this rule
*
* @param transition transition to check
* @param puzzleElement equivalent puzzleElement
* @return null if the child node logically follow from the parent node at the specified
* puzzleElement, otherwise error message
*/
@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleBoard origBoard = (StarBattleBoard) transition.getParents().get(0).getBoard();
ContradictionRule contraRule = new TooFewStarsContradictionRule();

StarBattleCell cell = (StarBattleCell) board.getPuzzleElement(puzzleElement);

if (cell.getType() != StarBattleCellType.BLACK) {
return super.getInvalidUseOfRuleMessage()
+ ": Only black cells are allowed for this rule!";
}

int x = cell.getLocation().x;
int y = cell.getLocation().y;

StarBattleCell northWest = board.getCell(x - 1, y - 1);
StarBattleCell north = board.getCell(x, y - 1);
StarBattleCell northEast = board.getCell(x + 1, y - 1);
StarBattleCell west = board.getCell(x - 1, y);
StarBattleCell east = board.getCell(x + 1, y);
StarBattleCell southWest = board.getCell(x - 1, y + 1);
StarBattleCell south = board.getCell(x, y + 1);
StarBattleCell southEast = board.getCell(x + 1, y + 1);

StarBattleCell[] adjacent = {northWest, north, northEast, west, east, southWest, south, southEast};

StarBattleBoard modified = (StarBattleBoard) origBoard.copy();
modified.getPuzzleElement(puzzleElement).setData(StarBattleCellType.STAR.value);
for(int i = 0; i < 8; i++){ //sets each spot to a black square if not filled
StarBattleCell temp = adjacent[i];

if (temp != null && temp.getType() == StarBattleCellType.UNKNOWN) {
temp.setData(StarBattleCellType.BLACK.value);
int X = temp.getLocation().x;
int Y = temp.getLocation().y;
modified.getCell(X,Y).setData(StarBattleCellType.BLACK.value);
System.out.println("covering square " + X + " " + Y + " type " + modified.getCell(X,Y).getType() + " i = " + i + "\n");
if(contraRule.checkContradictionAt(modified, temp) == null){
System.out.println("Good job!");
return null; //used correctly if even one space causes a toofewstars issue
}
}
}
System.out.println("Wait why did this exit?\n");

return "Black cells must be placed adjacent to a tile(s) where a star is needed!";
}

/**
* Creates a transition {@link Board} that has this rule applied to it using the {@link
* TreeNode}.
*
* @param node tree node used to create default transition board
* @return default board or null if this rule cannot be applied to this tree node
*/
@Override
public Board getDefaultBoard(TreeNode node) {

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
++columnCount;
}
}

System.out.println("rowCount = " + rowCount + " columnCount = " + columnCount + " at " + column + "," + row + "\n");
if (rowCount < sbBoard.getPuzzleNumber() || columnCount < sbBoard.getPuzzleNumber()) {
System.out.println("Returning Null\n");
return null;
}
StarBattleRegion region = sbBoard.getRegion(cell);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Regions Within Rows: STBL-BASC-0006
Rows Within Columns: STBL-BASC-0007
Rows Within Regions: STBL-BASC-0008
Surround Star: STBL-BASC-0009
Empty Adjacent: STBL-BASC-0010

Contradiction Rules:
Too Many Stars: STBL-CONT-0001
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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.EmptyAdjacentDirectRule;
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 EmptyAdjacentDirectRuleTest {

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

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

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

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell1 = board.getCell(1,1);
cell1.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell2 = board.getCell(2,1);
cell2.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell3 = board.getCell(3,1);
cell3.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell4 = board.getCell(1,3);
cell4.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell5 = board.getCell(2,3);
cell5.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell6 = board.getCell(3,3);
cell6.setData(StarBattleCellType.BLACK.value);

board.addModifiedData(cell1);
board.addModifiedData(cell2);
board.addModifiedData(cell3);
board.addModifiedData(cell4);
board.addModifiedData(cell5);
board.addModifiedData(cell6);

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

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()) ||
point.equals(cell5.getLocation()) || point.equals(cell6.getLocation())) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}
}

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

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleCell cell1 = board.getCell(1,1);
cell1.setData(StarBattleCellType.BLACK.value);
/*
StarBattleCell cell2 = board.getCell(2,1);
cell2.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell3 = board.getCell(1,3);
cell3.setData(StarBattleCellType.BLACK.value);
StarBattleCell cell4 = board.getCell(2,3);
cell4.setData(StarBattleCellType.BLACK.value);
*/

board.addModifiedData(cell1);
/*
board.addModifiedData(cell2);
board.addModifiedData(cell3);
board.addModifiedData(cell4);
*/

Assert.assertNull(RULE.checkRule(transition));
System.out.println("General Case is done\n");
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())) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
} /*
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
} */
}
}
}

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

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

board.addModifiedData(cell1);
board.addModifiedData(cell2);

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

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())) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(j, i)));
}
}
}
}

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

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

board.addModifiedData(cell1);
board.addModifiedData(cell2);
board.addModifiedData(cell3);
board.addModifiedData(cell4);

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

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

0 comments on commit 9abd9c0

Please sign in to comment.