• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Best source for sudoku in java

Joined
Dec 2, 2009
Messages
3,352 (0.59/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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?
 
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).
 
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.
 
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.
 
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! :P
Also i have seen the algorithm from codeproject temp02 sent me 3 days ago
 
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)
 
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???
 
Code:
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
 
When i run that code is says no main class found :(
 
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.
 
Last edited:
Man the class is already done!
 
No ... you need to have a method named "main". Linky
 
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.
 
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!

PHP:
# 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.
 
Back
Top