fixed error checking bug. note, unexpected behavior occurs when incorrect values are entered and checkValues is true. needs more testing/additional fixes

This commit is contained in:
Josh Ashton
2024-03-25 14:06:03 -06:00
parent 600c215b7d
commit bf011ea75b
5 changed files with 58 additions and 66 deletions
+30 -30
View File
@@ -119,6 +119,34 @@ public class Board extends JPanel {
setForeground(s.getTheme().getPrimaryText()); setForeground(s.getTheme().getPrimaryText());
} }
/**
* Create the Board Panel with the appropriate cells.
*
* Every CellGUI object is created with the appropriate Cell object and
* Settings object. The CellGUI objects are then added to the Board Panel.
*
* Each CellGUI object is also given a MouseListener to handle user input,
* and a KeyListener to handle keyboard input if/when the cell is selected.
*/
private void createBoard() {
if(s.getAutoFillNotes()) grid = sc.getPossibleValues(grid);
gridGUI = new CellGUI[9][9];
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
// Create a new CellGUI object with the appropriate Cell object.
CellGUI cell = new CellGUI(grid[i][j], s);
// Add a MouseListener to the cell.
cell.addMouseListener(createMouseListener(cell));
// Add the CellGUI object to the Board Panel.
gridGUI[i][j] = cell;
add(gridGUI[i][j]);
}
}
}
/** /**
* Select the given cell and highlight all cells in the same row, column, * Select the given cell and highlight all cells in the same row, column,
* and box. * and box.
@@ -172,34 +200,6 @@ public class Board extends JPanel {
} }
} }
/**
* Create the Board Panel with the appropriate cells.
*
* Every CellGUI object is created with the appropriate Cell object and
* Settings object. The CellGUI objects are then added to the Board Panel.
*
* Each CellGUI object is also given a MouseListener to handle user input,
* and a KeyListener to handle keyboard input if/when the cell is selected.
*/
private void createBoard() {
if(s.getAutoFillNotes()) grid = sc.getPossibleValues(grid);
gridGUI = new CellGUI[9][9];
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
// Create a new CellGUI object with the appropriate Cell object.
CellGUI cell = new CellGUI(grid[i][j], s);
// Add a MouseListener to the cell.
cell.addMouseListener(createMouseListener(cell));
// Add the CellGUI object to the Board Panel.
gridGUI[i][j] = cell;
add(gridGUI[i][j]);
}
}
}
/** /**
* Create a MouseListener for CellGUI objects. * Create a MouseListener for CellGUI objects.
* *
@@ -248,11 +248,11 @@ public class Board extends JPanel {
public void mouseReleased(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}
@Override @Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e) {
if(selected == null) cell.select(); cell.select();
} }
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e) {
if(selected == null) cell.select(); cell.select();
} }
}; };
} }
+18 -30
View File
@@ -65,7 +65,7 @@ class CellGUI extends JPanel {
// Populate the cell with the appropriate value or notes. // Populate the cell with the appropriate value or notes.
if(cell.getValue() == 0) { if(cell.getValue() == 0) {
internalPanel.setLayout(noteLayout); internalPanel.setLayout(noteLayout);
generateNotes(s.getAutoFillNotes()); generateNotes(noteMode);
} else { } else {
internalPanel.setLayout(valueLayout); internalPanel.setLayout(valueLayout);
valueLabel.setText(Integer.toString(cell.getValue())); valueLabel.setText(Integer.toString(cell.getValue()));
@@ -86,12 +86,16 @@ class CellGUI extends JPanel {
/** /**
* Set the value of the underlying Cell object. * Set the value of the underlying Cell object.
* *
* This implementation is to be used when Settings.getAutoCheckValues
* is false. It will assume that every value the user enters is incorrect,
* and thus the underlying List of possible values is never cleared.
*
* @param value * @param value
*/ */
public void setValue(int value) { public void setValue(int value) {
if(!selected) return; if(!selected) return;
cell.setValue(value); cell.setValue(value, false);
// Update the GUI with the actual Cell value (unchanged if invalid) // Update the GUI with the actual Cell value (unchanged if invalid)
valueLabel.setText(Integer.toString(cell.getValue())); valueLabel.setText(Integer.toString(cell.getValue()));
refresh(); refresh();
@@ -100,18 +104,22 @@ class CellGUI extends JPanel {
/** /**
* Set the value of the underlying Cell object. * Set the value of the underlying Cell object.
* *
* This implementation allows for incorrect values to be highlighted. * This implementation is to be used when Settings.getAutoCheckValues
* is true. It allows for incorrect values to be highlighted. And the
* underlying List of possible values to be cleared if the entered
* value is correct.
* *
* @param value * @param value
*/ */
public void setValue(int value, int expected) { public void setValue(int value, int expected) {
if(!selected) return; if(!selected) return;
cell.setValue(value); cell.setValue(value, value == expected);
valueLabel.setText(Integer.toString(cell.getValue())); valueLabel.setText(Integer.toString(cell.getValue()));
if(value != expected) { if(value != expected) {
incorrect = true; incorrect = true;
valueLabel.setText(Integer.toString(value));
errorStyle(); errorStyle();
} else refresh(); } else refresh();
} }
@@ -122,7 +130,7 @@ class CellGUI extends JPanel {
public void removeValue() { public void removeValue() {
if(!selected) return; if(!selected) return;
cell.setValue(0); cell.setValue(0, true);
if(!cell.isInitValue()) if(!cell.isInitValue())
valueLabel.setText(""); valueLabel.setText("");
@@ -166,25 +174,21 @@ class CellGUI extends JPanel {
/** /**
* Toggle the mode of the cell between value and note, including the * Toggle the mode of the cell between value and note, including the
* layout of the cell. * layout and visual content of the cell.
*
* If the current mode is value, switch to note mode, and vice versa.
*/ */
public void setNoteMode() { public void setNoteMode() {
if(cell.isInitValue()) return; if(cell.isInitValue()) return;
internalPanel.removeAll();
if(noteMode) { if(noteMode) {
internalPanel.removeAll();
internalPanel.setLayout(valueLayout); internalPanel.setLayout(valueLayout);
internalPanel.add(valueLabel); internalPanel.add(valueLabel);
} else { } else {
internalPanel.setLayout(noteLayout); internalPanel.setLayout(noteLayout);
noteMode = true;
generateNotes(true); generateNotes(true);
} }
refresh(); refresh();
noteMode = !noteMode; noteMode = !noteMode;
} }
@@ -196,11 +200,8 @@ class CellGUI extends JPanel {
if(incorrect) { if(incorrect) {
errorStyle(); errorStyle();
return; return;
} } else if(!selected)
if(!selected)
defaultStyle(); defaultStyle();
else highlightedStyle(); else highlightedStyle();
} }
@@ -256,20 +257,10 @@ class CellGUI extends JPanel {
* @see Theme.java * @see Theme.java
*/ */
private void errorStyle() { private void errorStyle() {
setBackground(theme.getErrorBackground());
setForeground(theme.getErrorText());
internalPanel.setBackground(theme.getErrorBackground());
internalPanel.setForeground(theme.getErrorText());
valueLabel.setForeground(theme.getErrorText()); valueLabel.setForeground(theme.getErrorText());
internalPanel.setBackground(theme.getErrorBackground());
setBorder(new LineBorder(theme.getErrorBorder(), 2)); setBorder(new LineBorder(theme.getErrorBorder(), 2));
for(int i = 0; i < 9; i++) {
if(notesLabels[i] != null) {
notesLabels[i].setBackground(theme.getErrorBackground());
notesLabels[i].setForeground(theme.getErrorText());
}
}
refresh(); refresh();
} }
@@ -295,10 +286,7 @@ class CellGUI extends JPanel {
int[] possibleValues = cell.getPossibleValues(); int[] possibleValues = cell.getPossibleValues();
// Prepare the internalPanel for noteMode. // Prepare the internalPanel for noteMode.
if(autoFill) { if(autoFill) internalPanel.removeAll();
internalPanel.removeAll();
noteMode = true;
}
// Handle cases where there are no possible values stored. // Handle cases where there are no possible values stored.
if(possibleValues.length == 0) { if(possibleValues.length == 0) {
+6 -2
View File
@@ -70,12 +70,16 @@ public class Cell {
* *
* The value must be between 1 and 9, inclusive. * The value must be between 1 and 9, inclusive.
* *
* Additionally if the value is incorrect, then do not clear the
* possible values.
*
* @param value * @param value
* @param isCorrect
*/ */
public void setValue(int value) { public void setValue(int value, boolean isCorrect) {
if(initValue) return; if(initValue) return;
if(isCorrect) possibleValues.clear();
possibleValues.clear();
this.value = value; this.value = value;
} }
+1 -1
View File
@@ -133,7 +133,7 @@ public class Settings {
newFileProperties = 0; newFileProperties = 0;
dimension = new Dimension(600, 800); dimension = new Dimension(600, 800);
resizable = false; resizable = false;
cellGUIStartMode = false; cellGUIStartMode = true;
defaultOpenState = 0; defaultOpenState = 0;
theme = new Theme(new File(appDirectory + "default.theme")); theme = new Theme(new File(appDirectory + "default.theme"));
autoFillNotes = false; autoFillNotes = false;
+3 -3
View File
@@ -173,7 +173,7 @@ public class SudokuChecker {
else if(intersection.size() == 1) { else if(intersection.size() == 1) {
int value = intersection.get(0); int value = intersection.get(0);
grid[row][col].setValue(value); grid[row][col].setValue(value, true);
updatePossibleValues(row, col); updatePossibleValues(row, col);
return; return;
} else { } else {
@@ -203,14 +203,14 @@ public class SudokuChecker {
if(grid[row][i].getValue() == 0) { if(grid[row][i].getValue() == 0) {
grid[row][i].removePossibleValue(value); grid[row][i].removePossibleValue(value);
if(grid[row][i].getPossibleValues().length == 1) { if(grid[row][i].getPossibleValues().length == 1) {
grid[row][i].setValue(grid[row][i].getPossibleValues()[0]); grid[row][i].setValue(grid[row][i].getPossibleValues()[0], true);
updatePossibleValues(row, i); updatePossibleValues(row, i);
} }
} }
if(grid[i][col].getValue() == 0) { if(grid[i][col].getValue() == 0) {
grid[i][col].removePossibleValue(value); grid[i][col].removePossibleValue(value);
if(grid[i][col].getPossibleValues().length == 1) { if(grid[i][col].getPossibleValues().length == 1) {
grid[i][col].setValue(grid[i][col].getPossibleValues()[0]); grid[i][col].setValue(grid[i][col].getPossibleValues()[0], true);
updatePossibleValues(i, col); updatePossibleValues(i, col);
} }
} }