Page 1 of 1

SudokuSolver.java: package cs445.a3; public class SudokuSolver { private int[][] initialBoard; public SudokuSolver(int[]

Posted: Thu Jul 14, 2022 2:12 pm
by answerhappygod
SudokuSolver.java:
package cs445.a3;
public class SudokuSolver {
private int[][] initialBoard;
public SudokuSolver(int[][] board) {
// Here, you can do any pre-processing you want to the initialboard
initialBoard = board;
}
public static void printBoard(int[][] board) {
if (board == null) {
System.out.println("No assignment (null)");
return;
}
for (int i = 0; i < 9; i++) {
if (i == 3 || i == 6) {
System.out.println("----+-----+----");
}
for (int j = 0; j < 9; j++) {
if (j == 2 || j == 5) {
System.out.print(board[j] + " | ");
} else {
System.out.print(board[j]);
}
}
System.out.print("\n");
}
}
/**
* This public solve method simply calls the recursive helpermethod on
* this Sudoku Solver's initialBoard variable.
*/
public int[][] solve() {
int[][] result = solve(initialBoard);
// Here, you can do any post-processing you want to thesolution
return result;
}
/**
* This recursive method finds the first solution to a givenSudoku board.
*/
private int[][] solve(int[][] board) {
if (reject(board)) return null;
if (isFullSolution(board)) return board;
int[][] attempt = extend(board);
while (attempt != null) {
int[][] solution = solve(attempt);
if (solution != null) return solution;
attempt = next(attempt);
}
return null;
}
////
// Backtracking helper methods start here
////
private static boolean isFullSolution(int[][] board) {
// TODO: Complete this method
private static boolean reject(int[][] board) {
// TODO: Complete this method
return true;
}
private static int[][] extend(int[][] board) {
// TODO: Complete this method
return null;
}
private static int[][] next(int[][] board) {
// TODO: Complete this method
return null;
}
////
// Internal testing methods start here
////
private static void testIsFullSolution() {
// TODO: Complete this method
}
private static void testReject() {
// TODO: Complete this method
}
private static void testExtend() {
// TODO: Complete this method
}
private static void testNext() {
// TODO: Complete this method
}
/**
* If this class is run directly, just run the internal testmethods.
*/
public static void main(String[] args) {
System.out.println("Executing test methods.");
testIsFullSolution();
testReject();
testExtend();
testNext();
}
}
SudokoFromFile.java:
package cs445.a3;
import java.util.List;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
public class SudokuFromFile {
static int[][] readBoard(String filename) {
List<String> lines = null;
try {
lines = Files.readAllLines(Paths.get("build/resources/main/" +filename), Charset.defaultCharset());
} catch (IOException e) {
System.err.println("Error reading " + filename);
e.printStackTrace();
return null;
}
int[][] board = new int[9][9];
int val = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
try {
val =Integer.parseInt(Character.toString(lines.get(i).charAt(j)));
} catch (NumberFormatException e) {
val = 0;
}
board[j] = val;
}
}
return board;
}
public static void main(String[] args) {
if(args.length >= 1) {
int[][] board = readBoard(args[0]);
System.out.println("Initial board:");
SudokuSolver.printBoard(board);
System.out.println("Solution:");
SudokuSolver solver = new SudokuSolver(board);
SudokuSolver.printBoard(solver.solve());
} else {
System.err.println("No arguments. Check usageinstructions.");
}
}
}