import java.awt.Font; import java.io.File; import java.util.Arrays; import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** * * * @author Bill Kraynek */ public class TestSudoku { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { int[][] puzzle = new int[9][9]; int[][] solution; Scanner fileScan = new Scanner(new File("sudoku.data")); while (fileScan.hasNext()) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { puzzle[i][j] = fileScan.nextInt(); }// end for }// end for String grid = getGrid("\n The puzzle is \n\n", puzzle); solution = SolveSudoku.getSolution(puzzle); if (solution != null) { grid += getGrid(" The solution is \n\n", solution); } else { grid += " No Solution"; }// end if displayGrid(grid); }// end while puzzle = new int[9][9]; String grid = getGrid("\n The puzzle is \n\n", puzzle); solution = SolveSudoku.getSolution(puzzle); grid += getGrid(" The solution is \n\n", solution); displayGrid(grid); }// end main static String getGrid(String title, int[][] grid) { String out = title; for (int i = 0; i < 9; i++) { out += (i == 3 || i == 6) ? " -----------------------\n" : ""; out += " "; for (int j = 0; j < 9; j++) { out += (j % 3 == 0 ? " |" : "") + (grid[i][j] == 0 ? "_|" : grid[i][j] + "|"); }// end for out += "\n"; }// end for out += "\n"; return out; } static void displayGrid(String grid) { JTextArea outArea = new JTextArea(30, 30); outArea.setText(grid); outArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20)); JOptionPane.showMessageDialog(null, new JScrollPane(outArea)); } }