bug fixes and project structure update

This commit is contained in:
Josh Ashton
2024-03-20 22:45:47 -06:00
parent d28377eb14
commit 8893cbf99a
4 changed files with 38 additions and 73 deletions
-2
View File
@@ -1,5 +1,3 @@
package src;
/** /**
* A Cell represents a single cell in the Sudoku grid. * A Cell represents a single cell in the Sudoku grid.
* This helper class is used to store the row, column, value, and possible values for a cell. * This helper class is used to store the row, column, value, and possible values for a cell.
+13 -11
View File
@@ -1,7 +1,6 @@
package src;
import java.util.Scanner; import java.util.Scanner;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JComboBox; import javax.swing.JComboBox;
@@ -34,6 +33,7 @@ public class Nav extends JPanel {
* file I/O behavior is dictated by the values in the Settings object. * file I/O behavior is dictated by the values in the Settings object.
*/ */
public Nav() { public Nav() {
super();
System.out.println("Welcome to the Sudoku Solver!"); System.out.println("Welcome to the Sudoku Solver!");
Scanner in = new Scanner(System.in); Scanner in = new Scanner(System.in);
@@ -44,7 +44,7 @@ public class Nav extends JPanel {
String inputFilename = in.nextLine(); String inputFilename = in.nextLine();
in.close(); in.close();
open(inputFilename); openFile(inputFilename);
} }
/** /**
@@ -54,6 +54,7 @@ public class Nav extends JPanel {
* @param Settings * @param Settings
*/ */
public Nav(Settings s) { public Nav(Settings s) {
super();
this.s = s; this.s = s;
System.out.println("Welcome to the Sudoku Solver!"); System.out.println("Welcome to the Sudoku Solver!");
@@ -65,15 +66,15 @@ public class Nav extends JPanel {
String inputFilename = in.nextLine(); String inputFilename = in.nextLine();
in.close(); in.close();
open(inputFilename); openFile(inputFilename);
} }
private void new() { private void newFile() {
int default = s.getNewFileProperties(); int defaultOption = s.getNewFileProperties();
// See Settings.getNewFileProperties for possible values and // See Settings.getNewFileProperties for possible values and
// the expected behavior of each value. // the expected behavior of each value.
switch(default) { switch(defaultOption) {
case 0: case 0:
case 1: case 1:
case 2: case 2:
@@ -87,7 +88,7 @@ public class Nav extends JPanel {
* *
* @param String * @param String
*/ */
private void open(String filename) { private void openFile(String filename) {
File f = new File(/*"spring24/sudoku/" + */filename + ".sdku"); File f = new File(/*"spring24/sudoku/" + */filename + ".sdku");
if(f == null || !f.exists() || f.isDirectory() || !f.canRead()) { if(f == null || !f.exists() || f.isDirectory() || !f.canRead()) {
System.out.println( System.out.println(
@@ -99,13 +100,13 @@ public class Nav extends JPanel {
f.getAbsolutePath() f.getAbsolutePath()
); );
return null; return;
} }
createGrid(f); createGrid(f);
} }
private void save() { private void saveFile() {
// TODO: Get the current Cell[][] from the Board JPanel // TODO: Get the current Cell[][] from the Board JPanel
// TODO: Write to a file. // TODO: Write to a file.
} }
@@ -162,12 +163,13 @@ public class Nav extends JPanel {
this.grid = grid; this.grid = grid;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
String filename = f.getName();
System.err.println("An error occurred while reading the file " + System.err.println("An error occurred while reading the file " +
filename + ".sdku." filename + ".sdku."
); );
e.printStackTrace(); e.printStackTrace();
return null; return;
} }
} }
} }
-1
View File
@@ -1,4 +1,3 @@
package src;
import java.util.Scanner; import java.util.Scanner;
import java.io.File; import java.io.File;
+18 -52
View File
@@ -1,5 +1,3 @@
package src;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -40,15 +38,13 @@ import java.util.Scanner;
* 961537284 * 961537284
* 287419635 * 287419635
* 345286179 * 345286179
*
* This challenge is broken up into parts. In Programming Club, we will split into teams of 3-4 people to work on each method.
*/ */
public class SudokuChecker { public class SudokuChecker {
private Cell[][] grid;
public static void main(String[] args) { public SudokuChecker(Cell[][] grid) {
this.grid = grid;
Cell[][] grid = readPuzzle(inputFilename); solve();
solve(grid);
System.out.println("The solution to the Sudoku puzzle is:"); System.out.println("The solution to the Sudoku puzzle is:");
for(int i = 0; i < 9; i++) { for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) { for(int j = 0; j < 9; j++) {
@@ -56,28 +52,6 @@ public class SudokuChecker {
} }
System.out.println(); System.out.println();
} }
//app.saveSolution(outputFilename, grid);
}
/**
* Given an input filename, read the Sudoku puzzle from the file and return it as a 9x9 grid numbers, with no value for spaces in the file.
*
* @param filename
* @return
*/
public Cell[][] readPuzzle(String filename) {
}
/**
* Given an output filename and a 9x9 grid of numbers, write the Sudoku puzzle to the file.
*
* @param grid
* @return
*/
public void saveSolution(String filename, Cell[][] grid) {
System.out.println("TODO: Write sudoku puzzle solution to " + filename + ".sdku");
// TODO: Account for possible invalid solution from isSolved and handle an error.
// TODO: Write the solution to the file.
} }
/** /**
@@ -102,14 +76,13 @@ public class SudokuChecker {
* Determine what numbers are available to be placed in the given cell of the grid. * Determine what numbers are available to be placed in the given cell of the grid.
* This is effectively an intersection of the numbers available in the row, column, and box of the cell. * This is effectively an intersection of the numbers available in the row, column, and box of the cell.
* *
* @param grid
* @param row * @param row
* @param col * @param col
* @return an array of numbers that are available to be placed in the given cell * @return an array of numbers that are available to be placed in the given cell
*/ */
private void getAvailableNumbers(Cell[][] grid, int row, int col) { private void getAvailableNumbers(int row, int col) {
ArrayList<Integer> intersection = intersection(getRowRemainingNumbers(grid, row), getColRemainingNumbers(grid, col)); ArrayList<Integer> intersection = intersection(getRowRemainingNumbers(row), getColRemainingNumbers(col));
intersection = intersection(intersection, getBoxRemainingNumbers(grid, row, col)); intersection = intersection(intersection, getBoxRemainingNumbers(row, col));
if(intersection.size() == 0) { if(intersection.size() == 0) {
return; return;
@@ -117,7 +90,7 @@ public class SudokuChecker {
int value = intersection.get(0); int value = intersection.get(0);
grid[row][col].setValue(value); grid[row][col].setValue(value);
updatePossibleValues(grid, row, col); updatePossibleValues(row, col);
return; return;
} else { } else {
// Join the arrays and find the intersection of the three arrays. // Join the arrays and find the intersection of the three arrays.
@@ -134,11 +107,10 @@ public class SudokuChecker {
* Update the possible values for cells in the same row, column, and box as the given cell. * Update the possible values for cells in the same row, column, and box as the given cell.
* This should always be called once a cell's value has been set, to remove that value from the possible values of other cells. * This should always be called once a cell's value has been set, to remove that value from the possible values of other cells.
* *
* @param grid
* @param row * @param row
* @param col * @param col
*/ */
private void updatePossibleValues(Cell[][] grid, int row, int col) { private void updatePossibleValues(int row, int col) {
int value = grid[row][col].getValue(); int value = grid[row][col].getValue();
for(int i = 0; i < 9; i++) { for(int i = 0; i < 9; i++) {
@@ -146,14 +118,14 @@ public class SudokuChecker {
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]);
updatePossibleValues(grid, 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]);
updatePossibleValues(grid, i, col); updatePossibleValues(i, col);
} }
} }
} }
@@ -171,16 +143,14 @@ public class SudokuChecker {
/** /**
* Solve the Sudoku puzzle. * Solve the Sudoku puzzle.
*
* @param grid
*/ */
public void solve(Cell[][] grid) { public void solve() {
// Continue until a valid solution is reached. // Continue until a valid solution is reached.
while(!isValidSolution(grid)) { while(!isValidSolution()) {
for(int i = 0; i < 9; i++) { for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) { for(int j = 0; j < 9; j++) {
if(grid[i][j].getValue() == 0) { if(grid[i][j].getValue() == 0) {
getAvailableNumbers(grid, i, j); getAvailableNumbers(i, j);
} }
} }
} }
@@ -192,11 +162,10 @@ public class SudokuChecker {
/** /**
* Get any number between 1 and 9 that is not in the row. * Get any number between 1 and 9 that is not in the row.
* *
* @param grid
* @param row * @param row
* @return * @return
*/ */
private ArrayList<Integer> getRowRemainingNumbers(Cell[][] grid, int row) { private ArrayList<Integer> getRowRemainingNumbers(int row) {
ArrayList<Integer> remainingNumbers = new ArrayList<Integer>(); ArrayList<Integer> remainingNumbers = new ArrayList<Integer>();
for(int i = 1; i <= 9; i++) { for(int i = 1; i <= 9; i++) {
boolean found = false; boolean found = false;
@@ -217,11 +186,10 @@ public class SudokuChecker {
/** /**
* Get any number between 1 and 9 that is not in the column. * Get any number between 1 and 9 that is not in the column.
* *
* @param grid
* @param col * @param col
* @return * @return
*/ */
private ArrayList<Integer> getColRemainingNumbers(Cell[][] grid, int col) { private ArrayList<Integer> getColRemainingNumbers(int col) {
ArrayList<Integer> remainingNumbers = new ArrayList<Integer>(); ArrayList<Integer> remainingNumbers = new ArrayList<Integer>();
for(int i = 1; i <= 9; i++) { for(int i = 1; i <= 9; i++) {
boolean found = false; boolean found = false;
@@ -244,11 +212,10 @@ public class SudokuChecker {
* *
* A box is a 3x3 subgrid of the 9x9 grid. * A box is a 3x3 subgrid of the 9x9 grid.
* *
* @param grid
* @param box * @param box
* @return * @return
*/ */
private ArrayList<Integer> getBoxRemainingNumbers(Cell[][] grid, int row, int col) { private ArrayList<Integer> getBoxRemainingNumbers(int row, int col) {
ArrayList<Integer> remainingNumbers = new ArrayList<Integer>(); ArrayList<Integer> remainingNumbers = new ArrayList<Integer>();
int boxRow = row / 3; int boxRow = row / 3;
int boxCol = col / 3; int boxCol = col / 3;
@@ -291,10 +258,9 @@ public class SudokuChecker {
* *
* A valid Sudoku puzzle is one where each row, column, and 3x3 subgrid contains the numbers 1-9 exactly once. * A valid Sudoku puzzle is one where each row, column, and 3x3 subgrid contains the numbers 1-9 exactly once.
* *
* @param grid a 9x9 grid of numbers
* @return true if the grid is a valid Sudoku puzzle, and false otherwise * @return true if the grid is a valid Sudoku puzzle, and false otherwise
*/ */
private boolean isValidSolution(Cell[][] grid) { private boolean isValidSolution() {
// Check that every cell has a value between 1 and 9. // Check that every cell has a value between 1 and 9.
for(int i = 0; i < 9; i++) { for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) { for(int j = 0; j < 9; j++) {