Advertisement
SenkuIshigami

Sudoku

Jan 7th, 2021
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.56 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Pos;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Alert;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.control.TextField;
  9. import javafx.scene.layout.*;
  10. import javafx.stage.FileChooser;
  11. import javafx.stage.Stage;
  12.  
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.lang.reflect.Field;
  16. import java.util.Arrays;
  17. import java.util.Scanner;
  18.  
  19. public class Main extends Application {
  20.     Button btn[][]=new Button[9][9];
  21.     private Field[][] fields;       // Array of fields.
  22.     private Pane[][] panels;      // Panels holding the fields.
  23.  
  24.     @Override
  25.     public void start(Stage primaryStage) throws Exception {
  26.         GridPane grid = new GridPane();
  27.         BorderPane root=new BorderPane();
  28.         int[][] b = new int[9][9];
  29.  
  30.         try {
  31.             File file = new File("game1.txt");
  32.             Scanner input=new Scanner(file);
  33.             int n = 0;
  34.             while (input.hasNextLine()) {
  35.                 while (n < 9) {
  36.                     String[] s = input.nextLine().split(" ");
  37.  
  38.                     for (int i = 0; i < 9; i++) {
  39.  
  40.                         b[n][i] = Integer.parseInt(s[i]);
  41.                         //      System.out.println(s[i]);
  42.                     }
  43.                     n++;
  44.                 }
  45.  
  46.             }
  47.  
  48.             // insert TextField into grid
  49.             for (int r = 0; r < 9; r++) {
  50.                 for (int c = 0; c < 9; c++) {
  51.  
  52.                     Button t = new Button();
  53.                     t = new Button(b[r][c] + "");
  54.                     btn[c][r] = t;
  55.                     btn[c][r].setMaxSize(25, 25);
  56.                    // btn[c][r].setFocusTraversable(false);
  57.                     grid.add(t, c, r );
  58.                 }
  59.  
  60.             }
  61.  
  62.  
  63.             // Clear played texts from user
  64.             Button clear = new Button("Clear");
  65.             clear.setOnAction(e -> {
  66.                 for (int r = 0; r < 9; r++) {
  67.                     for (int c = 0; c < 9; c++) {
  68.  
  69.                         Button t = new Button();
  70.  
  71.                         t = new Button(b[r][c] + "");
  72.                         // t.setAlignment(Pos.CENTER);
  73.                         btn[c][r] = t;
  74.                         btn[c][r].setMaxSize(25, 25);
  75.                         grid.add(t, c, r + 15);
  76.                     }
  77.                 }
  78.  
  79.             });
  80.  
  81.         } catch (FileNotFoundException e) {
  82.             e.printStackTrace();
  83.         }
  84.         // Clear played texts from user
  85.         Button  clear = new Button("Clear");
  86.         clear.setOnAction(e -> {
  87.             for (int r=0; r<9 ; r++) {
  88.                 for (int c = 0; c < 9; c++) {
  89.  
  90.                     Button t = new Button();
  91.  
  92.                     t = new Button(b[r][c]+"");
  93.                     // t.setAlignment(Pos.CENTER);
  94.                     btn[c][r]=t;
  95.                     btn[c][r].setMaxSize(25,25);
  96.                     grid.add(t,c,r+15);
  97.                 }
  98.             }
  99.  
  100.         });
  101.         Alert a = new Alert(Alert.AlertType.INFORMATION);
  102.  
  103.  
  104.         //Check Button
  105.         Button  Check = new Button("Check");
  106.         //Checking if the user solution is Correct.
  107.         Check.setOnAction(e->{
  108.             int userSolve[][]=new int[9][9];
  109.             for (int r=0; r<userSolve.length ; r++) {
  110.                 for (int c = 0; c < userSolve.length; c++) {
  111.  
  112.                     userSolve[r][c]=Integer.parseInt(btn[c][r].getText());
  113.                     //System.out.println(textField[c][r].getText());
  114.                 }
  115.             }
  116.  
  117.             solveSudoku(b, 9);
  118.  
  119.             if (Arrays.deepEquals(userSolve, b)) {
  120.  
  121.                 a.setTitle("Your Result");
  122.                 a.setHeaderText("You Won! \nCongrats.");
  123.                 a.setContentText("Your Answer is Correct.");
  124.                 a.show();
  125.                 System.out.println("true");
  126.             }
  127.             else {
  128.                 a.setTitle("Your Result");
  129.                 a.setHeaderText("You Lose!");
  130.                 a.setContentText("Your Answer is not Correct.");
  131.                 a.show();
  132.                 System.out.println("false");
  133.             }
  134.         });
  135.  
  136.         // New game button
  137.         Button newGame = new Button("New Game");
  138.  
  139.         newGame.setOnAction(e -> {
  140.  
  141.             try {
  142.                 FileChooser file1 = new FileChooser();
  143.                 File f2= file1.showOpenDialog(null);
  144.                 Scanner input1 = new Scanner(f2);
  145.                 int n1=0;
  146.                 while(input1.hasNextLine()) {
  147.                     while(n1<9) {
  148.                         String s1[]=input1.nextLine().split(",");
  149.  
  150.                         for (int i = 0; i < 9; i++) {
  151.  
  152.                             b[n1][i]=Integer.parseInt(s1[i]);
  153.                             System.out.println(s1[i]);
  154.                         }
  155.                         n1++;
  156.                     }
  157.  
  158.  
  159.                     for (int r=0; r<9 ; r++) {
  160.                         for (int c = 0; c < 9; c++) {
  161.  
  162.  
  163.  
  164.                             Button  t = new Button(b[r][c]+"");
  165.                             // t.setAlignment(Pos.CENTER);
  166.                             btn[c][r]=t;
  167.                             btn[c][r].setMaxSize(25,25);
  168.                             grid.add(t,c,r+15);
  169.                         }
  170.                     }
  171.                     primaryStage.setTitle("Sudoku - Time: 0");
  172.                 }
  173.             } catch (FileNotFoundException e2) {
  174.                 // TODO Auto-generated catch block
  175.                 e2.printStackTrace();
  176.             }
  177.         });
  178.  
  179. //Solving it.
  180.         Button Solve = new Button("Solve");
  181.  
  182.         Solve.setOnAction(e->{
  183.             solveSudoku(b,9);
  184.             for (int r=0; r<9 ; r++) {
  185.                 for (int c = 0; c < 9; c++) {
  186.  
  187.                     Button t = new Button();
  188.  
  189.                     t = new  Button(b[r][c]+"");
  190.                     // t.setAlignment(Pos.CENTER);
  191.                     btn[c][r]=t;
  192.                     btn[c][r].setMaxSize(25,25);
  193.                     grid.add(t,c,r+15);
  194.                 }
  195.             }
  196.  
  197.             //  timeline.stop();
  198.         });
  199.         grid.setAlignment(Pos.TOP_LEFT);
  200.         grid.setHgap(2);
  201.         grid.setVgap(3);
  202.  
  203.         /*HBox h1 = new HBox(newGame,Check, clear,Slove);
  204.         h1.setAlignment(Pos.BOTTOM_CENTER);
  205.         h1.setSpacing(30);*/
  206.  
  207.  
  208.         HBox h1 = new HBox(newGame,Check, clear,Solve);
  209.         h1.setAlignment(Pos.BOTTOM_CENTER);
  210.         h1.setSpacing(30);
  211.         VBox v = new VBox(grid,h1);
  212.         v.setSpacing(70);
  213.  
  214.  
  215.         root.setCenter(v);
  216.  
  217.         // Sets the scene to the BorderPane layout and links the CSS file
  218.         Scene scene = new Scene(root, 350, 450);
  219.         primaryStage.setScene(scene);
  220.         primaryStage.setTitle("Sudoku");
  221.         primaryStage.show();
  222.     }
  223.     public static boolean solveSudoku(int[][] board, int n)
  224.     {
  225.         int row = -1;
  226.         int col = -1;
  227.         boolean isEmpty = true;
  228.         for (int i = 0; i < n; i++)
  229.         {
  230.             for (int j = 0; j < n; j++)
  231.             {
  232.                 if (board[i][j] == 0)
  233.                 {
  234.                     row = i;
  235.                     col = j;
  236.  
  237.                     // We still have some remaining
  238.                     // missing values in Sudoku
  239.                     isEmpty = false;
  240.                     break;
  241.                 }
  242.             }
  243.             if (!isEmpty) {
  244.                 break;
  245.             }
  246.         }
  247.  
  248.         // No empty space left
  249.         if (isEmpty)
  250.         {
  251.             return true;
  252.         }
  253.  
  254.         // Else for each-row backtrack
  255.         for (int num = 1; num <= n; num++)
  256.         {
  257.             if (isSafe(board, row, col, num))
  258.             {
  259.                 board[row][col] = num;
  260.                 if (solveSudoku(board, n))
  261.                 {
  262.                     // print(board, n);
  263.                     return true;
  264.                 }
  265.                 else
  266.                 {
  267.                     // replace it
  268.                     board[row][col] = 0;
  269.                 }
  270.             }
  271.         }
  272.  
  273.         return false;
  274.     }
  275.  
  276.     public static boolean isSafe(int[][] board, int row, int col,int num)
  277.     {
  278.         // Row has the unique (row-clash)
  279.         for (int d = 0; d < board.length; d++)
  280.         {
  281.  
  282.             // Check if the number we are trying to
  283.             // place is already present in
  284.             // that row, return false;
  285.             if (board[row][d] == num) {
  286.                 return false;
  287.             }
  288.         }
  289.  
  290.         // Column has the unique numbers (column-clash)
  291.         for (int r = 0; r < board.length; r++)
  292.         {
  293.  
  294.             // Check if the number
  295.             // we are trying to
  296.             // place is already present in
  297.             // that column, return false;
  298.             if (board[r][col] == num)
  299.             {
  300.                 return false;
  301.             }
  302.         }
  303.  
  304.         // Corresponding square has
  305.         // unique number (box-clash)
  306.         int sqrt = (int)Math.sqrt(board.length);
  307.         int boxRowStart = row - row % sqrt;
  308.         int boxColStart = col - col % sqrt;
  309.  
  310.         for (int r = boxRowStart;
  311.              r < boxRowStart + sqrt; r++)
  312.         {
  313.             for (int d = boxColStart;
  314.                  d < boxColStart + sqrt; d++)
  315.             {
  316.                 if (board[r][d] == num)
  317.                 {
  318.                     return false;
  319.                 }
  320.             }
  321.         }
  322.  
  323.         // if there is no clash, it's safe
  324.         return true;
  325.     }
  326.  
  327.     public static void main(String[] args) {
  328.         launch(args);
  329.     }
  330. }
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement