patternjavaModerate
Logical Sudoku Solver in Java
Viewed 0 times
solversudokulogicaljava
Problem
I've been working on a Sudoku solver as an introduction to the Java programming language. I know there's a bunch of ways to program a Sudoku solver, including a brute force/recursive approach and a logical approach. I've chosen the logical approach to start out to keep things simple.
This program solves Sudokus the exact same way I do when I'm using pen and paper. It picks a number, and then it picks a 3x3 region (I call these regions "squares" in my comments), and then it analyzes that region and the rows and columns that intersect it to see what boxes it can eliminate. Once all but one box has been eliminated, then that leaves us with our solved square.
I have big plans for the program, including adding a GUI, adding more logical techniques (Sudoku is surprisingly elaborate, check out all the techniques they have here for example), and adding that recursive/brute force technique in another method. Before I go further however, I'd like to run my code by you guys for review. That way if I have any bad habits we can catch them now.
My background is mainly PHP programming.
SudokuSolver class:
Sudo
This program solves Sudokus the exact same way I do when I'm using pen and paper. It picks a number, and then it picks a 3x3 region (I call these regions "squares" in my comments), and then it analyzes that region and the rows and columns that intersect it to see what boxes it can eliminate. Once all but one box has been eliminated, then that leaves us with our solved square.
I have big plans for the program, including adding a GUI, adding more logical techniques (Sudoku is surprisingly elaborate, check out all the techniques they have here for example), and adding that recursive/brute force technique in another method. Before I go further however, I'd like to run my code by you guys for review. That way if I have any bad habits we can catch them now.
My background is mainly PHP programming.
SudokuSolver class:
public class SudokuSolver
{
public static void main(String[] args)
{
// score 51 - websudoku.com - medium difficulty - solved in 14 iterations using LAST CANDIDATE
Sudoku easyToSolve = new Sudoku("036000820009500000800400007600100039003000500920006008500004002000002700071000450");
// score 57 - websudoku.com - hard difficulty - my solver can't solve this yet - has some HIDDEN SINGLES
Sudoku hardToSolve = new Sudoku("000007018094150700005600000106000000080070020000000904000003800008029140370400000");
// Here are the methods you can use here:
// printPuzzle() - prints the puzzle in a more readable form
// solvePuzzle() - solves the puzzle
// solvePuzzle(true) - shows you in detail how it solves the puzzle
easyToSolve.solvePuzzle(true);
hardToSolve.solvePuzzle(true);
}
}Sudo
Solution
Quitting an application
This is not a good way to quit an application where you use it. When you're in a class and you encounter an invalid state for your application, throw an exception. It's not the responsibility of the class to decide if you should terminate the application or not. If you throw an exception, you can control the flow of your application.
In a small program, you won't see the drawback of using this, but in more complex application you will likely want to close your application in a graceful manner. Even for your application you could want someday to save the state of the Sudoku you're working with maybe.
In your case, you could probably throw
Boolean if
This is the same as this :
I suggest you check directly with the boolean, it will remove "noise" form your code.
Method with boolean argument
The first time I read your main, I find it hard to understand what
You can now know simply by reading the method name that you will have more details in your output.
if ( incomingPuzzle.length() != 81 )
{
System.out.println("Invalid Sudoku puzzle syntax. Should be 81 numbers.");
System.exit(1);
}This is not a good way to quit an application where you use it. When you're in a class and you encounter an invalid state for your application, throw an exception. It's not the responsibility of the class to decide if you should terminate the application or not. If you throw an exception, you can control the flow of your application.
In a small program, you won't see the drawback of using this, but in more complex application you will likely want to close your application in a graceful manner. Even for your application you could want someday to save the state of the Sudoku you're working with maybe.
In your case, you could probably throw
IllegalArgumentException, this is a RuntimeException taht unless explicitly catch somewhere, will terminate the application. The advantage of using an exception vs System.exit is that you could decide to catch the exception and terminate the application with what you need to do.Boolean if
if ( showDetails == true )This is the same as this :
if (showDetails)I suggest you check directly with the boolean, it will remove "noise" form your code.
Method with boolean argument
The first time I read your main, I find it hard to understand what
solvePuzzle(true) was suppose to mean. I needed to read a comment and look at the method to know that this turn on a more detailed output. I'm not a fan of this approach. I could suggest that you create a another method which would look like this : public void solvePuzzleWithDetails()
{
solvePuzzle(true);
}You can now know simply by reading the method name that you will have more details in your output.
Code Snippets
if ( incomingPuzzle.length() != 81 )
{
System.out.println("Invalid Sudoku puzzle syntax. Should be 81 numbers.");
System.exit(1);
}if ( showDetails == true )if (showDetails)public void solvePuzzleWithDetails()
{
solvePuzzle(true);
}Context
StackExchange Code Review Q#46640, answer score: 16
Revisions (0)
No revisions yet.