# Best source for sudoku in java



## GSquadron (May 13, 2011)

Hi guys!
I am trying to do the java game Sudoku. This is a homework, but i need to see a source code in order to be based on that. I have googled a lot, but all seem very bad programmed. Is there any source code for the Java Sudoku generator without any errors?


----------



## zehpavora (May 13, 2011)

IMHO, and I'm a second-year programmer, I think the bad coded sources are the best ones to look into.

 Since they're bad, you can train your eye to find those bad segments and correct them.
 Once corrected, you'll have not only a very good source code, but you'll improve your personal garbage-collector (pun intended).


----------



## temp02 (May 13, 2011)

Trying to fix the code is useless, unless you know exactly whats wrong with it and how to fix it which is kinda hard since you didn't write it, I would take a look at the sudoku algorithms before looking at any code (but I guess thats just me, since I don't really play the game).
Irregardless, here is some code for the puzzle generator, it's not in Java though, but once you understand how to generate the puzzle the port should be pretty easy (look at SudoGen.vb, open it on gVim for instance).
Good luck.


----------



## zehpavora (May 13, 2011)

Well, you have a point, temp02. I supposed the code he found is not big, therefore some code correction wouldn't hurt. 

I work with software maintenance, so we do a lot of coding correction. I find that really useful, but only if you have enough patience.


----------



## GSquadron (May 13, 2011)

OMG the code for doing sudoku is at LEAST 5 pages!
I don't even know how to program 1 page man. 
How can i find the errors in 5 pages when i don't know all commands he uses? 
Like the import java.visual.jwt??? It says error even on the first row! 
Also i have seen the algorithm from codeproject temp02 sent me 3 days ago


----------



## Kreij (May 13, 2011)

Write your own then. 
Here is a paper that explains how to generate a valid Sudoku solution, using latin squares, quite easily (Section 8.3.1)


----------



## GSquadron (May 14, 2011)

So i try to understand java language and all i have done so far is only this code:

package sudokugame;
public class Sudoku {
public static void Sudoku(){
}
Public Randomnumbers (int_random a[], int_random b[]);
Public int i[a]*;
}

for (int a = 1; a <= 9; ++a) {
int i [a]+= a+1;
}
for (int b = 1; b <= 9; ++b) {
int i [a]+= b+1;
return i;

int rows = a;
int columns = b;
int box = i[a/3][b/3];
}

I know i have done a lot of errors :S, but anyone can help me out here???*


----------



## scope54 (May 14, 2011)

```
public class Sudoku 
{
    private int[][] i= new int[9][9];
    public static void Sudoku(){}
    public int Randomnumbers (int[] randomA, int[] randomB)
    {
        return 0;
    }
      
    public void setup()
    {
        final int ROWS = i.length;
        final int COLS = i[0].length;
        for (int r = 0; r < ROWS; r++)
        {
            for (int c = 0; c < COLS; c++)
            {
                i[r][c] += r+1;
            }
        }
       int[][] box = new int[ROWS/3][COLS/3];
    }
}
```

that compiles


----------



## GSquadron (May 14, 2011)

When i run that code is says no main class found


----------



## Kreij (May 14, 2011)

That's because you need a "main" class. It's the entry point to the program.
When the program is compiled, the compiler needs to know where the entry point is otherwise there in no way of knowing what to start with when the application is launched.
All programming languages require this.


----------



## GSquadron (May 15, 2011)

Man the class is already done!


----------



## Kreij (May 15, 2011)

No ... you need to have a method named "main". Linky


----------



## zehpavora (May 16, 2011)

I don't want to sound disrespectful, but do you have any clue about java?
(Ive read that sentence a lot, I couldn't make it sound better)

Netbeans creates a main class for you when you start a new project, if you want (I love Netbeans).

If you really want to code in Java, have a quick look at Oracle's Tutorial Database.
They are really awesome and easy to understand.

scope54's code will not "output" anything, but it'll run.

Get yourself a piece of paper and write down the logic of the program. Not the code itself, but the steps the program has to follow.

With this piece of paper, you will have a logical path, which is by far the most useful thing you can have when you start coding.


----------



## Disparia (Jun 5, 2011)

Kreij said:


> Write your own then.
> Here is a paper that explains how to generate a valid Sudoku solution, using latin squares, quite easily (Section 8.3.1)



Meant to take a crack at it earlier. Here's my PHP implementation using the squares method outlined in that paper. Easy indeed!


```
# Squares;
$s = array(
	'012201120', '201120012', '120012201',
	'021102210', '102210021', '210021102',
	'201012120', '120201012', '102021210',
	'210102021', '021210102', '012120201'
);
shuffle($s);

# Board;
$b = '';
for ($x = 0; $x <= 6; $x+= 3) {
	for ($y = 0; $y < 9; ++$y) {
		for ($z = 0; $z < 3; ++$z) {
			$b.= $s[9]{$y} * 3 + $s[$y]{$x + $z} + 1;
		}
	}
}

// $b is now an 81 character string with values to be used in a Sudoku board.
```

Started a loop shuffling the array and inserting the results into a database. Up to 500,000 loops you'll likely get a unique board. After that, it starts becoming more likely to get a dupe.

After 500,000 loops:
1 : 492434
2 : 3783

After 1,000,000 loops: 
2 : 402749
1 : 176344
3 : 3166
4 : 2165

Ran this test 3 times and got roughly the same results each time.


----------

