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());
}
/**
* 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,
* 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.
*
@@ -248,11 +248,11 @@ public class Board extends JPanel {
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {
if(selected == null) cell.select();
cell.select();
}
@Override
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.
if(cell.getValue() == 0) {
internalPanel.setLayout(noteLayout);
generateNotes(s.getAutoFillNotes());
generateNotes(noteMode);
} else {
internalPanel.setLayout(valueLayout);
valueLabel.setText(Integer.toString(cell.getValue()));
@@ -86,12 +86,16 @@ class CellGUI extends JPanel {
/**
* 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
*/
public void setValue(int value) {
if(!selected) return;
cell.setValue(value);
cell.setValue(value, false);
// Update the GUI with the actual Cell value (unchanged if invalid)
valueLabel.setText(Integer.toString(cell.getValue()));
refresh();
@@ -100,18 +104,22 @@ class CellGUI extends JPanel {
/**
* 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
*/
public void setValue(int value, int expected) {
if(!selected) return;
cell.setValue(value);
cell.setValue(value, value == expected);
valueLabel.setText(Integer.toString(cell.getValue()));
if(value != expected) {
incorrect = true;
valueLabel.setText(Integer.toString(value));
errorStyle();
} else refresh();
}
@@ -122,7 +130,7 @@ class CellGUI extends JPanel {
public void removeValue() {
if(!selected) return;
cell.setValue(0);
cell.setValue(0, true);
if(!cell.isInitValue())
valueLabel.setText("");
@@ -166,25 +174,21 @@ class CellGUI extends JPanel {
/**
* Toggle the mode of the cell between value and note, including the
* layout of the cell.
*
* If the current mode is value, switch to note mode, and vice versa.
* layout and visual content of the cell.
*/
public void setNoteMode() {
if(cell.isInitValue()) return;
internalPanel.removeAll();
if(noteMode) {
internalPanel.removeAll();
internalPanel.setLayout(valueLayout);
internalPanel.add(valueLabel);
} else {
internalPanel.setLayout(noteLayout);
noteMode = true;
generateNotes(true);
}
refresh();
noteMode = !noteMode;
}
@@ -196,11 +200,8 @@ class CellGUI extends JPanel {
if(incorrect) {
errorStyle();
return;
}
if(!selected)
} else if(!selected)
defaultStyle();
else highlightedStyle();
}
@@ -256,20 +257,10 @@ class CellGUI extends JPanel {
* @see Theme.java
*/
private void errorStyle() {
setBackground(theme.getErrorBackground());
setForeground(theme.getErrorText());
internalPanel.setBackground(theme.getErrorBackground());
internalPanel.setForeground(theme.getErrorText());
valueLabel.setForeground(theme.getErrorText());
internalPanel.setBackground(theme.getErrorBackground());
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();
}
@@ -295,10 +286,7 @@ class CellGUI extends JPanel {
int[] possibleValues = cell.getPossibleValues();
// Prepare the internalPanel for noteMode.
if(autoFill) {
internalPanel.removeAll();
noteMode = true;
}
if(autoFill) internalPanel.removeAll();
// Handle cases where there are no possible values stored.
if(possibleValues.length == 0) {
+6 -2
View File
@@ -70,12 +70,16 @@ public class Cell {
*
* 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 isCorrect
*/
public void setValue(int value) {
public void setValue(int value, boolean isCorrect) {
if(initValue) return;
if(isCorrect) possibleValues.clear();
possibleValues.clear();
this.value = value;
}
+1 -1
View File
@@ -133,7 +133,7 @@ public class Settings {
newFileProperties = 0;
dimension = new Dimension(600, 800);
resizable = false;
cellGUIStartMode = false;
cellGUIStartMode = true;
defaultOpenState = 0;
theme = new Theme(new File(appDirectory + "default.theme"));
autoFillNotes = false;
+3 -3
View File
@@ -173,7 +173,7 @@ public class SudokuChecker {
else if(intersection.size() == 1) {
int value = intersection.get(0);
grid[row][col].setValue(value);
grid[row][col].setValue(value, true);
updatePossibleValues(row, col);
return;
} else {
@@ -203,14 +203,14 @@ public class SudokuChecker {
if(grid[row][i].getValue() == 0) {
grid[row][i].removePossibleValue(value);
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);
}
}
if(grid[i][col].getValue() == 0) {
grid[i][col].removePossibleValue(value);
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);
}
}