Skip to content

Commit

Permalink
Found error in ThreeAdjacentContradictionRule, missing valid contradi…
Browse files Browse the repository at this point in the history
…ctions in OneTileGap and SurroundPair Direct Rules
  • Loading branch information
zacharybonagura committed Mar 29, 2024
1 parent a38365e commit 27f0d91
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,21 @@ public CompleteRowColumnDirectRule() {
*/
@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
BinaryBoard initialBoard = (BinaryBoard) transition.getParents().get(0).getBoard();
BinaryBoard finalBoard = (BinaryBoard) transition.getBoard();
int elementIndex = puzzleElement.getIndex();
int boardDim = initialBoard.getWidth();
int elementRow = elementIndex / boardDim;
int elementCol = elementIndex % boardDim;

int numColZeros = 0;
int numColOnes = 0;

for (int i = 0; i < boardDim; i++) {
BinaryCell cell = initialBoard.getCell(i, elementCol);
if(cell.getData() == 1){
numColOnes ++;
}
else if(cell.getData() == 0) {
numColZeros ++;
}
BinaryBoard board = (BinaryBoard) transition.getBoard();
BinaryBoard origBoard = (BinaryBoard) transition.getParents().get(0).getBoard();
ContradictionRule contraRule = new UnbalancedRowOrColumnContradictionRule();

BinaryCell cell = (BinaryCell) board.getPuzzleElement(puzzleElement);
if (cell.getType() == BinaryType.UNKNOWN) {
return "Only ONE or ZERO cells are allowed for this rule!";
}
int numRowZeros = 0;
int numRowOnes = 0;

for (int i = 0; i < boardDim; i++) {
BinaryCell cell = initialBoard.getCell(elementRow, i);
if(cell.getData() == 1) {
numRowOnes ++;
}
else if(cell.getData() == 0) {
numRowZeros ++;
if(cell.getType() != BinaryType.UNKNOWN){
if (contraRule.checkContradictionAt(origBoard, puzzleElement) != null) {
return "Balanced row or column found";
}
}
if (numColOnes + numColZeros != boardDim) {
return super.getInvalidUseOfRuleMessage() + ": The column for the specificed element is not complete";
}
if (numRowOnes + numRowZeros != boardDim) {
return super.getInvalidUseOfRuleMessage() + ": The row for the specified element is not complete";

}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import edu.rpi.legup.puzzle.binary.BinaryCell;
import edu.rpi.legup.puzzle.binary.BinaryType;

import java.util.Set;

public class OneTileGapDirectRule extends DirectRule {
private final String INVALID_USE_MESSAGE = "Number at cell is incorrect";
public OneTileGapDirectRule() {
Expand All @@ -21,22 +23,35 @@ public OneTileGapDirectRule() {

@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
BinaryBoard board = (BinaryBoard) transition.getBoard();
BinaryBoard destBoard = (BinaryBoard) transition.getBoard();
BinaryBoard origBoard = (BinaryBoard) transition.getParents().get(0).getBoard();
ContradictionRule contraRule = new ThreeAdjacentContradictionRule();

Set<PuzzleElement> changedCells = destBoard.getModifiedData();
// for (PuzzleElement p : changedCells) {
// BinaryCell c = (BinaryCell) destBoard.getPuzzleElement(p);
// if (c.getData() == 0) {
// c.setData(1);
// int cX = c.getLocation().x;
// int cY = c.getLocation().y;
// if ((destBoard.getCell(cX-1, cY).getType() == BinaryType.ONE && destBoard.getCell(cX+1, cY).getType() == BinaryType.ONE) ||
// (destBoard.getCell(cX, cY-1).getType() == BinaryType.ONE && destBoard.getCell(cX, cY+1).getType() == BinaryType.ONE)) {
// return ""
// }
//
// }
// else if (c.getData() == 1)
// c.setData(0);
// else if (c.getData() == 2)
// return "Only ONE or ZERO cells are allowed for this rule!";
// if (c.getLocation().x-1)
// }
//
// if (c.getType() )
// if (contraRule.checkContradictionAt(destBoard, puzzleElement) != null) {
// return "Grouping of Three Ones or Zeros found";
// }

BinaryCell cell = (BinaryCell) board.getPuzzleElement(puzzleElement);
if (cell.getType() == BinaryType.UNKNOWN) {
return "Only ONE or ZERO cells are allowed for this rule!";
}

if(cell.getType() != BinaryType.UNKNOWN){
if (contraRule.checkContradictionAt(origBoard, puzzleElement) == null) {
return "Grouping of Three Ones or Zeros found";
}

}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import edu.rpi.legup.puzzle.binary.BinaryCell;
import edu.rpi.legup.puzzle.binary.BinaryType;

import java.util.ArrayList;
import java.util.Set;

public class SurroundPairDirectRule extends DirectRule {

public SurroundPairDirectRule() {
Expand All @@ -21,22 +24,24 @@ public SurroundPairDirectRule() {

@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
BinaryBoard board = (BinaryBoard) transition.getBoard();
BinaryBoard destBoard = (BinaryBoard) transition.getBoard();
BinaryBoard origBoard = (BinaryBoard) transition.getParents().get(0).getBoard();
ContradictionRule contraRule = new ThreeAdjacentContradictionRule();


BinaryCell cell = (BinaryCell) board.getPuzzleElement(puzzleElement);
if (cell.getType() == BinaryType.UNKNOWN) {
return "Only ONE or ZERO cells are allowed for this rule!";
}

if(cell.getType() != BinaryType.UNKNOWN){
if (contraRule.checkContradictionAt(origBoard, puzzleElement) == null) {
return "Grouping of Three Ones or Zeros found";
}

Set<PuzzleElement> changedCells = destBoard.getModifiedData();
// for (PuzzleElement p : changedCells) {
// BinaryCell c = (BinaryCell) destBoard.getPuzzleElement(p);
// if (c.getData() == 0)
// c.setData(1);
// else if (c.getData() == 1)
// c.setData(0);
// else if (c.getData() == 2)
// return "Only ONE or ZERO cells are allowed for this rule!";
// }
if (contraRule.checkContradictionAt(destBoard, puzzleElement) == null) {
return "Grouping of Three Ones or Zeros found";
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
int cellY = cell.getLocation().y;

if(cell.getType() == BinaryType.ONE || cell.getType() == BinaryType.ZERO) {
for (int x = cell.getLocation().x - 2; x >= 0 && x < cell.getLocation().x && x < width - 2; x++){
for (int x = cell.getLocation().x - 2; x < cell.getLocation().x && x < width - 2; x++){
if (x < 0)
continue;
if(binaryBoard.getCell(x, cellY).getType() == binaryBoard.getCell(x + 1, cellY).getType() &&
binaryBoard.getCell(x + 1, cellY).getType() == binaryBoard.getCell(x + 2, cellY).getType()) {
System.out.println("Contradiction Found because of " + binaryBoard.getCell(x, cellY).getLocation().x + " " + binaryBoard.getCell(x, cellY).getLocation().y + " " + + binaryBoard.getCell(x+1, cellY).getLocation().x + " " + binaryBoard.getCell(x+1, cellY).getLocation().y + " " + + binaryBoard.getCell(x+2, cellY).getLocation().x + " " + binaryBoard.getCell(x+2, cellY).getLocation().y);
return null;
}
}

for (int y = cell.getLocation().y - 2; y >= 0 && y < cell.getLocation().y && y < height - 2; y++){
for (int y = cell.getLocation().y - 2; y < cell.getLocation().y && y < height - 2; y++){
if (y < 0)
continue;
if(binaryBoard.getCell(cellX, y).getType() == binaryBoard.getCell(cellX, y + 1).getType() &&
binaryBoard.getCell(cellX, y + 1).getType() == binaryBoard.getCell(cellX, y + 2).getType()) {
System.out.println("Contradiction Found because of " + binaryBoard.getCell(cellX, y+2).getLocation().x + " " + binaryBoard.getCell(cellX, y).getLocation().y + " " + + binaryBoard.getCell(cellX, y+1).getLocation().x + " " + binaryBoard.getCell(cellX, y+1).getLocation().y + " " + + binaryBoard.getCell(cellX, y+2).getLocation().x + " " + binaryBoard.getCell(cellX, y+2).getLocation().y);
return null;
}
}
Expand Down

0 comments on commit 27f0d91

Please sign in to comment.