-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No cell for number no number for cell
Unfinished but pushing
- Loading branch information
Showing
2 changed files
with
81 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/edu/rpi/legup/puzzle/sudoku/rules/NoNumberForCellContradictionRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |