Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SolutionCreator.java
- //Description: Was made to create a random 9x9 Sudoku solution
- import java.util.Random;
- import java.util.Date;
- public class SolutionCreator{
- public int [][]solution;
- private Random rand;
- //Constructor
- public SolutionCreator(){
- rand = new Random();
- solution = new int [9][9];
- }
- //Generates a new solution and writes it to public int solution
- public void GenerateSolution(){
- //choose a random simple generic solution of following choices:
- Date d = new Date();
- rand.setSeed(d.getTime());
- switch (rand.nextInt(6)) {
- case 0: int [][] s0 = {{1,2,3,4,5,6,7,8,9},
- {4,7,9,1,2,8,6,5,3},
- {8,6,5,7,9,3,2,4,1},
- {3,9,4,6,7,1,5,2,8},
- {5,1,2,9,8,4,3,6,7},
- {7,8,6,5,3,2,9,1,4},
- {2,4,7,3,1,5,8,9,6},
- {6,3,8,2,4,9,1,7,5},
- {9,5,1,8,6,7,4,3,2}};
- solution = s0;
- break;
- case 1:int [][] s1 = {{1,2,3,4,5,6,7,8,9},
- {4,5,9,8,7,1,2,3,6},
- {8,7,6,2,3,9,1,4,5},
- {3,9,4,6,1,2,5,7,8},
- {5,8,2,7,9,3,6,1,4},
- {6,1,7,5,4,8,3,9,2},
- {2,3,1,9,6,4,8,5,7},
- {7,4,8,1,2,5,9,6,3},
- {9,6,5,3,8,7,4,2,1}};
- solution = s1;
- break;
- case 2:int [][] s2 = {{1,2,3,4,5,6,7,8,9},
- {7,8,6,2,9,3,5,1,4},
- {9,5,4,7,8,1,6,2,3},
- {2,3,1,5,4,9,8,6,7},
- {4,6,8,1,7,2,3,9,5},
- {5,7,9,6,3,8,1,4,2},
- {3,1,2,9,6,5,4,7,8},
- {6,4,5,8,2,7,9,3,1},
- {8,9,7,3,1,4,2,5,6}};
- solution = s2;
- break;
- case 3:int [][] s3 = {{1,2,3,4,5,6,7,8,9},
- {7,4,6,9,2,8,1,3,5},
- {9,8,5,1,3,7,4,6,2},
- {4,3,1,8,7,2,9,5,6},
- {6,7,9,5,1,3,2,4,8},
- {8,5,2,6,4,9,3,7,1},
- {2,9,8,7,6,4,5,1,3},
- {3,1,4,2,8,5,6,9,7},
- {5,6,7,3,9,1,8,2,4}};
- solution = s3;
- break;
- case 4:int [][] s4 = {{1,2,3,4,5,6,7,8,9},
- {4,7,8,2,9,1,6,5,3},
- {6,9,5,3,7,8,4,1,2},
- {3,1,2,7,6,4,8,9,5},
- {8,5,6,1,3,9,2,4,7},
- {9,4,7,8,2,5,3,6,1},
- {2,6,9,5,4,7,1,3,8},
- {5,3,1,6,8,2,9,7,4},
- {7,8,4,9,1,3,5,2,6}};
- solution = s4;
- break;
- default:int [][] s5 = {{1,2,3,4,5,6,7,8,9},
- {6,7,8,3,2,9,5,1,4},
- {9,4,5,8,1,7,2,6,3},
- {2,1,6,9,4,5,8,3,7},
- {3,9,7,6,8,2,1,4,5},
- {5,8,4,7,3,1,6,9,2},
- {4,6,2,5,9,8,3,7,1},
- {7,5,9,1,6,3,4,2,8},
- {8,3,1,2,7,4,9,5,6}};
- solution = s5;
- break;
- }
- //randomize solution
- for(int i=0;i<rand.nextInt(51)+50;i++){
- //swap numbers
- int num1 = rand.nextInt(9)+1;
- int num2 = rand.nextInt(9)+1;
- while(num1==num2)num2 = rand.nextInt(9)+1;
- for(int r=0;r<9;r++){
- for(int c=0;c<9;c++){
- if(solution[r][c]==num1)solution[r][c]=num2;
- else if(solution[r][c]==num2)solution[r][c]=num1;
- }
- }
- //column swapper
- for(int c=0;c<3;c++){
- int c1 = rand.nextInt(3);
- int c2 = rand.nextInt(3);
- while(c1==c2)c2 = rand.nextInt(3);
- for(int r=0;r<9;r++){
- int tmp = solution[r][c1+c*3];
- solution[r][c1+c*3]=solution[r][c2+c*3];
- solution[r][c2+c*3]=tmp;
- }
- }
- //row swapper
- for(int r=0;r<3;r++){
- if(rand.nextFloat()<.5){
- int r1 = rand.nextInt(3);
- int r2 = rand.nextInt(3);
- while(r1==r2)r2 = rand.nextInt(3);
- for(int c=0;c<9;c++){
- int tmp = solution[r1+r*3][c];
- solution[r1+r*3][c]=solution[r2+r*3][c];
- solution[r2+r*3][c]=tmp;
- }
- }
- }
- }
- }
- //Prints the solution
- public void PrintSolution(){
- for(int r=0;r<9;r++){
- for(int c=0;c<9;c++){
- System.out.print(solution[r][c]);
- if(c==2||c==5)System.out.print("|");
- }
- System.out.println();
- if(r==2||r==5)System.out.println("---+---+---");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement