Advertisement
Guest User

Sudoku

a guest
Jan 1st, 2014
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.GridLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.BorderFactory;
  8. import javax.swing.JButton;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JFrame;
  11.  
  12. @SuppressWarnings("serial")
  13. public class Sudoku extends javax.swing.JFrame {
  14.  
  15. public static boolean goAgain = false;
  16. public static boolean[][][] possibilities = new boolean[9][9][9];
  17. public static boolean[] noPossibilities = {false, false, false, false, false, false, false, false, false};
  18. public static int[][] definite = new int[9][9];
  19.  
  20. public static JTextArea[][] field = new JTextArea[9][9];
  21. public static Font font = new Font("Veranda", Font.BOLD, 48);
  22.  
  23. public static void initGUI() {
  24.  
  25. JFrame frame = new JFrame();
  26. frame.setTitle("Sudoku");
  27. frame.setLocation(500, 0);
  28. frame.setLayout(new GridLayout(10,9));
  29. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  30.  
  31. //Initiate faces
  32. for( int j = 0; j < 9; j++) {
  33. for( int i = 0; i < 9; i++) {
  34. field[i][j] = new JTextArea();
  35. field[i][j].setVisible(true);
  36. field[i][j].setFont(font);
  37. field[i][j].setText("");
  38. field[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
  39. frame.add(field[i][j]);
  40. if((i<=2&&(j<=2||j>=6))||(i>=6&&(j<=2||j>=6))||((i>2&&i<6)&&(j>2&&j<6))) {
  41. field[i][j].setBackground(Color.LIGHT_GRAY);
  42. }
  43.  
  44. }
  45. }
  46.  
  47. //Initiate buttons
  48. JButton solveMe = new JButton("Generate");
  49. JButton resetMe = new JButton("Reset");
  50. JButton genSudoku = new JButton("New");
  51.  
  52. solveMe.addActionListener(new ActionListener() {
  53. @Override
  54. public void actionPerformed(ActionEvent e) {
  55. for( int j = 0; j < 9; j++) {
  56. for( int i = 0; i < 9; i++) {
  57. if(!field[i][j].getText().isEmpty()) {
  58. String numberString = field[i][j].getText();
  59. definite[i][j] = Integer.parseInt(numberString);
  60. } else {
  61. definite[i][j] = 0;
  62. }
  63. }
  64. }
  65. generateSudoku(definite);
  66. }
  67. });
  68.  
  69. resetMe.addActionListener(new ActionListener() {
  70. @Override
  71. public void actionPerformed(ActionEvent arg0) {
  72. for( int j = 0; j < 9; j++) {
  73. for( int i = 0; i < 9; i++) {
  74. field[i][j].setText("");
  75. definite[i][j] = 0;
  76. }
  77. }
  78. }
  79. });
  80.  
  81. frame.add(solveMe);
  82. frame.add(resetMe);
  83. frame.add(genSudoku);
  84. frame.setSize(650,750);
  85. frame.setVisible(true);
  86. }
  87.  
  88. public static void getPossibilities() {
  89. boolean[] noPossibilities = {false, false, false, false, false, false, false, false, false};
  90. for( int j = 0; j < 9; j++) {
  91. for( int i = 0; i < 9; i++) {
  92. if(definite[i][j] == 0) {
  93. boolean[] defaultPossibilities = {true, true, true, true, true, true, true, true, true};
  94. possibilities[i][j] = defaultPossibilities;
  95. for(int x = 0; x < 9; x++) {
  96. if(definite[x][j] != 0) {
  97. possibilities[i][j][definite[x][j]-1] = false;
  98. }
  99. }
  100. for(int y = 0; y < 9; y++) {
  101. if(definite[i][y] != 0) {
  102. possibilities[i][j][definite[i][y]-1] = false;
  103. }
  104. }
  105. if(i <= 2) {
  106. if(j <= 2) {
  107. for(int x = 0; x < 3; x++) {
  108. for(int y = 0; y < 3; y++) {
  109. if(definite[x][y] != 0) {
  110. possibilities[i][j][definite[x][y]-1] = false;
  111. }
  112. }
  113. }
  114. } else if(j > 2 && j < 6) {
  115. for(int x = 0; x < 3; x++) {
  116. for(int y = 3; y < 6; y++) {
  117. if(definite[x][y] != 0) {
  118. possibilities[i][j][definite[x][y]-1] = false;
  119. }
  120. }
  121. }
  122. } else if(j > 5) {
  123. for(int x = 0; x < 3; x++) {
  124. for(int y = 6; y < 9; y++) {
  125. if(definite[x][y] != 0) {
  126. possibilities[i][j][definite[x][y]-1] = false;
  127. }
  128. }
  129. }
  130. }
  131. } else if(i > 2 && i < 6) {
  132. if(j <= 2) {
  133. for(int x = 3; x < 6; x++) {
  134. for(int y = 0; y < 3; y++) {
  135. if(definite[x][y] != 0) {
  136. possibilities[i][j][definite[x][y]-1] = false;
  137. }
  138. }
  139. }
  140. } else if(j > 2 && j < 6) {
  141. for(int x = 3; x < 6; x++) {
  142. for(int y = 3; y < 6; y++) {
  143. if(definite[x][y] != 0) {
  144. possibilities[i][j][definite[x][y]-1] = false;
  145. }
  146. }
  147. }
  148. } else if(j > 5) {
  149. for(int x = 3; x < 6; x++) {
  150. for(int y = 6; y < 9; y++) {
  151. if(definite[x][y] != 0) {
  152. possibilities[i][j][definite[x][y]-1] = false;
  153. }
  154. }
  155. }
  156. }
  157. } else if(i > 5) {
  158. if(j <= 2) {
  159. for(int x = 6; x < 9; x++) {
  160. for(int y = 0; y < 3; y++) {
  161. if(definite[x][y] != 0) {
  162. possibilities[i][j][definite[x][y]-1] = false;
  163. }
  164. }
  165. }
  166. } else if(j > 2 && j < 6) {
  167. for(int x = 6; x < 9; x++) {
  168. for(int y = 3; y < 6; y++) {
  169. if(definite[x][y] != 0) {
  170. possibilities[i][j][definite[x][y]-1] = false;
  171. }
  172. }
  173. }
  174. } else if(j > 5) {
  175. for(int x = 6; x < 9; x++) {
  176. for(int y = 6; y < 9; y++) {
  177. if(definite[x][y] != 0) {
  178. possibilities[i][j][definite[x][y]-1] = false;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. } else {
  185. possibilities[i][j] = noPossibilities;
  186. }
  187. }
  188. }
  189. }
  190.  
  191. public static void fillIn(int i, int j) {
  192. int count = 0;
  193. int value = 0;
  194. for(int n = 0; n < 9; n++) {
  195. if(possibilities[i][j][n]) {
  196. count++;
  197. value = n;
  198. }
  199. }
  200. if(count != 1) {
  201. value = 0;
  202. count = 0;
  203. } else {
  204. possibilities[i][j] = noPossibilities;
  205. value += 1;
  206. definite[i][j] = value;
  207. field[i][j].setText(""+value);
  208. System.out.println("Value: "+value);
  209. value = 0;
  210. count = 0;
  211. goAgain = true;
  212. }
  213. }
  214.  
  215. public static void fillIn2() {
  216. getPossibilities();
  217. while(goAgain) {
  218. getPossibilities();
  219. goAgain = false;
  220. for(int i = 0; i < 9; i++) {
  221. for(int j = 0; j < 9; j++) {
  222. fillIn(i, j);
  223. }
  224. }
  225. }
  226. }
  227.  
  228. public static void fillIn3() {
  229. getPossibilities();
  230. boolean[][][] goHereHori = new boolean[9][9][9];
  231. boolean[][][] goHereVert = new boolean[9][9][9];
  232. boolean[][][] goHereGrid = new boolean[9][9][9];
  233. for(int a = 0; a < 9; a++) {
  234. for(int b = 0; b < 9; b++) {
  235. for(int c = 0; c < 9; c++) {
  236. goHereHori[a][b][c] = true;
  237. goHereVert[a][b][c] = true;
  238. goHereGrid[a][b][c] = true;
  239. }
  240. }
  241. }
  242. for(int i = 0; i < 9; i++) {
  243. for(int j = 0; j < 9; j++) {
  244. for(int n = 0; n < 9; n++) {
  245.  
  246. if(possibilities[i][j][n]) {
  247. for(int x = 0; x < 9; x++) {
  248. if(possibilities[x][j][n] && x != i) {
  249. goHereHori[i][j][n] = false;
  250. }
  251. }
  252.  
  253. for(int y = 0; y < 9; y++) {
  254. if(possibilities[i][y][n] && y != j) {
  255. goHereVert[i][j][n] = false;
  256. }
  257. }
  258.  
  259. if(j < 3) {
  260. if(i < 3) {
  261. for(int x = 0; x < 3; x++) {
  262. for(int y = 0; y < 3; y++) {
  263. if(possibilities[x][y][n] && !(y == j && x == i)) {
  264. goHereGrid[i][j][n] = false;
  265. }
  266. }
  267. }
  268. } else if(i < 6 && i > 2) {
  269. for(int x = 3; x < 6; x++) {
  270. for(int y = 0; y < 3; y++) {
  271. if(possibilities[x][y][n] && !(y == j && x == i)) {
  272. goHereGrid[i][j][n] = false;
  273. }
  274. }
  275. }
  276. } else {
  277. for(int x = 6; x < 9; x++) {
  278. for(int y = 0; y < 3; y++) {
  279. if(possibilities[x][y][n] && !(y == j && x == i)) {
  280. goHereGrid[i][j][n] = false;
  281. }
  282. }
  283. }
  284. }
  285. } else if(j < 6 && j > 2) {
  286. if(i < 3) {
  287. for(int x = 0; x < 3; x++) {
  288. for(int y = 3; y < 6; y++) {
  289. if(possibilities[x][y][n] && !(y == j && x == i)) {
  290. goHereGrid[i][j][n] = false;
  291. }
  292. }
  293. }
  294. } else if(i < 6 && i > 2) {
  295. for(int x = 3; x < 6; x++) {
  296. for(int y = 3; y < 6; y++) {
  297. if(possibilities[x][y][n] && !(y == j && x == i)) {
  298. goHereGrid[i][j][n] = false;
  299. }
  300. }
  301. }
  302. } else {
  303. for(int x = 6; x < 9; x++) {
  304. for(int y = 3; y < 6; y++) {
  305. if(possibilities[x][y][n] && !(y == j && x == i)) {
  306. goHereGrid[i][j][n] = false;
  307. }
  308. }
  309. }
  310. }
  311. } else {
  312. if(i < 3) {
  313. for(int x = 0; x < 3; x++) {
  314. for(int y = 6; y < 9; y++) {
  315. if(possibilities[x][y][n] && !(y == j && x == i)) {
  316. goHereGrid[i][j][n] = false;
  317. }
  318. }
  319. }
  320. } else if(i < 6 && i > 2) {
  321. for(int x = 3; x < 6; x++) {
  322. for(int y = 6; y < 9; y++) {
  323. if(possibilities[x][y][n] && !(y == j && x == i)) {
  324. goHereGrid[i][j][n] = false;
  325. }
  326. }
  327. }
  328. } else {
  329. for(int x = 6; x < 9; x++) {
  330. for(int y = 6; y < 9; y++) {
  331. if(possibilities[x][y][n] && !(y == j && x == i)) {
  332. goHereGrid[i][j][n] = false;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. } else {
  339. goHereHori[i][j][n] = false;
  340. goHereVert[i][j][n] = false;
  341. goHereGrid[i][j][n] = false;
  342.  
  343. }
  344. }
  345. }
  346. }
  347.  
  348. for(int i = 0; i < 9; i++) {
  349. for(int j = 0; j < 9; j++) {
  350. for(int n = 0; n < 9; n++) {
  351. System.out.println("goHere "+i+" "+j+" "+n+" = "+(goHereVert[i][j][n] || goHereHori[i][j][n] || goHereGrid[i][j][n]));
  352. if(goHereVert[i][j][n] || goHereHori[i][j][n] || goHereGrid[i][j][n]) {
  353. definite[i][j] = n+1;
  354. field[i][j].setText(""+(n+1));
  355. possibilities[i][j] = noPossibilities;
  356. goAgain = true;
  357. }
  358. }
  359. }
  360. }
  361.  
  362. }
  363.  
  364. public static void fillIn4() {
  365. getPossibilities();
  366. while(goAgain) {
  367. getPossibilities();
  368. goAgain = false;
  369. fillIn3();
  370. }
  371. }
  372.  
  373. public static void generateSudoku(int[][] num) {
  374. getPossibilities();
  375. for(int i = 0; i < 9; i++) {
  376. for(int j = 0; j < 9; j++) {
  377. fillIn(i, j);
  378. }
  379. }
  380. //FIX THIS.
  381. fillIn2();
  382. fillIn3();
  383. fillIn4();
  384. }
  385.  
  386. public static void main(String[] args) {
  387. initGUI();
  388. }
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement