Skip to content

Commit

Permalink
Rewrite Columns Within Regions inference rule
Browse files Browse the repository at this point in the history
  • Loading branch information
summerhenson committed Aug 2, 2024
1 parent 562cdbb commit 54184df
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import edu.rpi.legup.puzzle.starbattle.StarBattleBoard;
import edu.rpi.legup.puzzle.starbattle.StarBattleCell;
import edu.rpi.legup.puzzle.starbattle.StarBattleCellType;
import io.opencensus.trace.Link;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

public class ColumnsWithinRegionsDirectRule extends DirectRule {
public ColumnsWithinRegionsDirectRule() {
Expand All @@ -23,6 +21,25 @@ public ColumnsWithinRegionsDirectRule() {
"edu/rpi/legup/images/starbattle/rules/ColumnsWithinRegionsDirectRule.png");
}

private void generateSubsets(List<List<Integer>> subsets, int current, int skip, int size) {
if (current == size) {
return;
}
List<List<Integer>> newSubsets = new LinkedList<List<Integer>>();
if (current != skip) {
for (List<Integer> subset: subsets) {
List<Integer> copy = new LinkedList<Integer>(subset);
copy.add(current);
newSubsets.add(copy);
}
subsets.addAll(newSubsets);
List<Integer> oneMember = new LinkedList<Integer>();
oneMember.add(current);
subsets.add(oneMember);
}
generateSubsets(subsets, current + 1 == skip ? current + 2 : current + 1, skip, size);
}

/**
* Checks whether the child node logically follows from the parent node at the specific
* puzzleElement index using this rule
Expand All @@ -34,13 +51,48 @@ public ColumnsWithinRegionsDirectRule() {
*/
@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
// assumption: the rule has been applied to its fullest extent and the rows and regions
// are now mutually encompassing

StarBattleBoard board = (StarBattleBoard) transition.getBoard();
StarBattleBoard origBoard = (StarBattleBoard) transition.getParents().get(0).getBoard();
StarBattleCell cell = (StarBattleCell) board.getPuzzleElement(puzzleElement);
int dim = board.getSize();
int region = cell.getGroupIndex();
int column = cell.getLocation().x;

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

List<List<Integer>> subsets = new LinkedList<List<Integer>>();
generateSubsets(subsets,0, column, dim);

for (List<Integer> columnSubset: subsets) {
Set<Integer> regions = new HashSet<Integer>();
boolean containsRegion = false;
int columnStars = 0;
int regionStars = 0;
for (int c: columnSubset) {
columnStars += board.columnStars(c);
for (StarBattleCell ce: origBoard.getCol(c)) {
if (ce.getType() == StarBattleCellType.UNKNOWN) {
if (regions.add(ce.getGroupIndex())) {
regionStars += board.getRegion(ce.getGroupIndex()).numStars();
}
if (ce.getGroupIndex() == region) {
containsRegion = true;
}
}
}
}
if (containsRegion && board.getPuzzleNumber() * columnSubset.size() - columnStars
>= board.getPuzzleNumber() * regions.size() - regionStars) {
return null;
}
}
/*
//StarBattleBoard modified = (StarBattleBoard) origBoard.copy();
//modified.getPuzzleElement(puzzleElement).setData(StarBattleCellType.BLACK.value);
// the columns that are contained
Set<Integer> columns = new HashSet<Integer>();
// the regions that contain them
Expand Down Expand Up @@ -94,7 +146,7 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
}
}
*/
return null;
return "Wrong!";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void ColumnsWithinRegionsDirectRule_FalseColumnsWithinRegions2()
}

@Test
public void ColumnsWithinRegionsDirectRule_FalseColumnsWithinRegions3()
public void ColumnsWithinRegionsDirectRule_PartialRemoval()
throws InvalidFileFormatException {
TestUtilities.importTestBoard("puzzles/starbattle/rules/ColumnsWithinRegionsDirectRule/PartialColumnTwoCells", starbattle);
TreeNode rootNode = starbattle.getTree().getRootNode();
Expand All @@ -280,11 +280,18 @@ public void ColumnsWithinRegionsDirectRule_FalseColumnsWithinRegions3()
cell1.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell1);

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

Point location1 = new Point(0, 1);
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)));
Point point = new Point(k,i);
if (point.equals(location1)) {
Assert.assertNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
else {
Assert.assertNotNull(RULE.checkRuleAt(transition, board.getCell(k, i)));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,18 @@ public void RegionsWithinColumnsDirectRule_FalseRegionsWithinColumns4()
cell.setData(StarBattleCellType.BLACK.value);
board.addModifiedData(cell);

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

Point location = new Point(0,3);
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)));
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)));
}
}
}
}
Expand Down

0 comments on commit 54184df

Please sign in to comment.