1 Attachment(s)
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 ^^
Attachment 1461
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.
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 :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 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 :D
//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 :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");
}
}
}
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.
Quote:
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.
Quote:
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)
Re: Good people what's wrong with my sudoku generator[newbie needs help]
OK ..... i read that announcements and i think i'm ready. :D
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
Code Java:
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) .....
Re: Good people what's wrong with my sudoku generator[newbie needs help]
What do you mean it simply crashes? Is there an error message?
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
Code Java:
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
Re: Good people what's wrong with my sudoku generator[newbie needs help]
Quote:
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.
Quote:
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.
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
Re: Good people what's wrong with my sudoku generator[newbie needs help]
Quote:
Originally Posted by
Achtung
...
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?
Code java:
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