# Thread: Sudoku randomizer help

1. Member
Join Date
Apr 2010
Posts
3
Rep Power
0

## Sudoku randomizer help

SOLVED
I used if else statements for each puzzle and that worked! Thanks anyway!

I've written some code in java that takes a Sudoku puzzle (where 0's represent the blanks) and solves the rest of the puzzle. Now I need to add to that a piece of code that can randomly select from 10 different Sudoku puzzles, either from within the same java file or by referencing a txt document, and then solve that randomly selected puzzles:

A rough idea for some code to randomize the selection would look something like this:
Java Code:
```import java.math.*;
import java.util.*;

public class Sudokurandom {

public static void main(String[] args) {
int puzzle1[][] = {
{ 2, 0, 0, 0, 4, 8, 9, 0, 0 },
{ 0, 0, 6, 5, 0, 0, 0, 0, 3 },
{ 0, 0, 9, 2, 6, 0, 4, 0, 0 },
{ 3, 6, 2, 8, 0, 0, 0, 9, 0 },
{ 0, 1, 0, 0, 9, 0, 0, 4, 0 },
{ 0, 9, 0, 0, 0, 7, 3, 1, 2 },
{ 0, 0, 1, 0, 3, 6, 7, 0, 0 },
{ 7, 0, 0, 0, 0, 1, 6, 0, 0 },
{ 0, 0, 4, 7, 8, 0, 0, 0, 9 },
};
int puzzle2[][] = {
{ 0, 0, 9, 0, 0, 5, 0, 0, 0 },
{ 0, 7, 3, 8, 6, 0, 5, 0, 0 },
{ 2, 0, 1, 3, 7, 0, 0, 8, 0 },
{ 0, 3, 0, 0, 0, 0, 0, 2, 0 },
{ 9, 2, 4, 6, 0, 1, 3, 7, 5 },
{ 0, 1, 0, 0, 0, 0, 0, 6, 0 },
{ 0, 4, 0, 0, 2, 6, 8, 0, 3 },
{ 0, 0, 6, 0, 3, 7, 2, 1, 0 },
{ 0, 0, 0, 5, 0, 0, 6, 0, 0 },
};
int puzzle3[][] = {
{ 5, 0, 0, 0, 7, 0, 0, 0, 1 },
{ 0, 9, 8, 0, 1, 5, 7, 0, 0 },
{ 0, 4, 1, 2, 0, 0, 5, 8, 6 },
{ 0, 8, 0, 0, 0, 0, 0, 0, 9 },
{ 0, 0, 9, 0, 2, 0, 8, 0, 0 },
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 },
{ 0, 1, 3, 0, 0, 8, 0, 2, 5 },
{ 0, 3, 0, 0, 9, 2, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 7, 9, 0 },
{ 0, 6, 0, 7, 0, 0, 1, 3, 8 },
};    //random puzzles to be called on for solving

int pNumber=(int) Math.ceil(Math.random()*10);
}
}```
I am stuck and I can't get the solver to work with the randomizer, any ideas? in pseudo code I was thinking I should try the following
>>> Select a random number between 1 - 10 (For example, say it selects 3)
>>> Combine that number with the word puzzle (For example "puzzle" + 3 = "puzzle3")
>>> set the variable "puzzle" to the randomly selected puzzle (e.g. puzzle = puzzle3)
>>> allow my previously created solver code to solve the Sudoku matrix that is being referenced by the variable "puzzle"

So far as I can tell, the issue is that setting the variable "puzzle" to the string "puzzle3" isn't actually referencing the matrix itself. If anyone can give me some advice on how to do this or even suggest a better way to randomly select a pre-existing puzzle, I'd appreciate it greatly.
Last edited by mochibon; 04-07-2010 at 07:19 AM. Reason: solved

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•