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

Automated Java code formatting changes #787

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/main/java/edu/rpi/legup/history/AutoCaseRuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class AutoCaseRuleCommand extends PuzzleCommand {

private List<TreeTransition> caseTrans;


/**
* AutoCaseRuleCommand Constructor creates a command for validating a case rule
*
Expand Down Expand Up @@ -117,7 +116,8 @@ public String getErrorString() {
return "The selection must produce at least one case";
}

int numberOfCaseRules = caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement()).size();
int numberOfCaseRules =
caseRule.getCases(caseBoard.getBaseBoard(), elementView.getPuzzleElement()).size();
System.out.println("Number of cases:" + numberOfCaseRules);
if (numberOfCaseRules > caseRule.MAX_CASES) {
return "The selection can produce a max of " + caseRule.MAX_CASES + " cases";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import edu.rpi.legup.puzzle.nurikabe.NurikabeType;
import edu.rpi.legup.puzzle.nurikabe.NurikabeUtilities;
import edu.rpi.legup.utility.DisjointSets;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -19,7 +18,8 @@

public class FinishRoomCaseRule extends CaseRule {
public FinishRoomCaseRule() {
super("NURI-CASE-0002",
super(
"NURI-CASE-0002",
"Finish Room",
"Room can be finished in up to five ways",
"edu/rpi/legup/images/nurikabe/cases/FinishRoom.png");
Expand All @@ -28,8 +28,8 @@ public FinishRoomCaseRule() {
}

/**
* Checks whether the {@link TreeTransition} logically follows from the parent node using this rule. This method is
* the one that should overridden in child classes.
* Checks whether the {@link TreeTransition} logically follows from the parent node using this
* rule. This method is the one that should overridden in child classes.
*
* @param transition transition to check
* @return null if the child node logically follow from the parent node, otherwise error message
Expand All @@ -39,21 +39,27 @@ public String checkRuleRaw(TreeTransition transition) {
NurikabeBoard destBoardState = (NurikabeBoard) transition.getBoard();
List<TreeTransition> childTransitions = transition.getParents().get(0).getChildren();
if (childTransitions.size() > 5) {
return super.getInvalidUseOfRuleMessage() + ": This case rule must have 5 or less children.";
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must have 5 or less children.";
}
if(childTransitions.size() < 2) {
return super.getInvalidUseOfRuleMessage() + ": This case rule must have 2 or more children.";
if (childTransitions.size() < 2) {
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must have 2 or more children.";
}
Set<Point> locations = new HashSet<>();
for (TreeTransition t1 : childTransitions) {
locations.add(((NurikabeCell) t1.getBoard().getModifiedData().iterator().next()).getLocation()); //loop see if matches
locations.add(
((NurikabeCell) t1.getBoard().getModifiedData().iterator().next())
.getLocation()); // loop see if matches
if (t1.getBoard().getModifiedData().size() != 1) {
return super.getInvalidUseOfRuleMessage() + ": This case rule must have 1 modified cell for each case.";
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must have 1 modified cell for each case.";
}
for (Point loc : locations) {
for (Point loc2 : locations) {
if (!(loc.equals(loc2)) && (loc.x == loc2.x) && (loc.y == loc2.y)) {
return super.getInvalidUseOfRuleMessage() + ": This case rule must alter a different cell for each case.";
return super.getInvalidUseOfRuleMessage()
+ ": This case rule must alter a different cell for each case.";
}
}
}
Expand All @@ -69,18 +75,32 @@ public CaseBoard getCaseBoard(Board board) {
DisjointSets<NurikabeCell> regions = NurikabeUtilities.getNurikabeRegions(nurikabeBoard);
nurikabeBoard.setModifiable(false);


for (PuzzleElement element : nurikabeBoard.getPuzzleElements()) { //loops all puzzle elements
if (((NurikabeCell) element).getType() == NurikabeType.NUMBER) { //if the tile is a white number block
Set<NurikabeCell> disRow = regions.getSet(((NurikabeCell) element)); //store the row of the white region
boolean only = true; //placeholder boolean of if the element being tested is the only number block in the room or not
for(NurikabeCell d : disRow) { //loops through tiles in the room
if((d.getType() == NurikabeType.NUMBER) && !(d.getData().equals(((NurikabeCell) element).getData()))) { //if found another number tile and it's data is different than the element we're working with
only = false; //set only to false
for (PuzzleElement element :
nurikabeBoard.getPuzzleElements()) { // loops all puzzle elements
if (((NurikabeCell) element).getType()
== NurikabeType.NUMBER) { // if the tile is a white number block
Set<NurikabeCell> disRow =
regions.getSet(
((NurikabeCell) element)); // store the row of the white region
boolean only =
true; // placeholder boolean of if the element being tested is the only
// number block in the room or not
for (NurikabeCell d : disRow) { // loops through tiles in the room
if ((d.getType() == NurikabeType.NUMBER)
&& !(d.getData()
.equals(
((NurikabeCell) element)
.getData()))) { // if found another number tile
// and it's data is different
// than the element we're
// working with
only = false; // set only to false
}
}
if (disRow.size() + 1 == ((NurikabeCell) element).getData() && only) { //if size of region is 1 less than the number block and the number block is only number block in the region
caseBoard.addPickableElement(element); //add that room as a pickable element
if (disRow.size() + 1 == ((NurikabeCell) element).getData()
&& only) { // if size of region is 1 less than the number block and the
// number block is only number block in the region
caseBoard.addPickableElement(element); // add that room as a pickable element
}
}
}
Expand All @@ -90,19 +110,25 @@ public CaseBoard getCaseBoard(Board board) {
/**
* Gets the possible cases at a specific location based on this case rule
*
* @param board the current board state
* @param board the current board state
* @param puzzleElement equivalent puzzleElement
* @return a list of elements the specified could be
*/
@Override
public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement) {//throws IllegalStateException {
ArrayList<Board> cases = new ArrayList<>(); //makes array list of cases
NurikabeBoard nuriBoard = (NurikabeBoard) board.copy(); //nurikabe board to edit
NurikabeCell numbaCell = nuriBoard.getCell(((NurikabeCell) puzzleElement).getLocation().x,
((NurikabeCell) puzzleElement).getLocation().y); //number cell whose room we want to fill
int filledRoomSize = numbaCell.getData(); //size of room we want afterward
Set<Point> locations = new HashSet<>(); //locations where white space is added to finish room
Point left = new Point(-1,0);
public ArrayList<Board> getCases(
Board board, PuzzleElement puzzleElement) { // throws IllegalStateException {
ArrayList<Board> cases = new ArrayList<>(); // makes array list of cases
NurikabeBoard nuriBoard = (NurikabeBoard) board.copy(); // nurikabe board to edit
NurikabeCell numbaCell =
nuriBoard.getCell(
((NurikabeCell) puzzleElement).getLocation().x,
((NurikabeCell) puzzleElement)
.getLocation()
.y); // number cell whose room we want to fill
int filledRoomSize = numbaCell.getData(); // size of room we want afterward
Set<Point> locations =
new HashSet<>(); // locations where white space is added to finish room
Point left = new Point(-1, 0);
Point right = new Point(1, 0);
Point bot = new Point(0, -1);
Point top = new Point(0, 1);
Expand All @@ -111,69 +137,101 @@ public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement) {//th
directions.add(right);
directions.add(top);
directions.add(bot);
Set<Point> checkedPoints = new HashSet<>(); //add all into checked points and continue at start of loop if inside
DisjointSets<NurikabeCell> regions = NurikabeUtilities.getNurikabeRegions(nuriBoard); //gathers regions
Set<NurikabeCell> disRow = regions.getSet(numbaCell); //set of white spaces
for (NurikabeCell d : disRow) { //loops through white spaces
if(cases.size() >= 6) { //no need to check this many cases
//throw new IllegalStateException("Too many cases");
continue; //crash/runtime protection
Set<Point> checkedPoints =
new HashSet<>(); // add all into checked points and continue at start of loop if
// inside
DisjointSets<NurikabeCell> regions =
NurikabeUtilities.getNurikabeRegions(nuriBoard); // gathers regions
Set<NurikabeCell> disRow = regions.getSet(numbaCell); // set of white spaces
for (NurikabeCell d : disRow) { // loops through white spaces
if (cases.size() >= 6) { // no need to check this many cases
// throw new IllegalStateException("Too many cases");
continue; // crash/runtime protection
}
for (Point direction : directions) {
if (cases.size() >= 6) { // no need to check this many cases
// throw new IllegalStateException("Too many cases");
continue; // crash/runtime protection
}
for(Point direction : directions) {
if(cases.size() >= 6) { //no need to check this many cases
//throw new IllegalStateException("Too many cases");
continue; //crash/runtime protection
}
if(!((nuriBoard.getWidth() > (d.getLocation().x + direction.x) && (nuriBoard.getHeight() > d.getLocation().y + direction.y) &&
(d.getLocation().x + direction.x >= 0) && (d.getLocation().y + direction.y >= 0)))) {
continue; //if next location check would be outside of grid then continue
}
NurikabeCell curr = nuriBoard.getCell(d.getLocation().x + direction.x, d.getLocation().y + direction.y);
if(checkedPoints.contains(curr.getLocation())) {
continue; //if we already checked whether or not making this tile white would complete the room then continue
}
checkedPoints.add(curr.getLocation()); //adds location to checkedPoints so we don't check it again and accidentally add
if (curr.getType() == NurikabeType.UNKNOWN) { //found adjacent space to region that is currently unknown
curr.setData(NurikabeType.WHITE.toValue()); //changes adjacent cell color to white
nuriBoard.addModifiedData(curr); // adds modified before check
regions = NurikabeUtilities.getNurikabeRegions(nuriBoard); //update regions
Set<NurikabeCell> disCreatedRow = regions.getSet(curr); //gets set of created row with new white cell added
if (disCreatedRow.size() == filledRoomSize) { //If adding white fills the room to exact size of number block and doesn't connect with another room
Point here = curr.getLocation(); //gets current location of new white tile that fills room
boolean alreadyIn = false; //sets whether or not the tile has already been added to false
for (Point p : locations) { //loops through locations of previously added tiles
if (p == here) { //if point is already in
alreadyIn = true; //change already in to true
break;
}
}
if (!alreadyIn) { //if point wasn't already in
Board casey = nuriBoard.copy(); //copy the current board with white tile changed
PuzzleElement datacasey = curr; //gets changed white tile as a puzzle element
datacasey.setData(NurikabeType.WHITE.toValue()); //ensure set to white, probably redundant
casey.addModifiedData(datacasey); //ensure confirmed white change
regions = NurikabeUtilities.getNurikabeRegions(nuriBoard); //update regions
cases.add(casey); //add this case to list of cases
locations.add(here); //add location of new white tile to list of locations so that we don't accidentally add it again later
}
if (!((nuriBoard.getWidth() > (d.getLocation().x + direction.x)
&& (nuriBoard.getHeight() > d.getLocation().y + direction.y)
&& (d.getLocation().x + direction.x >= 0)
&& (d.getLocation().y + direction.y >= 0)))) {
continue; // if next location check would be outside of grid then continue
}
NurikabeCell curr =
nuriBoard.getCell(
d.getLocation().x + direction.x, d.getLocation().y + direction.y);
if (checkedPoints.contains(curr.getLocation())) {
continue; // if we already checked whether or not making this tile white would
// complete the room then continue
}
checkedPoints.add(
curr.getLocation()); // adds location to checkedPoints so we don't check
// it again and accidentally add
if (curr.getType()
== NurikabeType
.UNKNOWN) { // found adjacent space to region that is currently
// unknown
curr.setData(
NurikabeType.WHITE.toValue()); // changes adjacent cell color to white
nuriBoard.addModifiedData(curr); // adds modified before check
regions = NurikabeUtilities.getNurikabeRegions(nuriBoard); // update regions
Set<NurikabeCell> disCreatedRow =
regions.getSet(
curr); // gets set of created row with new white cell added
if (disCreatedRow.size()
== filledRoomSize) { // If adding white fills the room to exact size of
// number block and doesn't connect with another
// room
Point here = curr.getLocation(); // gets current location of new white tile
// that fills room
boolean alreadyIn =
false; // sets whether or not the tile has already been added to
// false
for (Point p :
locations) { // loops through locations of previously added tiles
if (p == here) { // if point is already in
alreadyIn = true; // change already in to true
break;
}
curr.setData(NurikabeType.UNKNOWN.toValue()); //set cell type back to unknown
nuriBoard.addModifiedData(curr); // confirms change back to unknown
regions = NurikabeUtilities.getNurikabeRegions(nuriBoard); //updates regions
}
if (!alreadyIn) { // if point wasn't already in
Board casey =
nuriBoard.copy(); // copy the current board with white tile
// changed
PuzzleElement datacasey =
curr; // gets changed white tile as a puzzle element
datacasey.setData(
NurikabeType.WHITE
.toValue()); // ensure set to white, probably redundant
casey.addModifiedData(datacasey); // ensure confirmed white change
regions =
NurikabeUtilities.getNurikabeRegions(
nuriBoard); // update regions
cases.add(casey); // add this case to list of cases
locations.add(
here); // add location of new white tile to list of locations so
// that we don't accidentally add it again later
}
}
curr.setData(NurikabeType.UNKNOWN.toValue()); // set cell type back to unknown
nuriBoard.addModifiedData(curr); // confirms change back to unknown
regions = NurikabeUtilities.getNurikabeRegions(nuriBoard); // updates regions
}
}
}
return cases;
}

/**
* Checks whether the child node logically follows from the parent node
* at the specific puzzleElement index using this rule
* 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 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
* @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) {
Expand Down
Loading