Skip to content

Commit

Permalink
Finished DuplicateRowsOrColumnsContradictionRule for rows but not col…
Browse files Browse the repository at this point in the history
…umns, added method to get all types of cells in row.
  • Loading branch information
zacharybonagura committed Mar 26, 2024
1 parent bafc113 commit 5972432
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 57 deletions.
11 changes: 8 additions & 3 deletions puzzles files/binary/6x6 easy/876868768
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
<cell value="0" x="1" y="3"/>
<cell value="0" x="2" y="3"/>
<cell value="1" x="5" y="3"/>
<cell value="0" x="0" y="1"/>
<cell value="0" x="1" y="1"/>
<cell value="1" x="3" y="1"/>
<cell value="1" x="1" y="0"/>
<cell value="0" x="4" y="0"/>
<cell value="0" x="5" y="0"/>
<cell value="0" x="0" y="1"/>
<cell value="0" x="1" y="1"/>
<cell value="1" x="3" y="1"/>
<cell value="0" x="2" y="1"/>
<cell value="1" x="4" y="1"/>
<cell value="1" x="5" y="1"/>
<cell value="0" x="0" y="2"/>
<cell value="0" x="1" y="2"/>
<cell value="1" x="3" y="2"/>
<cell value="0" x="2" y="2"/>
<cell value="1" x="4" y="2"/>
</cells>
</board>
</puzzle>
Expand Down
11 changes: 11 additions & 0 deletions puzzles files/binary/6x6 easy/blank
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Legup version="2.0.0">
<puzzle name="Binary">
<board height="6" width="6">
<cells>

</cells>
</board>
</puzzle>
<solved isSolved="false" lastSaved="--"/>
</Legup>
16 changes: 13 additions & 3 deletions src/main/java/edu/rpi/legup/puzzle/binary/BinaryBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,11 +27,20 @@ public BinaryCell getCell(int x, int y) {
}
return (BinaryCell) super.getCell(x, y);
}

public Set<BinaryCell> getRow(int rowNum) {
public Set<BinaryCell> getRowCells(int rowNum) {
Set<BinaryCell> 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<BinaryType> getRowTypes(int rowNum) {
ArrayList<BinaryType> row = new ArrayList<BinaryType>();
for (int i = 0; i < size; i++) {
BinaryCell cell = getCell(i, rowNum);
row.add(cell.getType());
}
return row;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem
int boardDim = initialBoard.getWidth();
int elementRow = elementIndex / boardDim;
int elementCol = elementIndex % boardDim;
Set<BinaryCell> curRow = initialBoard.getRow(elementRow);
Set<BinaryCell> curCol = initialBoard.getCol(elementCol);

int numColZeros = 0;
int numColOnes = 0;
Expand All @@ -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 ++;
}
}
Expand All @@ -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 ++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<BinaryCell> row = binaryBoard.getRow(cell.getLocation().y);
BinaryCell[] rowArray = row.toArray(new BinaryCell[0]);

ArrayList<BinaryType> 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<BinaryCell> 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<BinaryCell> 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<BinaryType> currRow = binaryBoard.getRowTypes(i);
if (currRow.equals(row))
return null;
}
if (i != x) {
Set<BinaryCell> 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<BinaryCell> 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<BinaryCell> 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<BinaryCell> 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;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
BinaryBoard binaryBoard = (BinaryBoard) board;

BinaryCell cell = (BinaryCell) binaryBoard.getPuzzleElement(puzzleElement);
Set<BinaryCell> row = binaryBoard.getRow(cell.getLocation().y);
Set<BinaryCell> row = binaryBoard.getRowCells(cell.getLocation().y);

int size = row.size();
int rowNumZeros = 0;
Expand Down

0 comments on commit 5972432

Please sign in to comment.