From 5972432d0cffed8765a80774409db9fcc51eec38 Mon Sep 17 00:00:00 2001 From: Zachary Bonagura Date: Tue, 26 Mar 2024 17:47:41 -0400 Subject: [PATCH] Finished DuplicateRowsOrColumnsContradictionRule for rows but not columns, added method to get all types of cells in row. --- puzzles files/binary/6x6 easy/876868768 | 11 +- puzzles files/binary/6x6 easy/blank | 11 ++ .../rpi/legup/puzzle/binary/BinaryBoard.java | 16 ++- .../rules/CompleteRowColumnDirectRule.java | 8 +- ...plicateRowsOrColumnsContradictionRule.java | 104 ++++++++++-------- ...nbalancedRowOrColumnContradictionRule.java | 2 +- 6 files changed, 95 insertions(+), 57 deletions(-) create mode 100644 puzzles files/binary/6x6 easy/blank diff --git a/puzzles files/binary/6x6 easy/876868768 b/puzzles files/binary/6x6 easy/876868768 index f6282d4c7..d4a8f71d4 100644 --- a/puzzles files/binary/6x6 easy/876868768 +++ b/puzzles files/binary/6x6 easy/876868768 @@ -11,15 +11,20 @@ - - - + + + + + + + + diff --git a/puzzles files/binary/6x6 easy/blank b/puzzles files/binary/6x6 easy/blank new file mode 100644 index 000000000..006528e16 --- /dev/null +++ b/puzzles files/binary/6x6 easy/blank @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/edu/rpi/legup/puzzle/binary/BinaryBoard.java b/src/main/java/edu/rpi/legup/puzzle/binary/BinaryBoard.java index 332355808..8471028ef 100644 --- a/src/main/java/edu/rpi/legup/puzzle/binary/BinaryBoard.java +++ b/src/main/java/edu/rpi/legup/puzzle/binary/BinaryBoard.java @@ -4,6 +4,7 @@ import edu.rpi.legup.model.gameboard.PuzzleElement; import java.awt.*; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class BinaryBoard extends GridBoard { @@ -26,11 +27,20 @@ public BinaryCell getCell(int x, int y) { } return (BinaryCell) super.getCell(x, y); } - - public Set getRow(int rowNum) { + public Set getRowCells(int rowNum) { Set row = new HashSet<>(); for (int i = 0; i < size; i++) { - row.add(getCell(i, rowNum)); + BinaryCell cell = getCell(i, rowNum); + row.add(cell); + } + return row; + } + + public ArrayList getRowTypes(int rowNum) { + ArrayList row = new ArrayList(); + for (int i = 0; i < size; i++) { + BinaryCell cell = getCell(i, rowNum); + row.add(cell.getType()); } return row; } diff --git a/src/main/java/edu/rpi/legup/puzzle/binary/rules/CompleteRowColumnDirectRule.java b/src/main/java/edu/rpi/legup/puzzle/binary/rules/CompleteRowColumnDirectRule.java index fbb64a13c..147d1c2a1 100644 --- a/src/main/java/edu/rpi/legup/puzzle/binary/rules/CompleteRowColumnDirectRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/binary/rules/CompleteRowColumnDirectRule.java @@ -40,8 +40,6 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem int boardDim = initialBoard.getWidth(); int elementRow = elementIndex / boardDim; int elementCol = elementIndex % boardDim; - Set curRow = initialBoard.getRow(elementRow); - Set curCol = initialBoard.getCol(elementCol); int numColZeros = 0; int numColOnes = 0; @@ -51,7 +49,7 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem if(cell.getData() == 1){ numColOnes ++; } - else if(cell.getData() == 0){ + else if(cell.getData() == 0) { numColZeros ++; } } @@ -60,10 +58,10 @@ else if(cell.getData() == 0){ for (int i = 0; i < boardDim; i++) { BinaryCell cell = initialBoard.getCell(elementRow, i); - if(cell.getData() == 1){ + if(cell.getData() == 1) { numRowOnes ++; } - else if(cell.getData() == 0){ + else if(cell.getData() == 0) { numRowZeros ++; } } diff --git a/src/main/java/edu/rpi/legup/puzzle/binary/rules/DuplicateRowsOrColumnsContradictionRule.java b/src/main/java/edu/rpi/legup/puzzle/binary/rules/DuplicateRowsOrColumnsContradictionRule.java index 17dd4b631..295f48598 100644 --- a/src/main/java/edu/rpi/legup/puzzle/binary/rules/DuplicateRowsOrColumnsContradictionRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/binary/rules/DuplicateRowsOrColumnsContradictionRule.java @@ -7,6 +7,8 @@ import edu.rpi.legup.puzzle.binary.BinaryCell; import edu.rpi.legup.puzzle.binary.BinaryType; +import java.util.ArrayList; +import java.util.Iterator; import java.util.Set; public class DuplicateRowsOrColumnsContradictionRule extends ContradictionRule { private final String NO_CONTRADICTION_MESSAGE = "Does not contain a contradiction at this index"; @@ -22,57 +24,69 @@ public DuplicateRowsOrColumnsContradictionRule() { @Override public String checkContradictionAt(Board board, PuzzleElement puzzleElement) { BinaryBoard binaryBoard = (BinaryBoard) board; - boolean rowValid = false; - boolean colValid = false; BinaryCell cell = (BinaryCell) binaryBoard.getPuzzleElement(puzzleElement); - Set row = binaryBoard.getRow(cell.getLocation().y); - BinaryCell[] rowArray = row.toArray(new BinaryCell[0]); + + ArrayList row = binaryBoard.getRowTypes(cell.getLocation().y); + + System.out.println(row); int size = row.size(); - int y = cell.getLocation().y; - for (int i = 0; i < size; i++) { - if (rowValid) { - break; - } - if (i != y) { - Set currRow = binaryBoard.getRow(i); - BinaryCell[] currRowArray = currRow.toArray(new BinaryCell[0]); - for (int j = 0; j < size; j++) { - BinaryCell rowElement = rowArray[j]; - BinaryCell currRowElement = currRowArray[j]; - if (rowElement.getType() != currRowElement.getType()) { - rowValid = true; - } - } - } - } - if (!rowValid) { - return null; - } - Set col = binaryBoard.getCol(cell.getLocation().x); - BinaryCell[] colArray = col.toArray(new BinaryCell[0]); - size = col.size(); - int x = cell.getLocation().x; + + for (int i = 0; i < size; i++) { - if (colValid) { - break; + if (i > cell.getLocation().y) { + ArrayList currRow = binaryBoard.getRowTypes(i); + if (currRow.equals(row)) + return null; } - if (i != x) { - Set currCol = binaryBoard.getCol(i); - BinaryCell[] currColArray = currCol.toArray(new BinaryCell[0]); - for (int j = 0; j < size; j++) { - BinaryCell colElement = colArray[j]; - BinaryCell currColElement = currColArray[j]; - if (colElement.getType() != currColElement.getType()) { - colValid = true; - } - } - } - } - if (!colValid) { - return null; } +// BinaryCell[] rowArray = row.toArray(new BinaryCell[0]); +// +// boolean rowValid = false; +// int size = row.size(); +// int y = cell.getLocation().y; +// for (int i = 0; i < size; i++) { +// if (i != y) { +// Set currRow = binaryBoard.getRow(i); +// BinaryCell[] currRowArray = currRow.toArray(new BinaryCell[0]); +// for (int j = 0; j < size; j++) { +// BinaryCell rowElement = rowArray[j]; +// BinaryCell currRowElement = currRowArray[j]; +// System.out.println("Row: " + i + " Org x: " + rowElement.getLocation().x + " Curr x: " + currRowElement.getLocation().x); +//// if (rowElement.getType() != currRowElement.getType()) { +//// rowValid = true; +//// break; +//// } +// } +//// if (!rowValid) +//// return null; +// } +// } +// return null; +// Set col = binaryBoard.getCol(cell.getLocation().x); +// BinaryCell[] colArray = col.toArray(new BinaryCell[0]); +// size = col.size(); +// int x = cell.getLocation().x; +// for (int i = 0; i < size; i++) { +// if (colValid) { +// break; +// } +// if (i != x) { +// Set currCol = binaryBoard.getCol(i); +// BinaryCell[] currColArray = currCol.toArray(new BinaryCell[0]); +// for (int j = 0; j < size; j++) { +// BinaryCell colElement = colArray[j]; +// BinaryCell currColElement = currColArray[j]; +// if (colElement.getType() != currColElement.getType()) { +// colValid = true; +// } +// } +// } +// } +// if (!colValid) { +// return null; +// } + //System.out.println(cell.getLocation().x + " " + cell.getLocation().y); return super.getNoContradictionMessage() + ": " + this.NO_CONTRADICTION_MESSAGE; - } } diff --git a/src/main/java/edu/rpi/legup/puzzle/binary/rules/UnbalancedRowOrColumnContradictionRule.java b/src/main/java/edu/rpi/legup/puzzle/binary/rules/UnbalancedRowOrColumnContradictionRule.java index 09692c394..b58f9f02e 100644 --- a/src/main/java/edu/rpi/legup/puzzle/binary/rules/UnbalancedRowOrColumnContradictionRule.java +++ b/src/main/java/edu/rpi/legup/puzzle/binary/rules/UnbalancedRowOrColumnContradictionRule.java @@ -25,7 +25,7 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) { BinaryBoard binaryBoard = (BinaryBoard) board; BinaryCell cell = (BinaryCell) binaryBoard.getPuzzleElement(puzzleElement); - Set row = binaryBoard.getRow(cell.getLocation().y); + Set row = binaryBoard.getRowCells(cell.getLocation().y); int size = row.size(); int rowNumZeros = 0;