440,092 Members | 1,546 Online 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 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.length) return false; } }  return true;  } 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 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 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(){         } } 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
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 So far I have     public void fill(String[] lines){         for(int x=0; x< square; x++){             lines[x] =          } 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 