| Java and Sudoku So I've been working on a problem in Java where I create a class Sudoku, which I've made using a 2 dimensional array.
For anyone who might not know, a sudoku is a 9*9 square of cells, each cell contains a number.
The rules of Sudoku are very simple:
* Every column must contain the numbers 1-9 inclusive (this automatically means it must only contain each number once)
* Every row must contain the numbers 1-9 inclusive (this automatically means it must only contain each number once)
* Every 3*3 major sub-square must contain the numbers 1-9 inclusive (this automatically means it must only contain each number once)
So it might look like this,
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
Of course this isnt a valid solution, as the 3*3 subgrids don't have 1-9.
My problem comes in that I'm trying to create the sudoku, and then give a message if its a valid solution or not. I'm kind of new to Java, and was wondering what would be the best means to create an algorithm to search thrrough the rows and columns and subgrids to see if 1-9 is there, and if not to return back false as well as where in the sudoku the solution first wasn't satisfied.
Would a linear or binary search work here? Or how would you implement a solution?
This is sort of the framework I'm working on.
import java.io.*;
import java.util.*;
public class Sudoku {
private int[ ][ ] puzzle;
private String message;
public Sudoku ( String fileName ) throws FileNotFoundException {
. . .
}
private boolean isRowValid(int row) {
. . .
}
private boolean isColumnValid(int col) {
. . .
}
private boolean isSubSquareValid(int row, int col) {
. . .
}
public boolean isASolution() {
. . .
}
public String toString() {
. . .
}
} |