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

Surround completed region newest #673

Merged
merged 6 commits into from
Nov 3, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
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.ArrayList;
import java.util.HashSet;
import java.util.Set;
Corppet marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.*;

public class SurroundRegionDirectRule extends DirectRule {
private final static Logger LOGGER = LogManager.getLogger("");
Corppet marked this conversation as resolved.
Show resolved Hide resolved

public SurroundRegionDirectRule() {
super("NURI-BASC-0007", "Surround Region",
Expand All @@ -29,7 +38,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 +52,51 @@ 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
NurikabeCell cellLeft = destBoardState.getCell(loc.x - 1, loc.y);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is logically correct, this code block uses duplicate code for each direction. I recommend iteration through each direction in a for loop rather than copy pasting the code four times.

Some pseudocode that might help:

import java.awt.Point;

List<Point> directions = { new Point(-1, 0), new Point(1, 0), new Point(0, -1), new Point(0, 1) };

for (Point direction : directions) {
    NurikabeCell cell = destBoardState.getCell(loc.x + direction.x, loc.y + direction.y);
    
    // avoid redundant nested clauses
    if (cell != null && (...) {
        // apply logic
    }
}

NurikabeCell cellRight = destBoardState.getCell(loc.x + 1, loc.y);
NurikabeCell cellTop = destBoardState.getCell(loc.x, loc.y + 1);
NurikabeCell cellBottom = destBoardState.getCell(loc.x, loc.y - 1);
if(cellLeft != null) {
if (cellLeft.getType() == NurikabeType.WHITE || cellLeft.getType() == NurikabeType.NUMBER) {
adj.add(cellLeft);
}
}
if(cellRight != null) {
if (cellRight.getType() == NurikabeType.WHITE || cellRight.getType() == NurikabeType.NUMBER) {
adj.add(cellRight);
}
}
if(cellTop != null) {
if (cellTop.getType() == NurikabeType.WHITE || cellTop.getType() == NurikabeType.NUMBER) {
adj.add(cellTop);
}
}
if(cellBottom != null) {
if (cellBottom.getType() == NurikabeType.WHITE || cellBottom.getType() == NurikabeType.NUMBER) {
adj.add(cellBottom);
}
}
//ads cells to adj only if they are white or number blocks
ArrayList<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
Loading