Skip to content

Commit

Permalink
Region Based Changes (#559)
Browse files Browse the repository at this point in the history
Co-authored-by: Hanson Gu <[email protected]>
  • Loading branch information
19690ao and hansongu123 authored Sep 29, 2023
1 parent 12eb296 commit 50ba53f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
63 changes: 63 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/nurikabe/NurikabeUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,67 @@ public static HashMap<NurikabeCell, Integer> getWhiteRegionMap(NurikabeBoard boa
}
return whiteRegionMap;
}

/**
* Gets all the non-black cells connected to the given cell
*
* @param board nurikabe board
* @param center nurikabe cell
* @return a set of all white/numbered cells in the region
*/
public static Set<NurikabeCell> getSurroundedRegionOf(NurikabeBoard board, NurikabeCell center) {
int width = board.getWidth();
int height = board.getHeight();

// Mark all the vertices as not visited(By default
// set as false)
Set<NurikabeCell> visited = new HashSet<>();

// Create a queue for BFS
LinkedList<NurikabeCell> queue = new LinkedList<>();

// Mark the current node as visited and enqueue it
visited.add(center);
queue.add(center);

// Set of cells in the current region
Set<NurikabeCell> connected = new HashSet<>();

while (queue.size() != 0) {
// Dequeue a vertex from queue and print it
// s is the source node in the graph
NurikabeCell s = queue.poll();
System.out.print(s + " ");

// Make a set of all adjacent squares
Set<NurikabeCell> adj = new HashSet<>();

Point loc = s.getLocation();
// First check if the side is on the board
if (loc.x >= 1) {
adj.add(board.getCell(loc.x - 1, loc.y));
}
if (loc.x < width - 1) {
adj.add(board.getCell(loc.x + 1, loc.y));
}
if (loc.y >= 1) {
adj.add(board.getCell(loc.x, loc.y - 1));
}
if (loc.y < height - 1) {
adj.add(board.getCell(loc.x, loc.y + 1));
}
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
for (NurikabeCell n : adj) {
if (!visited.contains(n) && n.getType() != NurikabeType.BLACK) {
connected.add(n);
visited.add(n);
queue.add(n);
}
}
}

return connected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,18 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
return super.getInvalidUseOfRuleMessage() + ": " + this.INVALID_USE_MESSAGE;
}

// If the transition creates a room of white cells with no number, a contradiction occurs.
DisjointSets<NurikabeCell> anotherRegion = NurikabeUtilities.getPossibleWhiteRegions(nurikabeBoard);
List<Set<NurikabeCell>> allsets = anotherRegion.getAllSets();
for (Set<NurikabeCell> s : allsets) {
boolean numberExists = false;
for (NurikabeCell c : s) {
if (c.getType() == NurikabeType.NUMBER) {
numberExists = true;
}
}
if (!numberExists) {
return null;
Set<NurikabeCell> region = NurikabeUtilities.getSurroundedRegionOf(nurikabeBoard, cell);

boolean numberExists = false;
for (NurikabeCell c : region) {
if (c.getType() == NurikabeType.NUMBER) {
numberExists = true;
break;
}
}
if (!numberExists) {
return null;
}
return super.getNoContradictionMessage() + ": " + this.NO_CONTRADICTION_MESSAGE;
}

Expand Down

0 comments on commit 50ba53f

Please sign in to comment.