By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Sudoku Verification, Turning Pseudo-Code into real code!

P: 3
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
Expand|Select|Wrap|Line Numbers
  1. private boolean validateSudoku(int[][] b)
  2. for (int i = 0; i < b.length; i++) {
  3. String row = "";
  4. String col = "";
  5. for (int j = 0; j < b[i].length; j++) {
  6. if(!row.contains(b[i][j] + ""))
  7. row +=b[i][j] + "";
  8. if(!col.contains(b[j][i] + ""))
  9. row += b[j][i] + ""; 
  10. if(row.length() + col.length() != b.length + b[0].length)
  11. return false;
  12. }
  13. return true; 
  14. }
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.

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

Oh here's my full code I've done so far by the way.


Expand|Select|Wrap|Line Numbers
  1. public class Sudoku {
  2.     private int N = 3; //sets 'N'. This can be changed to make bigger/smaller sudoku
  3.     private int[][]board; // creates the board.
  4.     private int square = N*N;
  5.  
  6.     public static void main (String []Args){
  7.         Sudoku s1 = new Sudoku();
  8.         s1.show();
  9.         s1.isSolution();
  10.     }
  11.  
  12.     public Sudoku(){ //constructor
  13.         board = new int [square][square]; //Sets the overall size of the board.
  14.         for (int i=0; i< square; ++i){
  15.             for(int j=0; j< square; ++j){
  16.                 board [i][j]=0; // Goes through all columns and rows, sets it to 0
  17.             }
  18.         }
  19.     }
  20.  
  21.     public    int getSquare(int i, int j){ // Gets the value of board[i][j] 
  22.  
  23.         if(i >=0 && i< square && j>=0 && j<square)
  24.             return board[i][j];
  25.         else
  26.             return 0;
  27.  
  28.     }
  29.  
  30.     public void setSquare(int i, int j, int val){// Sets board[i][j] = val.
  31.         board [i][j] = val;
  32.  
  33.     }
  34.  
  35.     public void show() {
  36.         for (int i=0; i< square; ++i){
  37.             if (i%3 == 0 && i > 0)
  38.                 System.out.println("------+------+------");
  39.             for(int j=0; j< square; ++j){
  40.                 if (board [i][j] == 0)
  41.                     System.out.print(" .");
  42.                 else 
  43.                     System.out.print(" " + board [i][j]);
  44.                 if ((j+1)%3 == 0 && j < square -1)    
  45.                     System.out.print("|");
  46.             }
  47.             System.out.println();
  48.         }
  49.     }
  50.  
  51.     public boolean isSolution(){
  52.  
  53.  
  54.     }
  55. }
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.
Oct 16 '09 #1
Share this Question
Share on Google+
3 Replies


P: 3
I have another bit to add in. Another part of the assignment is
Write a method

void fill(String[] lines);
in the Sudoku class that takes an array of N*N Strings. Each string has N*N elements, each of which is either a digit or period. Periods indicate empty squares.
Expand|Select|Wrap|Line Numbers
  1. So far I have
  2.     public void fill(String[] lines){
  3.         for(int x=0; x< square; x++){
  4.             lines[x] = 
  5.         }
square being N*N, in this case 9.
I want this code to pick up only periods and digits. None of the border of +,-,| around it. Is there a type of code to do that? Forgive me if you can't tell I'm pretty darn rusty here.
Oct 16 '09 #2

Expert 10K+
P: 11,448
Check the Java insights section, it has a three part article on Sudoku, including the reading and initializing, solving and printing the darn thing.

kind regards,

Jos
Oct 16 '09 #3

P: 3
My question has been moved to http://forums.sun.com/thread.jspa?th...art=0&tstart=0 and is coming along well.
Oct 16 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.