Sudoku, just need help on generating random numbers... and then checking

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diggity
    New Member
    • Jun 2010
    • 2

    Sudoku, just need help on generating random numbers... and then checking

    okay, so i started on code for Sudoku on ready to program java ... very close to java. I was able to create a 2d array and display and change the variables. What i want to do now is to generate random numbers in the array. Numbers in between 1 and 9. and then i need to check for rows, columns and the boxes... i am new to arrays and i really need some help.

    Code:
    // The "Sudoku" class.
    import java.awt.*;
    import hsa.Console;
    public class Sudoku
    {
        static Console c;           // The output console
        public static void main (String[] args)
        {
            c = new Console ();
    
            final int ROWS = 9;
            final int COLS = 9;
            int row;
            int col;
            int newValue = 0;
            /* row and col are what the user enters, to locate what place they want to change. New value is what 
    they want to change to*/
                
    
           
             int[] [] board = new int [ROWS] [COLS];
           //creates the array
            c.println("");
            c.println("");
            c.println("");
            while (newValue != -1)   // entire loop prints ///////////////////////////////////all the numbers out
            {
                for (int i = 0 ; i < ROWS ; i++)
                {
    
                    for (int j = 0 ; j < COLS ; j++)
                    {
    
                        c.print ("    " + board [i] [j]);
    
                    }
                    c.println ("");
       
    
                }
                c.print ("Row: ");
                row = c.readInt () - 1;
                c.print ("Column: ");
                col = c.readInt () - 1;
                c.print ("change to: ");
                newValue = c.readInt ();//inputs 4 location
                if (newValue < 1 || newValue > 9 || row <0 || row > 8 || col < 0 ||col>8)
                {
                    c.print ("your entry is invalid");
                    c.clear();
                }
                else
                {
                    board [row] [col] = newValue;
                    c.print (board [row] [col]);
                    c.clear ();
                }
    
    
            }
        }
    } // this is all it does, please help!
    // main method
    // Sudoku class
    Last edited by Niheel; Jun 10 '10, 12:51 AM. Reason: Placed code in code tags. Please use code tags to show your code in the future.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Since you have the two for loops printing the value of the cell, just before that, set that cell to a random value, eg

    board[i][j] = rand()*9 + 1
    c.print (" " + board [i] [j]);

    Comment

    • diggity
      New Member
      • Jun 2010
      • 2

      #3
      the problem with doing that is that i also have to stay within the ruels of the game, the same number may appear twice in the same row/column or 3x3 cell

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        How do you want to check for duplicates?
        What do you want to do if there are no more possibilities (eg, the numbers generated so far lead to an invalid solution.)

        Comment

        Working...