Hi, and thanks for possible help in advance.
Here's my dilemma. I've been making a sudoku generator, and I'm now stuck on one part. I must be able to take a 'solution' and verify that it is correct, by checking a few things.
1. There are enough numbers, no 'periods' which signify '0' or 'no input'.
2. That there are no duplicate numbers in any row, column, or 3x3 box.
I have been thinking of ways to do this and I've come up with 2 things. Firstly is this bit of code. When I put i in, however, it won't work.
//requires that you only allow valid input of characters 1-n where n is the size of the board
The Second is this pseudo code I've created, and although sure this method is better, I can't seem to figure out how to make this happen.
Oh here's my full code I've done so far by the way.
I'm getting kinda frustrated and a point in the right direction or even some help would be greatly appreciated. Thank you for your time and help.
Here's my dilemma. I've been making a sudoku generator, and I'm now stuck on one part. I must be able to take a 'solution' and verify that it is correct, by checking a few things.
1. There are enough numbers, no 'periods' which signify '0' or 'no input'.
2. That there are no duplicate numbers in any row, column, or 3x3 box.
I have been thinking of ways to do this and I've come up with 2 things. Firstly is this bit of code. When I put i in, however, it won't work.
//requires that you only allow valid input of characters 1-n where n is the size of the board
Code:
private boolean validateSudoku(int[][] b) { for (int i = 0; i < b.length; i++) { String row = ""; String col = ""; for (int j = 0; j < b[i].length; j++) { if(!row.contains(b[i][j] + "")) row +=b[i][j] + ""; if(!col.contains(b[j][i] + "")) row += b[j][i] + ""; if(row.length() + col.length() != b.length + b[0].length) return false; } } return true; }
take an array with 9 numbers; this represents a row, or a column, or a 3x3 square, whatever, call this listArray
if listArray is not exactly 9 elements long, it's invalid, so return false
create blank array, call this testArray
for each number in listArray:
if testArray contains this number:
listArray is invalid
return false
else:
add this number to testArray
return true
if listArray is not exactly 9 elements long, it's invalid, so return false
create blank array, call this testArray
for each number in listArray:
if testArray contains this number:
listArray is invalid
return false
else:
add this number to testArray
return true
Oh here's my full code I've done so far by the way.
Code:
public class Sudoku { private int N = 3; //sets 'N'. This can be changed to make bigger/smaller sudoku private int[][]board; // creates the board. private int square = N*N; public static void main (String []Args){ Sudoku s1 = new Sudoku(); s1.show(); s1.isSolution(); } public Sudoku(){ //constructor board = new int [square][square]; //Sets the overall size of the board. for (int i=0; i< square; ++i){ for(int j=0; j< square; ++j){ board [i][j]=0; // Goes through all columns and rows, sets it to 0 } } } public int getSquare(int i, int j){ // Gets the value of board[i][j] if(i >=0 && i< square && j>=0 && j<square) return board[i][j]; else return 0; } public void setSquare(int i, int j, int val){// Sets board[i][j] = val. board [i][j] = val; } public void show() { for (int i=0; i< square; ++i){ if (i%3 == 0 && i > 0) System.out.println("------+------+------"); for(int j=0; j< square; ++j){ if (board [i][j] == 0) System.out.print(" ."); else System.out.print(" " + board [i][j]); if ((j+1)%3 == 0 && j < square -1) System.out.print("|"); } System.out.println(); } } public boolean isSolution(){ } }
Comment