Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4. public class SudokuTest1 {
  5.  
  6.     public static void main(String[] arg) throws FileNotFoundException {
  7.     JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
  8.     while (true) {
  9.     SudokuModel mod = new MySudokuModel();
  10.     String filename = null;
  11.     File f = null;
  12.     if (dialog.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
  13.         f = dialog.getSelectedFile();
  14.     else        System.exit(0);
  15.     Scanner sc = new Scanner(f);
  16.     String input = "";      
  17.     while (sc.hasNextLine())      
  18.         input += sc.nextLine() + '\n';    
  19.     System.out.println(f.getName() + ": " +  "\n" + input);        
  20.     try {        mod.setBoard(input);      
  21.     if (!mod.isUnique()) {          
  22.         System.out.println("Illegal Sudoku puzzle. Multiple solutions");        }      
  23.     else {          
  24.         mod.solve();              
  25.         System.out.println(f.getName() + " Solution: ");          
  26.         int[][] m = mod.getBoard();          
  27.         for (int i=0; i<9; i++) {        
  28.             for (int j=0; j<9; j++) {          
  29.                 int v = mod.getSquare(i,j);          
  30.                 if (v != m[i][j])            
  31.                     System.out.println("Error. Methods getBoard and getSquare return different values");        
  32.      if (v != 0)              
  33.          System.out.print(v);        
  34.      else                
  35.          System.out.print('.');            }      
  36.             System.out.println();          }    
  37.         }    
  38.     }            catch (IllegalArgumentException e) {    
  39.         System.out.println("Illegal Sudoku puzzle. " + e.getMessage());      }
  40.     System.out.println("========================================================");     }  
  41.     }
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement