Skip to content

Commit

Permalink
Updated the implementation of UnreachableSumContradictionRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ADEPLI committed Dec 6, 2024
1 parent d7022ba commit e8fe776
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.awt.*;
import java.util.List;

public class FinishWithEmptyDirectRule extends DirectRule {
public FinishWithEmptyDirectRule() {
public class RequiredEmptyDirectRule extends DirectRule {
public RequiredEmptyDirectRule() {
super(
"KAKU-BASC-0002",
"Finish with Empty",
Expand Down Expand Up @@ -57,6 +57,7 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
* @return if the cell is forced to be at its position on the board
*/
private boolean isForced(KakurasuBoard board, KakurasuCell cell) {
// TODO: Fix this so it doesn't only work if all are empty
Point loc = cell.getLocation();
List<KakurasuCell> filledRow = board.getRowCol(loc.y, KakurasuType.FILLED, true);
List<KakurasuCell> filledCol = board.getRowCol(loc.x, KakurasuType.FILLED, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.kakurasu.KakurasuBoard;
import edu.rpi.legup.puzzle.kakurasu.KakurasuCell;
import edu.rpi.legup.puzzle.kakurasu.KakurasuClue;
import edu.rpi.legup.puzzle.kakurasu.KakurasuType;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class FinishWithFilledDirectRule extends DirectRule {
public FinishWithFilledDirectRule() {
public class RequiredFilledDirectRule extends DirectRule {
public RequiredFilledDirectRule() {
super(
"KAKU-BASC-0001",
"Finish with Filled",
"Required Filled",
"The only way to satisfy the clue in a row or column are these filled tiles.",
"edu/rpi/legup/images/kakurasu/temp.png");
}
Expand Down Expand Up @@ -59,6 +57,7 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
* @return if the cell is forced to be at its position on the board
*/
private boolean isForced(KakurasuBoard board, KakurasuCell cell) {
// TODO: Fix this so it doesn't only work if all are filled
Point loc = cell.getLocation();
List<KakurasuCell> filledRow = board.getRowCol(loc.y, KakurasuType.FILLED, true);
List<KakurasuCell> unknownRow = board.getRowCol(loc.y, KakurasuType.UNKNOWN, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import edu.rpi.legup.puzzle.kakurasu.KakurasuType;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class UnreachableSumContradictionRule extends ContradictionRule {
Expand All @@ -33,8 +34,6 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
KakurasuBoard kakurasuBoard = (KakurasuBoard) board;
KakurasuCell cell = (KakurasuCell) puzzleElement;

// TODO: Finish this rule

Point loc = cell.getLocation();
List<KakurasuCell> filledRow = kakurasuBoard.getRowCol(loc.y, KakurasuType.FILLED, true);
List<KakurasuCell> unknownRow = kakurasuBoard.getRowCol(loc.y, KakurasuType.UNKNOWN, true);
Expand All @@ -50,10 +49,51 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
colValueRemaining -= kc.getLocation().y + 1;
}

if (true) {
// If the value for either the row or col is already exceeded, this is the wrong rule to call.
if(rowValueRemaining < 0 || colValueRemaining < 0) return super.getNoContradictionMessage();

// If either value is already 0, then it is already possible to fulfill
// If it isn't 0, then it's possible for the remaining values to not be able to fulfill it
boolean rowPossible = (rowValueRemaining==0), colPossible = (colValueRemaining==0);

int rowTotal = 0, colTotal = 0;
// No need to sort the values as the KakurasuCells are given in increasing index order
if(!rowPossible) {
ArrayList<Integer> rowValues = new ArrayList<>();
for(KakurasuCell kc : unknownRow) {
rowValues.add(kc.getLocation().x + 1);
rowTotal += kc.getLocation().x + 1;
}
// If the remaining unknown cells' values is less than the remaining value,
// this requires the usage of a different contradiction rule, not this one.
if(rowTotal < rowValueRemaining) return super.getNoContradictionMessage();
rowPossible = isReachable(rowValueRemaining, 0, rowValues);
}
if(!colPossible) {
ArrayList<Integer> colValues = new ArrayList<>();
for(KakurasuCell kc : unknownCol) {
colValues.add(kc.getLocation().y + 1);
colTotal += kc.getLocation().y + 1;
}
// If the remaining unknown cells' values is less than the remaining value,
// this requires the usage of a different contradiction rule, not this one.
if(colTotal < colValueRemaining) return super.getNoContradictionMessage();
colPossible = isReachable(colValueRemaining, 0, colValues);
}

if (!rowPossible || !colPossible) {
return null;
} else {
return super.getNoContradictionMessage();
}
}

// Helper function that checks if the target clue is reachable given a list of KakurasuCells
// This function only works if the list of values are given in increasing index order (which it currently is)
private boolean isReachable(int target, int currentIndex, ArrayList<Integer> values) {
if(target == 0) return true;
if(target < 0 || currentIndex >= values.size()) return false;
return (isReachable(target, currentIndex+1, values) ||
isReachable(target - values.get(currentIndex), currentIndex+1, values));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
KAKU-BASC-0001 : FinishWithFilledDirectRule
KAKU-BASC-0002 : FinishWithEmptyDirectRule
KAKU-BASC-0003 : RequiredFilledDirectRule

KAKU-BASC-0001 : RequiredFilledDirectRule
KAKU-BASC-0002 : RequiredEmptyDirectRule

KAKU-CONT-0001 : ExceededSumContradictionRule
KAKU-CONT-0002 : IncompleteSumContradictionRule
KAKU-CONT-0003 : UnreachableSumContradictionRule

KAKU-CASE-0001 : A
KAKU-CASE-0001 : FilledOrEmptyCaseRule

0 comments on commit e8fe776

Please sign in to comment.