Skip to content

Commit

Permalink
PossibleCellsForNumber Region Based Implementation
Browse files Browse the repository at this point in the history
Updated PossibleCellsForNumber to display number values when case board is clicked and generate proper case boards based off number chosen and local region.
  • Loading branch information
EggyMath committed Feb 28, 2024
1 parent 104f446 commit d5280c4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 60 deletions.
17 changes: 17 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/sudoku/ModelSudokuBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.rpi.legup.puzzle.sudoku;

public class ModelSudokuBoard {
public int getModelRegionNumbers(int index) {
int columnMod = index % 3 + 1;
int rowMod = ((index / 9) % 3) * 3;
return columnMod + rowMod;
}

public int getModelRowNumbers(int index) {
return index % 9 + 1;
}

public int getModelColumnNumbers(int index) {
return index / 9 + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.puzzle.sudoku.rules.PossibleCellsForNumberCaseRule;
import edu.rpi.legup.puzzle.sudoku.rules.PossibleCellsForNumberRegionCaseRule;

import java.awt.event.MouseEvent;
import java.util.HashSet;
Expand All @@ -16,7 +16,7 @@ public class PossibleNumberCaseBoard extends CaseBoard {
private Set<Integer> pickableCols;


public PossibleNumberCaseBoard(SudokuBoard baseBoard, PossibleCellsForNumberCaseRule caseRule, SudokuCell cell) {
public PossibleNumberCaseBoard(SudokuBoard baseBoard, PossibleCellsForNumberRegionCaseRule caseRule, SudokuCell cell) {
super(baseBoard, caseRule);
this.cell = cell;
this.pickableRegions = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
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.PossibleNumberCaseBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuCell;
import edu.rpi.legup.puzzle.sudoku.*;

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

public class PossibleCellsForNumberCaseRule extends CaseRule {
public class PossibleCellsForNumberRegionCaseRule extends CaseRule {

public PossibleCellsForNumberCaseRule() {
//Board math for translating indexes to numbers
private ModelSudokuBoard model = new ModelSudokuBoard();

//Old board for caseBoard reference
private SudokuBoard lagBoard;
public PossibleCellsForNumberRegionCaseRule() {
super("SUDO-CASE-0002", "Possible Cells for Number",
"An empty cell has a limited set of possible numbers that can fill it.",
"edu/rpi/legup/images/sudoku/possible_cells_number.png");
Expand Down Expand Up @@ -49,12 +50,12 @@ 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();
lagBoard = (SudokuBoard) sudokuBoard.copy();
CaseBoard caseBoard = new CaseBoard(sudokuBoard, this);
for (PuzzleElement puzzleElement : sudokuBoard.getPuzzleElements()) {
puzzleElement.setData(model.getModelRegionNumbers(puzzleElement.getIndex()));
caseBoard.addPickableElement(puzzleElement);
}
return caseBoard;
}
Expand Down Expand Up @@ -82,50 +83,19 @@ public ArrayList<Board> getCases(Board board, PuzzleElement puzzleElement) {
*/
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;

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));
SudokuBoard sudokuBoard = lagBoard;
SudokuCell sourceCell = (SudokuCell) puzzleElement;

boolean repeat = false;
for (SudokuCell bc : blockableCells) {
if (bc.getData() == value) {
repeat = true;
break;
}
}
if (!repeat) {
caseCells.add(c);
}
Set<SudokuCell> group = sudokuBoard.getRegion(sourceCell.getGroupIndex());
for (SudokuCell cell : group){
if(cell.getData() == 0){
Board newCase = sudokuBoard.copy();
PuzzleElement element = newCase.getPuzzleElement(cell);
element.setData(model.getModelRegionNumbers(sourceCell.getIndex()));
newCase.addModifiedData(element);
cases.add(newCase);
}
}

for (SudokuCell c : caseCells) {
Board newCase = sudokuBoard.copy();
PuzzleElement element = newCase.getPuzzleElement(c);
element.setData(value);
newCase.addModifiedData(element);
cases.add(newCase);
}

return cases;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
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.PossibleNumberCaseBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuBoard;
import edu.rpi.legup.puzzle.sudoku.SudokuCell;
import edu.rpi.legup.puzzle.sudoku.*;

import java.util.ArrayList;
import java.util.List;
Expand Down

0 comments on commit d5280c4

Please sign in to comment.