Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solver {
- private int[][] sudoku;
- /**
- * Constructor
- * @param sudoku. Predefined with values.
- */
- public Solver(int[][] sudoku){
- this.sudoku = sudoku;
- }
- /**
- * Solves the Sudoku if possible. Traverses from left to right and top to bottom.
- * Checks each cell and enters the first empty one and tests numbers from 1-9.
- * If a number is allowed, solve is called again for the next empty cell. If no number
- * is allowed the method will backtrack to the previously assigned cell and try again.
- * This process is repeated recursively until all possible attempts have been tried.
- * @return True if solution is possible, else false.
- */
- public boolean solve() {
- for(int row=0;row<9;row++) {
- for(int col=0;col<9;col++) {
- if(sudoku[row][col]==0) {
- for(int number=1;number<=9;number++){
- if(checkPlacement(row,col, number)) {
- sudoku[row][col]=number;
- if (solve()) {
- return true;
- } else {
- sudoku[row][col]=0;
- }
- }
- }
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Checks if placement is allowed of specified int 1-9.
- * @param row row Coordinate
- * @param col col Coordinate
- * @param value value is the value of the int 1-9
- * @return Returns true if placement is allowed, else false
- */
- private boolean checkPlacement(int row, int col, int value){
- if (checkRow(row,col,value) && checkColumn(row,col,value) && checkRegion(row,col,value)){
- return true;
- }
- return false;
- }
- private boolean checkRow(int row, int col, int value){
- for (int i = 0; i<9 ; i++){ //Om det redan någonstans på raden finns value, return false
- if(sudoku[row][i] == value){
- return false;
- }
- }
- return true; //Om aldrig false uppfyllts, returnera true
- }
- private boolean checkColumn(int row, int col, int value){
- for (int i = 0; i<9 ; i++){ //Om det redan någonstans på kolumnen finns value, return false
- if(sudoku[i][col] == value){
- return false;
- }
- }
- return true; //Om aldrig false uppfyllts, returnera true
- }
- private boolean checkRegion(int row, int col, int value){
- int regionCol = col/3; //Bryter ner col och row i kvadrantvärden.
- int regionRow = row/3;
- int startCol = regionCol*3; //Bygger tillbaks informationen. start-ordinaterna ger topp-vänster värde i motsvarande kvadrant
- int startRow = regionRow*3;
- for(int irow = 0; irow<3; irow++){
- for(int icol = 0; icol<3; icol++){
- if(sudoku[startRow+irow][startCol+icol] == value){
- return false;
- }
- }
- }
- return true;
- }
- public int value(int row, int col){
- return sudoku[row][col];
- }
- public void displaySudoku()
- {
- for(int i=0;i<9;i++)
- {
- if(i%3==0 && i!=0)
- {
- System.out.println("----------------------------------\n");
- }
- for(int j=0;j<9;j++)
- {
- if(j%3==0 && j!=0)
- {
- System.out.print(" | ");
- }
- System.out.print(" " + sudoku[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println("\n\n__________________________________________\n\n");
- }
- public static void main(String[] args) {
- double startTime = System.nanoTime();
- int[][] test = new int[9][9];
- test [0][2]=1;
- test [3][6]=4;
- test [4][2]=9;
- test [3][4]=8;
- Solver s1 = new Solver(test);
- s1.solve();
- s1.displaySudoku();
- double endTime = System.nanoTime();
- double totalTime = (endTime - startTime)/(1000000000);
- System.out.println(totalTime + "s");
- }
- }
- import java.util.ArrayList;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.TilePane;
- import javafx.stage.Stage;
- /**
- *
- */
- public class SudokuGUI extends Application {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- // Skapar BP TF och TP
- primaryStage.setTitle("Sudoku");
- BorderPane BP = new BorderPane();
- TextField[][] TF = new TextField[9][9];
- TilePane TP = new TilePane();
- TP.setMaxSize(450, 450);
- // Lägger till knapparna
- HBox hbox = new HBox(80);
- Button Solve = new Button("Solve");
- Button Clear = new Button("Clear");
- Button Print = new Button("Print");
- hbox.getChildren().addAll(Solve,Clear,Print);
- for(int col = 0; col<9; col++){
- for(int row = 0; row<9; row++){
- TF[row][col] = new TextField("0");
- TF[row][col].setPrefSize(50, 50);
- TP.getChildren().add(TF[row][col]);
- }
- }
- BP.setCenter(TP);
- BP.setBottom(hbox);
- Scene scene = new Scene(BP,500,500);
- primaryStage.setScene(scene);
- primaryStage.show();
- Solve.setOnAction(Event-> {
- int[][] Sudoku = new int[9][9];
- for(int col = 0; col<9; col++){
- for(int row = 0; row<9; row++){
- Sudoku[row][col] = Integer.parseInt(TF[row][col].getText());
- }
- }
- Solver solve = new Solver(Sudoku);
- solve.solve();
- //solve.displaySudoku();
- for(int col = 0; col<9; col++){
- for(int row = 0; row<9; row++){
- TF[row][col].setText(Integer.toString(solve.value(row, col)));
- }
- }
- });
- Clear.setOnAction(Event-> {
- for(int col = 0; col<9; col++){
- for(int row = 0; row<9; row++){
- TF[row][col].setText("0");
- }
- }
- System.out.println("Cleared");
- });
- Print.setOnAction(Event-> {
- StringBuilder SB = new StringBuilder();
- for(int col = 0; col<9; col++){
- for(int row = 0; row<9; row++){
- SB.append(TF[row][col].getText());
- }
- }
- System.out.println(SB);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement