Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Good people what's wrong with my sudoku generator[newbie needs help]

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Good people what's wrong with my sudoku generator[newbie needs help]

    My code is exactly like this and it's not working i dunno why !!! Sorry for bad English but what can i do ^^
    SuDoKuGenerator.rar
    i think i attached a .rar file where my code is.
    I am newbie and i wrote code i think it should work but it's not working.If i want to fill 70/81 fields of sudoku grid my code would crash in second unlimited loop which should stop after number of entered field reach 70 or any other number above.I would be thankful for one who can tell me what is problem with my code.As i said i'm completely newbie and this is what i learned in my school.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    package sudoku.generator;

    import java.util.Scanner;

    public class SuDoKuGenerator {

    public static void main(String[] args)
    {
    //Creating two-dimensional array which is a sudoku table
    int[][] grid = new int[9][9];
    //giving a 0 value to each field which means it's a EMPTY field
    for(int i = 0;i<9;i++)
    {
    for(int j =0; j<9;j++)
    {
    grid[i][j] = 0;
    }
    }
    Scanner input = new Scanner(System.in);
    //Welcome message
    System.out.print("This is a Sudoku game.Please choose a difficult: "
    + "\n[1]Easy Sudoku\n[2]Medium Sudoku\n[3]Hard Sudoku\nYou are choosing by entering a number and then pressing ENTER KEY!\n");

    int neededNumberOfEntered,
    numberOfEntered = 0,
    fieldValue,
    randx,
    randy,
    counter = 0;

    //Interaction with user where is he asked for difficult
    while(true)
    {
    int level = input.nextInt();
    if(level == 1)
    {
    System.out.print("You have chosen Easy Sudoku!\n\n");
    neededNumberOfEntered = (int)(51 + Math.random()*25);
    break;
    }
    else if(level == 2)
    {
    System.out.print("You have chosen Medium Sudoku!\n\n");
    neededNumberOfEntered = (int)(31 + Math.random()*20);
    break;
    }
    else if(level == 3)
    {
    System.out.print("You have chosen Hard Sudoku!\n\n");
    neededNumberOfEntered = (int)(21 + Math.random()*10);
    break;
    }
    else
    {
    System.out.print("Please choose again!!! \n");
    }
    }

    //part of code which generates a half-solved Sudoku grid
    while(true)
    {
    //Condition
    if(numberOfEntered == neededNumberOfEntered)
    {
    break;
    }
    randx = (int)(Math.random()*9); // randx - first grid coordinate from 0 - 8
    randy = (int)(Math.random()*9); // randy - second grid coordinate from 0 - 8
    fieldValue = (int)(1 + (Math.random()*9)); // value of the field {1,2,3,4,5,6,7,8, or 9}
    boolean fieldIsEmpty = false; // Logical type which says that random chosen field is EMPTY(we just guess)
    boolean littleSquare = true; // littleSquare means 3x3 square. True means that 3x3 square is properly filled with random numbers
    int x,y; // x and y are helping cordinates for finding 3x3 square where is our random field (field grid[randx][randy])
    if((randx/3) == 0)
    {
    x=0;
    }
    else if((randx/3) == 1)
    {
    x=3;
    }
    else
    {
    x=6;
    }
    if((randy/3) == 0)
    {
    y=0;
    }
    else if((randy/3) == 1)
    {
    y=3;
    }
    else
    {
    y=6;
    }
    //In this box we are checking 3x3 grid.In case that all field values in 3x3 grid are different littleSquare should be TRUE
    int conditionForX = x+3;
    int conditionForY = y+3;
    for(;x<conditionForX;x++)
    {
    for(;y<conditionForY;y++)
    {
    if(grid[x][y] == fieldValue)
    {
    littleSquare = false;
    }
    }
    }
    //in this part we are asking if the field is empty
    if(grid[randx][randy] == 0)
    {
    fieldIsEmpty = true;
    }
    //in case that our field is not empty we will be on beginning of our loop, again define new coordinates and field value
    //if field it's empty we are checking next IF
    if(fieldIsEmpty == true)
    {
    //we are writting our field value in our random grid field
    grid[randx][randy] = fieldValue;
    //It's time to check rows and columns
    //we'll check nine times
    for(int i=0;i<9;i++)
    {
    //checking columns
    if((grid[i][randy] == fieldValue) && (i != randx))
    {
    grid[randx][randy] = 0;
    break;
    }
    //checking rows
    else if((grid[randx][i] == fieldValue) && (i != randy))
    {
    grid[randx][randy] = 0;
    break;
    }
    //also we need confirmation that our 3x3 grid is properly filled
    else if(littleSquare == false)
    {
    grid[randx][randy] = 0;
    break;
    }
    else
    {
    //in case thet first index i "survived" our control counter should be raised
    counter++;
    //field will be properly filled when we check 9 times, so our counter should have value of 9
    }
    }

    }
    //we are asking if our field "survived" our control and if counter is 9
    if(((counter % 9) == 0) && (counter != 0))
    {
    //if so we reset our counter to zero and raise a numberOfEntered integer which means we inserted one field properly
    numberOfEntered ++;
    counter = 0;

    }
    else
    {
    //if not we will just reset our counter
    counter = 0;
    }

    }
    //This is write out of our grid
    for(int i = 0;i<9;i++)
    {
    for(int j = 0;j<9;j++)
    {
    System.out.print(grid[i][j] + " ");
    }
    System.out.print("\n");

    }

    }
    }

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    Hi Achtung and willkommen

    Please see the announcements page for instructions on the use of code tags and other useful information. As you can see in your post, parts of the code turns into faces. The posted code is long and unclear without being formatted.
    My code is exactly like this and it's not working i dunno why !!!
    This does not say much about what is wrong. If there are any error messages please post them too. At least describe what the program should do, and what it is doing. Post the output from sample runs if necessary.

    Sorry for bad English but what can i do ^^
    You can post your question in English, and if you feel it is unclear, there are no rules against posting the same question in your favorite language at the bottom of your post. (I encourage it)

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    OK ..... i read that announcements and i think i'm ready.
    First: My code should MAKE A HALF SOLVED SUDOKU GRID (like you have in sudoku magazines).... i named it Sudoku generator ......
    Second: there is no errors - code is OKAY and there's no syntax errors like(;, {, } etc...).It simply crashes in second unlimited loop.
    Here is highlight-ed code
    package sudoku.generator;
     
    import java.util.Scanner;
     
    public class SuDoKuGenerator {
     
        public static void main(String[] args) 
        {
            //Creating two-dimensional array which is a sudoku table
           int[][] grid = new int[9][9]; 
           //giving a 0 value to each field which means it's a EMPTY field
           for(int i = 0;i<9;i++)
           {
               for(int j =0; j<9;j++)
               {
                   grid[i][j] = 0;
               }
           }
           Scanner input = new Scanner(System.in);
           //Welcome message :D
           System.out.print("This is a Sudoku game.Please choose a difficult: " 
           + "\n[1]Easy Sudoku\n[2]Medium Sudoku\n[3]Hard Sudoku\nYou are choosing by entering a number and then pressing ENTER KEY!\n");
     
           int neededNumberOfEntered,
               numberOfEntered = 0,
               fieldValue,
               randx,
               randy,
               counter = 0;
     
           //Interaction with user where is he asked for difficult 
           while(true)
           {
                int level = input.nextInt();
                if(level == 1)           
                {
                   System.out.print("You have chosen Easy Sudoku!\n\n");
                   neededNumberOfEntered = (int)(51 + Math.random()*25);
                   break;
                }
                else if(level == 2)
                {
                   System.out.print("You have chosen Medium Sudoku!\n\n");
                   neededNumberOfEntered = (int)(31 + Math.random()*20);
                   break;
                }
                else if(level == 3)
                {
                    System.out.print("You have chosen Hard Sudoku!\n\n");
                   neededNumberOfEntered = (int)(21 + Math.random()*10);
                   break;
                }
                else
                {
                    System.out.print("Please choose again!!! \n");
                }
           }
     
           //part of code which generates a half-solved Sudoku grid
           while(true)
           { 
              //Condition
              if(numberOfEntered  == neededNumberOfEntered)
              {
                  break;
              }
              randx = (int)(Math.random()*9); // randx - first grid coordinate from 0 - 8
              randy = (int)(Math.random()*9); // randy - second grid coordinate from 0 - 8
              fieldValue = (int)(1 + (Math.random()*9)); // value of the field {1,2,3,4,5,6,7,8, or 9}
              boolean fieldIsEmpty = false; // Logical type which says that random chosen field is EMPTY(we just guess)
              boolean littleSquare = true;  // littleSquare means 3x3 square. True means that 3x3 square is properly filled with random numbers
              int x,y; // x and y are helping coordinates for finding 3x3 square where is our random field (field grid[randx][randy])
              if((randx/3) == 0)
              {
                  x=0;
              }
              else if((randx/3) == 1)
              {
                  x=3;
              }
              else
              {
                  x=6;
              }
              if((randy/3) == 0)
              {
                  y=0;
              }
              else if((randy/3) == 1)
              {
                  y=3;
              }
              else
              {
                  y=6;
              }
              //In this box we are checking 3x3 grid.In case that all field values in 3x3 grid are different littleSquare should be TRUE 
              int conditionForX = x+3;
              int conditionForY = y+3;
              for(;x<conditionForX;x++)
              {
                  for(;y<conditionForY;y++)
                  {
                      if(grid[x][y] == fieldValue)
                      {
                          littleSquare = false;
                      }
                  }
              }
              //in this part we are asking if the field is empty
              if(grid[randx][randy] == 0)
              {
                  fieldIsEmpty = true;
              }
              //in case that our field is not empty we will be on beginning of our loop, again define new coordinates and field value :D
              //if field it's empty we are checking next IF
              if(fieldIsEmpty == true)
              {
                  //we are writing our field value in our random grid field
                  grid[randx][randy] = fieldValue;
                  //It's time to check rows and columns
                  //we'll check nine times
                  for(int i=0;i<9;i++)
                  {
                      //checking columns
                      if((grid[i][randy] == fieldValue) && (i != randx))
                      {
                          grid[randx][randy] = 0;
                          break;
                      }
                      //checking rows
                      else if((grid[randx][i] == fieldValue) && (i != randy))
                      {
                          grid[randx][randy] = 0;
                          break;
                      }
                      //also we need confirmation that our 3x3 grid is properly filled
                      else if(littleSquare == false)
                      {
                          grid[randx][randy] = 0;
                          break;
                      }
                      else
                      {
                          //in case that first index i "survived" our control counter should be raised
                          counter++;
                          //field will be properly filled when we check 9 times, so our counter should have value of 9
                      }    
                  }
     
              }
              //we are asking if our field "survived" our control and if counter is 9 :)
              if(((counter % 9) == 0) && (counter != 0))  
              {
                  //if so we reset our counter to zero and raise a numberOfEntered integer which means we inserted one field properly
                  numberOfEntered ++;
                  counter = 0;
     
              }
              else 
              {
                  //if not we will just reset our counter
                  counter = 0;
              }
     
           }
           //This is write out of our grid :D
           for(int i = 0;i<9;i++)
           {
               for(int j = 0;j<9;j++)
               {
                   System.out.print(grid[i][j] + "  ");
               }
               System.out.print("\n");
     
           }
     
        }
    }

    In my code you'll see two unlimited loops it crashes in second (longer one) .....

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    It simply crashes
    What do you mean it simply crashes? Is there an error message?

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    No there's NO ERROR MESSAGE ..... it just STAYS in second while .... i know it because program can't reach the end (can't write my) grid. IDK how to explain .... down in textbox where are outputs it doesn't says BUILD SUCESSFULL total time blah blah .... nope it says only when i stop program BUILD STOPPED TOTAL TIME blah blah .....i think it's more a logical problem.Please feel free to copy my code to your console app and try this.When you debug/run always keep pressing 1 - for easy sudoku (so machine will give you more revealed numbers) .... and i asked myself WHAT IF I WANNA WHOLE TABLE REVEALED ... OK i will put under condition part next

    while(true) // second while
    {
          //condition
          if(numberOfEntered == 81) break;
          //and everything like in my code upper :D
          .....
    }

    here he will ask us what level we want to "play" but he will, no matter which lvl we choose, fill grid with 81 field.(cuz Sudoku grid has 81 field 9x9 = 81 .... it's obvious that i wouldn't ask for help if eror "; expected" shows .... you should take a look at my code to understand what is a prob

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    you should take a look at my code to understand what is a prob
    The idea is to help you look at the code to understand what the problem is, not to fix it for you.



    it just STAYS in second while
    When is the second while supposed to stop? What is the condition? When does the condition change to a value to exit the loop? Use println to see the values of the variables to be sure they are being changed.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    Sure... i'll try that so we can see where the problem is ..... and one more question, if i need random integer in interval [0,8] am i right if i write these:

    int randominteger = (int)(Math.random() * 9);

    if this is right when i say (int) in front of double, how it transforms that double to int? (by simply removing everything after dot(,) or i don't know t'would be usefull to know)

    and for random integer in interval [1,9]

    int ranominteger2 =1 + (int)(Math.random()*9);

    i need to know is that right .... tnx anyway

  9. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Good people what's wrong with my sudoku generator[newbie needs help]

    Quote Originally Posted by Achtung View Post
    ...
    int ranominteger2 =1 + (int)(Math.random()*9);

    i need to know is that right ...
    It's always OK to ask, but why not get into a mode of cranking out little test programs to verify your understanding of things like this? Might be faster than posting a request and waiting for a helpful response. And getting the extra practice of writing quickies like this can't possibly be bad, can it?

    public class Z {
     
        public static void main(String [] args) {
     
            int xmax = 0;                   // Start it off very small
            int xmin = Integer.MAX_VALUE;   // Start it off very large
            int num = 10000; // You don't really need this many, but...
     
            for (int i = 0; i < num; i++) {
                int x = (int)(Math.random() * 9) + 1; 
                if (x > xmax) {
                    xmax = x;
                }
                if (x < xmin) {
                    xmin = x;
                }
            }
            System.out.println("For " + num + " random deviates: " +
                                "xmin = " + xmin + 
                                ", xmax = " + xmax);
        } // End main()
    } // End class definition

    Output:

    For 10000 random deviates: xmin = 1, xmax = 9
    Last edited by Zaphod_b; October 21st, 2012 at 11:20 AM.

Similar Threads

  1. Sudoku: wrong output?
    By lisa92 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 15th, 2012, 12:10 PM
  2. whats wrong? help to newbie
    By ribhoo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 28th, 2011, 03:23 PM
  3. Hi people!
    By beer-in-box in forum Member Introductions
    Replies: 8
    Last Post: June 1st, 2011, 03:13 PM
  4. Replies: 0
    Last Post: January 25th, 2011, 12:24 AM
  5. Hello People
    By mrunal.cavale in forum Member Introductions
    Replies: 4
    Last Post: September 11th, 2009, 03:20 AM