Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.*;
- import javafx.stage.FileChooser;
- import javafx.stage.Stage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.lang.reflect.Field;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main extends Application {
- Button btn[][]=new Button[9][9];
- private Field[][] fields; // Array of fields.
- private Pane[][] panels; // Panels holding the fields.
- @Override
- public void start(Stage primaryStage) throws Exception {
- GridPane grid = new GridPane();
- BorderPane root=new BorderPane();
- int[][] b = new int[9][9];
- try {
- File file = new File("game1.txt");
- Scanner input=new Scanner(file);
- int n = 0;
- while (input.hasNextLine()) {
- while (n < 9) {
- String[] s = input.nextLine().split(" ");
- for (int i = 0; i < 9; i++) {
- b[n][i] = Integer.parseInt(s[i]);
- // System.out.println(s[i]);
- }
- n++;
- }
- }
- // insert TextField into grid
- for (int r = 0; r < 9; r++) {
- for (int c = 0; c < 9; c++) {
- Button t = new Button();
- t = new Button(b[r][c] + "");
- btn[c][r] = t;
- btn[c][r].setMaxSize(25, 25);
- // btn[c][r].setFocusTraversable(false);
- grid.add(t, c, r );
- }
- }
- // Clear played texts from user
- Button clear = new Button("Clear");
- clear.setOnAction(e -> {
- for (int r = 0; r < 9; r++) {
- for (int c = 0; c < 9; c++) {
- Button t = new Button();
- t = new Button(b[r][c] + "");
- // t.setAlignment(Pos.CENTER);
- btn[c][r] = t;
- btn[c][r].setMaxSize(25, 25);
- grid.add(t, c, r + 15);
- }
- }
- });
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- // Clear played texts from user
- Button clear = new Button("Clear");
- clear.setOnAction(e -> {
- for (int r=0; r<9 ; r++) {
- for (int c = 0; c < 9; c++) {
- Button t = new Button();
- t = new Button(b[r][c]+"");
- // t.setAlignment(Pos.CENTER);
- btn[c][r]=t;
- btn[c][r].setMaxSize(25,25);
- grid.add(t,c,r+15);
- }
- }
- });
- Alert a = new Alert(Alert.AlertType.INFORMATION);
- //Check Button
- Button Check = new Button("Check");
- //Checking if the user solution is Correct.
- Check.setOnAction(e->{
- int userSolve[][]=new int[9][9];
- for (int r=0; r<userSolve.length ; r++) {
- for (int c = 0; c < userSolve.length; c++) {
- userSolve[r][c]=Integer.parseInt(btn[c][r].getText());
- //System.out.println(textField[c][r].getText());
- }
- }
- solveSudoku(b, 9);
- if (Arrays.deepEquals(userSolve, b)) {
- a.setTitle("Your Result");
- a.setHeaderText("You Won! \nCongrats.");
- a.setContentText("Your Answer is Correct.");
- a.show();
- System.out.println("true");
- }
- else {
- a.setTitle("Your Result");
- a.setHeaderText("You Lose!");
- a.setContentText("Your Answer is not Correct.");
- a.show();
- System.out.println("false");
- }
- });
- // New game button
- Button newGame = new Button("New Game");
- newGame.setOnAction(e -> {
- try {
- FileChooser file1 = new FileChooser();
- File f2= file1.showOpenDialog(null);
- Scanner input1 = new Scanner(f2);
- int n1=0;
- while(input1.hasNextLine()) {
- while(n1<9) {
- String s1[]=input1.nextLine().split(",");
- for (int i = 0; i < 9; i++) {
- b[n1][i]=Integer.parseInt(s1[i]);
- System.out.println(s1[i]);
- }
- n1++;
- }
- for (int r=0; r<9 ; r++) {
- for (int c = 0; c < 9; c++) {
- Button t = new Button(b[r][c]+"");
- // t.setAlignment(Pos.CENTER);
- btn[c][r]=t;
- btn[c][r].setMaxSize(25,25);
- grid.add(t,c,r+15);
- }
- }
- primaryStage.setTitle("Sudoku - Time: 0");
- }
- } catch (FileNotFoundException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- });
- //Solving it.
- Button Solve = new Button("Solve");
- Solve.setOnAction(e->{
- solveSudoku(b,9);
- for (int r=0; r<9 ; r++) {
- for (int c = 0; c < 9; c++) {
- Button t = new Button();
- t = new Button(b[r][c]+"");
- // t.setAlignment(Pos.CENTER);
- btn[c][r]=t;
- btn[c][r].setMaxSize(25,25);
- grid.add(t,c,r+15);
- }
- }
- // timeline.stop();
- });
- grid.setAlignment(Pos.TOP_LEFT);
- grid.setHgap(2);
- grid.setVgap(3);
- /*HBox h1 = new HBox(newGame,Check, clear,Slove);
- h1.setAlignment(Pos.BOTTOM_CENTER);
- h1.setSpacing(30);*/
- HBox h1 = new HBox(newGame,Check, clear,Solve);
- h1.setAlignment(Pos.BOTTOM_CENTER);
- h1.setSpacing(30);
- VBox v = new VBox(grid,h1);
- v.setSpacing(70);
- root.setCenter(v);
- // Sets the scene to the BorderPane layout and links the CSS file
- Scene scene = new Scene(root, 350, 450);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Sudoku");
- primaryStage.show();
- }
- public static boolean solveSudoku(int[][] board, int n)
- {
- int row = -1;
- int col = -1;
- boolean isEmpty = true;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (board[i][j] == 0)
- {
- row = i;
- col = j;
- // We still have some remaining
- // missing values in Sudoku
- isEmpty = false;
- break;
- }
- }
- if (!isEmpty) {
- break;
- }
- }
- // No empty space left
- if (isEmpty)
- {
- return true;
- }
- // Else for each-row backtrack
- for (int num = 1; num <= n; num++)
- {
- if (isSafe(board, row, col, num))
- {
- board[row][col] = num;
- if (solveSudoku(board, n))
- {
- // print(board, n);
- return true;
- }
- else
- {
- // replace it
- board[row][col] = 0;
- }
- }
- }
- return false;
- }
- public static boolean isSafe(int[][] board, int row, int col,int num)
- {
- // Row has the unique (row-clash)
- for (int d = 0; d < board.length; d++)
- {
- // Check if the number we are trying to
- // place is already present in
- // that row, return false;
- if (board[row][d] == num) {
- return false;
- }
- }
- // Column has the unique numbers (column-clash)
- for (int r = 0; r < board.length; r++)
- {
- // Check if the number
- // we are trying to
- // place is already present in
- // that column, return false;
- if (board[r][col] == num)
- {
- return false;
- }
- }
- // Corresponding square has
- // unique number (box-clash)
- int sqrt = (int)Math.sqrt(board.length);
- int boxRowStart = row - row % sqrt;
- int boxColStart = col - col % sqrt;
- for (int r = boxRowStart;
- r < boxRowStart + sqrt; r++)
- {
- for (int d = boxColStart;
- d < boxColStart + sqrt; d++)
- {
- if (board[r][d] == num)
- {
- return false;
- }
- }
- }
- // if there is no clash, it's safe
- return true;
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement