// // SudokuApplet.java // SudokuApplet // // Created by Jack Wimberley on 4/6/08. // Copyright (c) 2008 __MyCompanyName__. All rights reserved. // // A simple signed Java applet // This implementation of Algorithm X owes a lot to Stan Chesnutt's code. // I learned a lot about how to do this by looking over his freely provided code, // basing my code on his (restricting it for only Sudoku puzzles). // Many thanks to Mr. Chesnutt. import java.awt.*; import java.util.*; import java.io.*; import java.applet.*; import javax.swing.*; import java.awt.event.*; class NoSolutionException extends Exception {} class NoUniqueSolutionException extends Exception{} public class SudokuApplet extends JApplet implements ActionListener{ public Container content; public JPanel panel; public JPanel[][] grid; public JTextField[][] info; public JButton solve; public JButton clear; public JButton reset; public JLabel dialog; public AlgorithmPOE poe; public AlgorithmDLX dlx; public int[][] data; public int[][] solution; public void init() { content = getContentPane(); panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); // LOOK AND FEEL try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch(Exception e){} // PUZZLE grid = new JPanel[3][3]; info = new JTextField[9][9]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { grid[i][j] = new JPanel(); grid[i][j].setLayout(new GridBagLayout()); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { int row = 3*j+y; int col = 3*i+x; info[row][col] = new JTextField(2); info[i][j].setEditable(true); c.gridx = x; c.gridy = y; c.ipadx = 0; c.ipady = 0; grid[i][j].add(info[row][col],c); } } c.gridx = i; c.gridy = j; c.ipadx = 8; c.ipady = 8; panel.add(grid[i][j],c); } } // Solve Button c.gridx = 0; c.gridy = 3; c.gridwidth = 1; solve = new JButton("Solve"); panel.add(solve, c); solve.addActionListener(this); // Reset Button c.gridx = 1; c.gridy = 3; c.gridwidth = 1; reset = new JButton("Reset"); panel.add(reset, c); reset.addActionListener(this); // Clear Button c.gridx = 2; c.gridy = 3; c.gridwidth = 1; clear = new JButton("Clear"); panel.add(clear, c); clear.addActionListener(this); // Dialog Box c.gridx = 0; c.gridy = 4; c.gridwidth = 3; dialog = new JLabel("Enter in your data..."); panel.add(dialog, c); // Create GUI content.add(panel); setContentPane(content); setVisible(true); // Sudoku Variables data = new int[9][9]; solution = new int[9][9]; for (int i = 0; i < 9; i++ ) { Arrays.fill(data[i], -1); Arrays.fill(solution[i], -1); } } public void actionPerformed(ActionEvent event) { JButton button = (JButton) event.getSource(); if (button == solve) { try { // Get Data addData(); // Do the Algorithms long start = System.currentTimeMillis(); AlgorithmPOE poe = new AlgorithmPOE(data); solution = poe.solve(); long stop = System.currentTimeMillis(); print(solution); dialog.setText((stop - start) + " ms"); } catch(NoSolutionException e) { dialog.setText("There is no solution to this puzzle."); } catch(NumberFormatException nfe) { dialog.setText("You must enter integers between 1 and 9."); } } if (button == clear) { for (int i = 0; i < 9; i++) { Arrays.fill(data[i], -1); Arrays.fill(solution[i], -1); for (int j = 0; j < 9; j++) { info[i][j].setText(""); } } dialog.setText("Enter in you data..."); } if (button == reset) { for (int i = 0; i < 9; i++) { Arrays.fill(solution[i], -1); for (int j = 0; j < 9; j++) { info[i][j].setText(""); } } print(data); dialog.setText("Back where you started!"); } } public void addData() throws NumberFormatException { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { String s = info[i][j].getText(); try { int num = Integer.parseInt(s); data[i][j] = num-1; } catch(NumberFormatException nfe) { if (!s.equals("")) { throw nfe; } } if (data[i][j] < -1 || data[i][j] > 8) { throw new NumberFormatException(); } } } } public void print(int[][] x) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (x[i][j] != -1) { info[i][j].setText((x[i][j]+1) + ""); } } } } }