Skip to content

Commit

Permalink
No cell for number no number for cell
Browse files Browse the repository at this point in the history
Unfinished but pushing
  • Loading branch information
kchiu1 committed Mar 22, 2024
1 parent d888a77 commit 4685737
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.ContradictionRule;
import edu.rpi.legup.puzzle.sudoku.Sudoku;
import edu.rpi.legup.puzzle.sudoku.SudokuBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuCell;

Expand All @@ -12,8 +13,8 @@
public class NoCellForNumberContradictionRule extends ContradictionRule {

public NoCellForNumberContradictionRule() {
super("SUDO-CONT-0001", "No Cell for Number",
"Process of elimination yields no valid numbers for an empty cell.",
super("SUDO-CONT-0001", "No Cell for Number (Region)",
"Process of elimination yields no valid numbers for an empty cell in a region.",
"edu/rpi/legup/images/sudoku/NoSolution.png");
}

Expand All @@ -36,27 +37,35 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
int groupSize = sudokuBoard.getSize();

Set<SudokuCell> region = sudokuBoard.getRegion(cell.getGroupIndex());
Set<SudokuCell> row = sudokuBoard.getRow(cell.getLocation().y);
Set<SudokuCell> col = sudokuBoard.getCol(cell.getLocation().x);
Set<Integer> solution = new HashSet<>();
Set<Integer> numbersNotInRegion = new HashSet<>();
Set<Integer> numberNotFit = new HashSet<>();
Set<Integer> numberCanFit = new HashSet<>();

for (int i = 1; i <= groupSize; i++) {
solution.add(i);
numbersNotInRegion.add(i);
}

for (SudokuCell c : region) {
solution.remove(c.getData());
}
for (SudokuCell c : row) {
solution.remove(c.getData());
}
for (SudokuCell c : col) {
solution.remove(c.getData());
if(c.getData() != 0) {
numbersNotInRegion.remove(c.getData());
}
}
for (SudokuCell c : region) {

Set<SudokuCell> row = sudokuBoard.getRow(c.getLocation().y);
Set<SudokuCell> col = sudokuBoard.getCol(c.getLocation().x);
for(SudokuCell c1 : row) {
numberNotFit.add(c1.getData());
}
for(SudokuCell c2 : col) {
numberNotFit.add(c2.getData());
}

if (solution.isEmpty()) {
return null;
for(int i : numbersNotInRegion) {
numbersNotInRegion.remove(numberNotFit);
}
}


return super.getNoContradictionMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package edu.rpi.legup.puzzle.sudoku.rules;

import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.ContradictionRule;
import edu.rpi.legup.puzzle.sudoku.SudokuBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuCell;

import java.util.HashSet;
import java.util.Set;

public class NoNumberForCellContradictionRule extends ContradictionRule {

public NoNumberForCellContradictionRule() {
super("SUDO-CONT-0003", "No Number for Cell",
"Process of elimination yields no valid numbers for an empty cell.",
"edu/rpi/legup/images/sudoku/NoSolution.png");
}

/**
* Checks whether the transition has a contradiction at the specific puzzleElement index using this rule
*
* @param board board to check contradiction
* @param puzzleElement equivalent puzzleElement
* @return null if the transition contains a contradiction at the specified puzzleElement,
* otherwise error message
*/
@Override
public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
SudokuBoard sudokuBoard = (SudokuBoard) board;
SudokuCell cell = (SudokuCell) sudokuBoard.getPuzzleElement(puzzleElement);
if (cell.getData() != 0) {
return super.getNoContradictionMessage();
}

int groupSize = sudokuBoard.getSize();

Set<SudokuCell> region = sudokuBoard.getRegion(cell.getGroupIndex());
Set<SudokuCell> row = sudokuBoard.getRow(cell.getLocation().y);
Set<SudokuCell> col = sudokuBoard.getCol(cell.getLocation().x);
Set<Integer> solution = new HashSet<>();
for (int i = 1; i <= groupSize; i++) {
solution.add(i);
}

for (SudokuCell c : region) {
solution.remove(c.getData());
}

if (solution.isEmpty()) {
return null;
}

return super.getNoContradictionMessage();
}
}

0 comments on commit 4685737

Please sign in to comment.