Advertisement
Guest User

SolutionCreator.java

a guest
Aug 3rd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. //SolutionCreator.java
  2. //Description: Was made to create a random 9x9 Sudoku solution
  3. import java.util.Random;
  4. import java.util.Date;
  5. public class SolutionCreator{
  6.     public int [][]solution;
  7.     private Random rand;
  8.     //Constructor
  9.     public SolutionCreator(){
  10.         rand = new Random();
  11.         solution = new int [9][9];
  12.     }
  13.     //Generates a new solution and writes it to public int solution
  14.     public void GenerateSolution(){
  15.         //choose a random simple generic solution of following choices:
  16.         Date d = new Date();
  17.         rand.setSeed(d.getTime());
  18.         switch (rand.nextInt(6)) {
  19.             case 0: int [][] s0 = {{1,2,3,4,5,6,7,8,9},
  20.                                    {4,7,9,1,2,8,6,5,3},
  21.                                    {8,6,5,7,9,3,2,4,1},
  22.                                    {3,9,4,6,7,1,5,2,8},
  23.                                    {5,1,2,9,8,4,3,6,7},
  24.                                    {7,8,6,5,3,2,9,1,4},
  25.                                    {2,4,7,3,1,5,8,9,6},
  26.                                    {6,3,8,2,4,9,1,7,5},
  27.                                    {9,5,1,8,6,7,4,3,2}};
  28.                     solution = s0;
  29.                     break;
  30.             case 1:int [][] s1 = {{1,2,3,4,5,6,7,8,9},
  31.                                   {4,5,9,8,7,1,2,3,6},
  32.                                   {8,7,6,2,3,9,1,4,5},
  33.                                   {3,9,4,6,1,2,5,7,8},
  34.                                   {5,8,2,7,9,3,6,1,4},
  35.                                   {6,1,7,5,4,8,3,9,2},
  36.                                   {2,3,1,9,6,4,8,5,7},
  37.                                   {7,4,8,1,2,5,9,6,3},
  38.                                   {9,6,5,3,8,7,4,2,1}};
  39.                     solution = s1;
  40.                     break;
  41.             case 2:int [][] s2 = {{1,2,3,4,5,6,7,8,9},
  42.                                   {7,8,6,2,9,3,5,1,4},
  43.                                   {9,5,4,7,8,1,6,2,3},
  44.                                   {2,3,1,5,4,9,8,6,7},
  45.                                   {4,6,8,1,7,2,3,9,5},
  46.                                   {5,7,9,6,3,8,1,4,2},
  47.                                   {3,1,2,9,6,5,4,7,8},
  48.                                   {6,4,5,8,2,7,9,3,1},
  49.                                   {8,9,7,3,1,4,2,5,6}};
  50.                     solution = s2;
  51.                     break;
  52.             case 3:int [][] s3 = {{1,2,3,4,5,6,7,8,9},
  53.                                   {7,4,6,9,2,8,1,3,5},
  54.                                   {9,8,5,1,3,7,4,6,2},
  55.                                   {4,3,1,8,7,2,9,5,6},
  56.                                   {6,7,9,5,1,3,2,4,8},
  57.                                   {8,5,2,6,4,9,3,7,1},
  58.                                   {2,9,8,7,6,4,5,1,3},
  59.                                   {3,1,4,2,8,5,6,9,7},
  60.                                   {5,6,7,3,9,1,8,2,4}};
  61.                     solution = s3;
  62.                     break;
  63.             case 4:int [][] s4 = {{1,2,3,4,5,6,7,8,9},
  64.                                   {4,7,8,2,9,1,6,5,3},
  65.                                   {6,9,5,3,7,8,4,1,2},
  66.                                   {3,1,2,7,6,4,8,9,5},
  67.                                   {8,5,6,1,3,9,2,4,7},
  68.                                   {9,4,7,8,2,5,3,6,1},
  69.                                   {2,6,9,5,4,7,1,3,8},
  70.                                   {5,3,1,6,8,2,9,7,4},
  71.                                   {7,8,4,9,1,3,5,2,6}};
  72.                     solution = s4;
  73.                     break;
  74.             default:int [][] s5 = {{1,2,3,4,5,6,7,8,9},
  75.                                    {6,7,8,3,2,9,5,1,4},
  76.                                    {9,4,5,8,1,7,2,6,3},
  77.                                    {2,1,6,9,4,5,8,3,7},
  78.                                    {3,9,7,6,8,2,1,4,5},
  79.                                    {5,8,4,7,3,1,6,9,2},
  80.                                    {4,6,2,5,9,8,3,7,1},
  81.                                    {7,5,9,1,6,3,4,2,8},
  82.                                    {8,3,1,2,7,4,9,5,6}};
  83.                     solution = s5;
  84.                     break;
  85.         }
  86.         //randomize solution
  87.         for(int i=0;i<rand.nextInt(51)+50;i++){
  88.             //swap numbers
  89.             int num1 = rand.nextInt(9)+1;
  90.             int num2 = rand.nextInt(9)+1;
  91.             while(num1==num2)num2 = rand.nextInt(9)+1;
  92.             for(int r=0;r<9;r++){
  93.                 for(int c=0;c<9;c++){
  94.                     if(solution[r][c]==num1)solution[r][c]=num2;
  95.                     else if(solution[r][c]==num2)solution[r][c]=num1;
  96.                 }
  97.             }
  98.             //column swapper
  99.             for(int c=0;c<3;c++){
  100.                 int c1 = rand.nextInt(3);
  101.                 int c2 = rand.nextInt(3);
  102.                 while(c1==c2)c2 = rand.nextInt(3);
  103.                 for(int r=0;r<9;r++){
  104.                     int tmp = solution[r][c1+c*3];
  105.                     solution[r][c1+c*3]=solution[r][c2+c*3];
  106.                     solution[r][c2+c*3]=tmp;
  107.                 }
  108.             }
  109.             //row swapper
  110.             for(int r=0;r<3;r++){
  111.                 if(rand.nextFloat()<.5){
  112.                     int r1 = rand.nextInt(3);
  113.                     int r2 = rand.nextInt(3);
  114.                     while(r1==r2)r2 = rand.nextInt(3);
  115.                     for(int c=0;c<9;c++){
  116.                         int tmp = solution[r1+r*3][c];
  117.                         solution[r1+r*3][c]=solution[r2+r*3][c];
  118.                         solution[r2+r*3][c]=tmp;
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.     }
  124.     //Prints the solution
  125.     public void PrintSolution(){
  126.         for(int r=0;r<9;r++){
  127.             for(int c=0;c<9;c++){
  128.                 System.out.print(solution[r][c]);
  129.                 if(c==2||c==5)System.out.print("|");
  130.             }
  131.             System.out.println();
  132.             if(r==2||r==5)System.out.println("---+---+---");
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement