Sudoku Verification, Turning Pseudo-Code into real code!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DannyB13
    New Member
    • Oct 2009
    • 3

    Sudoku Verification, Turning Pseudo-Code into real code!

    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
    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; 
    }
    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.


    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(){
    		
    	
    	}
    }
    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.
  • DannyB13
    New Member
    • Oct 2009
    • 3

    #2
    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.
    Code:
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • DannyB13
        New Member
        • Oct 2009
        • 3

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

        Comment

        Working...