Skip to content

Commit

Permalink
Surround completed region newest (#673)
Browse files Browse the repository at this point in the history
* Update SurroundRegionDirectRule.java

* Update SurroundRegionDirectRule.java

* Update SurroundRegionDirectRule.java

* Revised `directions` to initialize in one line.

---------

Co-authored-by: Ivan Ho <[email protected]>
Co-authored-by: Corppet <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2023
1 parent 14ef71c commit 91366b0
Showing 1 changed file with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.DirectRule;
import edu.rpi.legup.model.rules.ContradictionRule;
import edu.rpi.legup.model.tree.TreeNode;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.nurikabe.NurikabeBoard;
import edu.rpi.legup.puzzle.nurikabe.NurikabeCell;
import edu.rpi.legup.puzzle.nurikabe.NurikabeType;
import edu.rpi.legup.puzzle.nurikabe.NurikabeUtilities;
import edu.rpi.legup.utility.DisjointSets;

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

public class SurroundRegionDirectRule extends DirectRule {

Expand All @@ -29,7 +37,6 @@ public SurroundRegionDirectRule() {
*/
@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
ContradictionRule contraRule = new TooManySpacesContradictionRule();

NurikabeBoard destBoardState = (NurikabeBoard) transition.getBoard();
NurikabeBoard origBoardState = (NurikabeBoard) transition.getParents().get(0).getBoard();
Expand All @@ -44,12 +51,35 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
NurikabeCell modCell = (NurikabeCell) modified.getPuzzleElement(puzzleElement);
modCell.setData(NurikabeType.WHITE.toValue());

if (contraRule.checkContradiction(modified) == null) {
return null;
}
else {
return "Does not follow from this rule at this index";
if(cell.getType() == NurikabeType.BLACK) {
DisjointSets<NurikabeCell> regions = NurikabeUtilities.getNurikabeRegions(destBoardState);
Set<NurikabeCell> adj = new HashSet<>(); //set to hold adjacent cells
Point loc = cell.getLocation(); //position of placed cell
List<Point> directions = Arrays.asList(new Point(-1, 0), new Point(1, 0), new Point(0, -1), new Point(0, 1));
for(Point direction : directions) {
NurikabeCell curr = destBoardState.getCell(loc.x + direction.x, loc.y + direction.y);
if(curr != null) {
if(curr.getType() == NurikabeType.WHITE || curr.getType() == NurikabeType.NUMBER) {
adj.add(curr); //adds cells to adj only if they are white or number blocks
}
}
}
List<NurikabeCell> numberedCells = new ArrayList<>(); //number value of number cells
for (NurikabeCell c : adj) { //loops through adjacent cells
Set<NurikabeCell> disRow = regions.getSet(c); //set of white spaces
for (NurikabeCell d : disRow) { //loops through white spaces
if (d.getType() == NurikabeType.NUMBER) { //if the white space is a number
numberedCells.add(d); //add that number to numberedCells
}
}
}
for (NurikabeCell number : numberedCells) { //loops through numberedCells
if (regions.getSet(number).size() == number.getData()) { //if that cells white area is the exact
return null; //size of the number of one of the number cells within that set
}
}
}
return "Does not follow from this rule at this index";
}

/**
Expand Down

0 comments on commit 91366b0

Please sign in to comment.