Guest User

nubela

a guest
Sep 21st, 2009
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. package utils;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. * This class would auto generate a 9x9 sudoku puzzler
  7. */
  8. public class PuzzleGenerator {
  9.  
  10. int[][] grid_array = new int[9][9];
  11. int[][] fixed_slots = new int[9][9];
  12.  
  13. public static void main(String[] args) {
  14.  
  15. }
  16.  
  17. public void generatePuzzle() {
  18.  
  19. /*
  20. * To generate a puzzle, we need to create a puzzle with numbers placed
  21. * such that there is only a single solution.
  22. *
  23. * 1) Randomly generate a slot, find the possible numbers to be placed
  24. * on it.
  25. *
  26. * 2) Randomly choose a number and place on it, find the number of
  27. * possible solutions, if == 1 , we got a puzzle.
  28. *
  29. * 3) Repeat 1->2 till we find the puzzle.
  30. */
  31. int num_of_sltns;
  32. do {
  33. num_of_sltns = numOfSolutions();
  34. if (num_of_sltns == 0) {
  35. fixed_slots = new int[9][9];
  36. } else if (num_of_sltns == 2) {
  37. int[] random_slot = generateRandomSlot();
  38. LinkedList<Integer> values_for_slot = possibleValuesInGrid(random_slot);
  39. int random_value = randomValueFromLinkedList(values_for_slot);
  40. fixed_slots[random_slot[0]][random_slot[1]] = random_value;
  41. }
  42. } while (num_of_sltns != 1);
  43. }
  44.  
  45. private int randomValueFromLinkedList(LinkedList<Integer> llist) {
  46. Random rand = new Random();
  47. int random_ptr = rand.nextInt(llist.size());
  48. return (int) llist.get(random_ptr);
  49. }
  50.  
  51. public int[] generateRandomSlot() {
  52. // generate a slot that isn't used.
  53. int[] ret = new int[2];
  54. Random rand = new Random();
  55. int x, y;
  56. do {
  57. x = rand.nextInt(9);
  58. y = rand.nextInt(9);
  59. } while (fixed_slots[x][y] == 0);
  60. ret[0] = x;
  61. ret[1] = y;
  62. return ret;
  63. }
  64.  
  65. public LinkedList<Integer> possibleValuesInGrid(int[] grid_posi) {
  66. return this.possibleValuesInGrid(grid_posi, fixed_slots);
  67. }
  68.  
  69. public LinkedList<Integer> possibleValuesInGrid(int[] grid_posi,
  70. int[][] fixed_slots) {
  71.  
  72. /*
  73. * we have 3 basic constraints.
  74. *
  75. * 1. unique in the 3x3 grid 2. unique in the row 3. unique in the
  76. * column
  77. */
  78.  
  79. LinkedList<Integer> avail = new LinkedList<Integer>();
  80. avail.add(1);
  81. avail.add(2);
  82. avail.add(3);
  83. avail.add(4);
  84. avail.add(5);
  85. avail.add(6);
  86. avail.add(7);
  87. avail.add(8);
  88. avail.add(9);
  89.  
  90. // minigrid
  91.  
  92. int x_offset = miniGridXOffset(getMiniGrid(grid_posi));
  93. int y_offset = miniGridYOffset(getMiniGrid(grid_posi));
  94.  
  95. int i = 0;
  96. int j = 0;
  97. while (i <= 2) {
  98. while (j <= 2) {
  99. if ((fixed_slots[x_offset + i][y_offset + j] != 0)
  100. && (avail
  101. .contains((Integer) fixed_slots[x_offset + i][y_offset
  102. + j])))
  103. avail.remove((Integer) fixed_slots[x_offset + i][y_offset
  104. + j]);
  105. j++;
  106. }
  107. i++;
  108. }
  109.  
  110. // row; y remains the same.
  111.  
  112. i = 0;
  113. while (i < 9) {
  114. if ((fixed_slots[i][grid_posi[1]] != 0)
  115. && (avail.contains((Integer) fixed_slots[i][grid_posi[1]])))
  116. avail.remove((Integer) fixed_slots[i][grid_posi[1]]);
  117. i++;
  118. }
  119.  
  120. // column; x remains the same
  121.  
  122. i = 0;
  123. while (i < 9) {
  124. if ((fixed_slots[grid_posi[0]][i] != 0)
  125. && (avail.contains((Integer) fixed_slots[grid_posi[0]][i]))) {
  126. avail.remove((Integer) fixed_slots[grid_posi[0]][i]);
  127. }
  128. i++;
  129. }
  130.  
  131. return avail;
  132.  
  133. }
  134.  
  135. public int miniGridXOffset(int x) {
  136. if ((x == 1) || (x == 4) || (x == 7)) {
  137. return 0;
  138. } else if ((x == 2) || (x == 5) || (x == 8)) {
  139. return 3;
  140. } else if ((x == 3) || (x == 6) || (x == 9)) {
  141. return 6;
  142. }
  143. return -1;
  144. }
  145.  
  146. public int miniGridYOffset(int x) {
  147. if ((x == 1) || (x == 2) || (x == 3)) {
  148. return 0;
  149. } else if ((x == 4) || (x == 5) || (x == 6)) {
  150. return 3;
  151. } else if ((x == 7) || (x == 8) || (x == 9)) {
  152. return 6;
  153. }
  154. return -1;
  155. }
  156.  
  157. public int getMiniGrid(int[] grid_posi) {
  158.  
  159. /*
  160. * Each grid contains 3x3 smaller mini grids.
  161. *
  162. * We get the grid_posi, and return the corresponding minigrids.
  163. */
  164.  
  165. // [1][2][3]
  166. // [4][5][6]
  167. // [7][8][9]
  168. if (grid_posi[0] <= 2) {
  169. // 1, 4, 7
  170. if (grid_posi[1] <= 2) {
  171. return 1;
  172. } else if (grid_posi[1] <= 5) {
  173. return 4;
  174. } else if (grid_posi[1] <= 8) {
  175. return 7;
  176. }
  177. } else if (grid_posi[0] <= 5) {
  178. // 2, 5, 8
  179. if (grid_posi[1] <= 2) {
  180. return 2;
  181. } else if (grid_posi[1] <= 5) {
  182. return 5;
  183. } else if (grid_posi[1] <= 8) {
  184. return 8;
  185. }
  186. } else if (grid_posi[0] <= 8) {
  187. // 3, 6, 9
  188. if (grid_posi[1] <= 2) {
  189. return 3;
  190. } else if (grid_posi[1] <= 5) {
  191. return 6;
  192. } else if (grid_posi[1] <= 8) {
  193. return 9;
  194. }
  195. }
  196. return 0;
  197. }
  198.  
  199. public int numOfSolutions() {
  200. // we only return 3 cases.
  201. // 0 for 0 sltns.
  202. // 1 for exactly 1 unique sltn.
  203. // 2 for more than 1.
  204. return 1;
  205. }
  206.  
  207. private boolean inFixedSlots(int[] slot) {
  208. if (fixed_slots[slot[0]][slot[1]] != 0)
  209. return true;
  210. return false;
  211. }
  212.  
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment