Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fixes #734

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SudokuCellFactory extends ElementFactory {
*
* @param node node that represents the puzzleElement
* @param board board to add the newly created cell
* @param board board to add the newly created cell
* @return newly created cell from the xml document Node
* @throws InvalidFileFormatException if file is invalid
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class LastCellForNumberDirectRule extends DirectRule {
public LastCellForNumberDirectRule() {
super("SUDO-BASC-0002", "Last Cell for Number",
super("SUDO-BASC-0001", "Last Cell for Number",
"This is the only cell open in its group for some number.",
"edu/rpi/legup/images/sudoku/forcedByElimination.png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class LastNumberForCellDirectRule extends DirectRule {

public LastNumberForCellDirectRule() {
super("SUDO-BASC-0003", "Last Number for Cell",
super("SUDO-BASC-0002", "Last Number for Cell",
"This is the only number left that can fit in the cell of a group.",
"edu/rpi/legup/images/sudoku/forcedByDeduction.png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import java.util.HashSet;
import java.util.Set;

public class NoSolutionContradictionRule extends ContradictionRule {
public class NoCellForNumberContradictionRule extends ContradictionRule {

public NoSolutionContradictionRule() {
super("SUDO-CONT-0001", "No Solution for Cell",
public NoCellForNumberContradictionRule() {
super("SUDO-CONT-0001", "No Cell for Number",
"Process of elimination yields no valid numbers for an empty cell.",
"edu/rpi/legup/images/sudoku/NoSolution.png");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElem

@Override
public CaseBoard getCaseBoard(Board board) {
SudokuBoard sudokuBoard = (SudokuBoard) board;
PossibleNumberCaseBoard caseBoard = new PossibleNumberCaseBoard(sudokuBoard, this, null);
for (int i = 0; i < sudokuBoard.getSize(); i++) {
caseBoard.addPickableRegion(i);
caseBoard.addPickableRow(i);
caseBoard.addPickableCol(i);
SudokuBoard sudokuBoard = (SudokuBoard) board.copy();
CaseBoard caseBoard = new CaseBoard(sudokuBoard, this);
for (PuzzleElement puzzleElement : sudokuBoard.getPuzzleElements()) {
if (((SudokuCell) puzzleElement).getData() == 0) {
caseBoard.addPickableElement(puzzleElement);
}
}
return caseBoard;
}

/**
* Gets the possible cases at a specific location based on this case rule
*
Expand Down Expand Up @@ -86,41 +85,9 @@ public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement, int v
List<SudokuCell> caseCells = new ArrayList<>();
SudokuCell cell = (SudokuCell) puzzleElement;

Set<SudokuCell> group;
if (groupType == GroupType.REGION) {
group = sudokuBoard.getRegion(cell.getGroupIndex());
}
else {
if (groupType == GroupType.ROW) {
group = sudokuBoard.getRow(cell.getLocation().y);
}
else {
group = sudokuBoard.getCol(cell.getLocation().x);
}
}

for (SudokuCell c : group) {
if (c.getData() == 0) {
Set<SudokuCell> blockableCells = sudokuBoard.getRegion(c.getGroupIndex());
blockableCells.addAll(sudokuBoard.getRow(c.getLocation().y));
blockableCells.addAll(sudokuBoard.getCol(c.getLocation().x));

boolean repeat = false;
for (SudokuCell bc : blockableCells) {
if (bc.getData() == value) {
repeat = true;
break;
}
}
if (!repeat) {
caseCells.add(c);
}
}
}

for (SudokuCell c : caseCells) {
for (int i = 0; i < 9; i++) {
Board newCase = sudokuBoard.copy();
PuzzleElement element = newCase.getPuzzleElement(c);
PuzzleElement element = newCase.getPuzzleElement(puzzleElement);
element.setData(value);
newCase.addModifiedData(element);
cases.add(newCase);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package edu.rpi.legup.puzzle.sudoku.rules;

import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.CaseRule;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.sudoku.GroupType;
import edu.rpi.legup.puzzle.sudoku.SudokuBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuCell;

import java.util.ArrayList;
import java.util.List;

public class PossibleNumberForRowCaseRule extends CaseRule {

public PossibleNumberForRowCaseRule() {
super("SUDO-CASE-0003", "Possible Numbers for Row",
"An empty cell has a limited set of possible numbers that can fill it.",
"edu/rpi/legup/images/sudoku/PossibleValues.png");
}

/**
* Checks whether the transition logically follows from the parent node using this rule
*
* @param transition transition to check
* @return null if the child node logically follow from the parent node, otherwise error message
*/
@Override
public String checkRuleRaw(TreeTransition transition) {
return null;
}

/**
* Checks whether the child node logically follows from the parent node
* at the specific puzzleElement index using this rule
*
* @param transition transition to check
* @param puzzleElement equivalent puzzleElement
* @return null if the child node logically follow from the parent node at the specified puzzleElement,
* otherwise error message
*/
@Override
public String checkRuleRawAt(TreeTransition transition, PuzzleElement puzzleElement) {
return null;
}

@Override
public CaseBoard getCaseBoard(Board board) {
SudokuBoard sudokuBoard = (SudokuBoard) board.copy();
CaseBoard caseBoard = new CaseBoard(sudokuBoard, this);
for (PuzzleElement puzzleElement : sudokuBoard.getPuzzleElements()) {
if (((SudokuCell) puzzleElement).getData() == 0) {
caseBoard.addPickableElement(puzzleElement);
}
}
return caseBoard;
}
/**
* Gets the possible cases at a specific location based on this case rule
*
* @param board the current board state
* @param puzzleElement equivalent puzzleElement
* @return a list of elements the specified could be
*/
@Override
public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement) {
return getCases(board, puzzleElement, 1, GroupType.REGION);
}

/**
* Gets the possible cases at a specific location based on this case rule
*
* @param board the current board state
* @param puzzleElement equivalent puzzleElement
* @param value value that the rule will be applied from
* @param groupType group type
* @return a list of elements the specified could be
*/
public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement, int value, GroupType groupType) {
ArrayList<Board> cases = new ArrayList<>();
SudokuBoard sudokuBoard = (SudokuBoard) board;
List<SudokuCell> caseCells = new ArrayList<>();
SudokuCell cell = (SudokuCell) puzzleElement;
//not sure what we would do here!
for (int i = 0; i < 9; i++) {
Board newCase = sudokuBoard.copy();
PuzzleElement element = newCase.getPuzzleElement(puzzleElement);
element.setData(value);
newCase.addModifiedData(element);
cases.add(newCase);
}

return cases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,27 @@ public String checkContradictionAt(Board board, PuzzleElement puzzleElement) {
Set<SudokuCell> row = sudokuBoard.getRow(cell.getLocation().y);
Set<SudokuCell> col = sudokuBoard.getCol(cell.getLocation().x);

Set<Integer> regionDup = new HashSet<>();
Set<Integer> rowDup = new HashSet<>();
Set<Integer> colDup = new HashSet<>();

Set<Integer> duplicates = new HashSet<>();
for (SudokuCell c : region) {
if (regionDup.contains(c.getData())) {
if (duplicates.contains(c.getData())) {
return null;
}
regionDup.add(c.getData());
duplicates.add(c.getData());
}
duplicates.clear();

for (SudokuCell c : row) {
if (rowDup.contains(c.getData())) {
if (duplicates.contains(c.getData())) {
return null;
}
rowDup.add(c.getData());
duplicates.add(c.getData());
}

duplicates.clear();
for (SudokuCell c : col) {
if (colDup.contains(c.getData())) {
if (duplicates.contains(c.getData())) {
return null;
}
colDup.add(c.getData());
duplicates.add(c.getData());
}

return super.getNoContradictionMessage();
Expand Down
Loading