Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. public class Solver {
  2. private int[][] sudoku;
  3.  
  4. /**
  5. * Constructor
  6. * @param sudoku. Predefined with values.
  7. */
  8. public Solver(int[][] sudoku){
  9. this.sudoku = sudoku;
  10. }
  11.  
  12.  
  13.  
  14.  
  15. /**
  16. * Solves the Sudoku if possible. Traverses from left to right and top to bottom.
  17. * Checks each cell and enters the first empty one and tests numbers from 1-9.
  18. * If a number is allowed, solve is called again for the next empty cell. If no number
  19. * is allowed the method will backtrack to the previously assigned cell and try again.
  20. * This process is repeated recursively until all possible attempts have been tried.
  21. * @return True if solution is possible, else false.
  22. */
  23. public boolean solve() {
  24.  
  25. for(int row=0;row<9;row++) {
  26.  
  27. for(int col=0;col<9;col++) {
  28.  
  29. if(sudoku[row][col]==0) {
  30.  
  31. for(int number=1;number<=9;number++){
  32.  
  33. if(checkPlacement(row,col, number)) {
  34.  
  35. sudoku[row][col]=number;
  36.  
  37. if (solve()) {
  38.  
  39. return true;
  40.  
  41. } else {
  42.  
  43. sudoku[row][col]=0;
  44.  
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. }
  51. }
  52. return true;
  53. }
  54.  
  55.  
  56.  
  57. /**
  58. * Checks if placement is allowed of specified int 1-9.
  59. * @param row row Coordinate
  60. * @param col col Coordinate
  61. * @param value value is the value of the int 1-9
  62. * @return Returns true if placement is allowed, else false
  63. */
  64. private boolean checkPlacement(int row, int col, int value){
  65. if (checkRow(row,col,value) && checkColumn(row,col,value) && checkRegion(row,col,value)){
  66. return true;
  67. }
  68. return false;
  69. }
  70. private boolean checkRow(int row, int col, int value){
  71. for (int i = 0; i<9 ; i++){ //Om det redan någonstans på raden finns value, return false
  72. if(sudoku[row][i] == value){
  73. return false;
  74. }
  75. }
  76. return true; //Om aldrig false uppfyllts, returnera true
  77.  
  78.  
  79. }
  80. private boolean checkColumn(int row, int col, int value){
  81. for (int i = 0; i<9 ; i++){ //Om det redan någonstans på kolumnen finns value, return false
  82. if(sudoku[i][col] == value){
  83. return false;
  84. }
  85. }
  86. return true; //Om aldrig false uppfyllts, returnera true
  87.  
  88. }
  89. private boolean checkRegion(int row, int col, int value){
  90. int regionCol = col/3; //Bryter ner col och row i kvadrantvärden.
  91. int regionRow = row/3;
  92. int startCol = regionCol*3; //Bygger tillbaks informationen. start-ordinaterna ger topp-vänster värde i motsvarande kvadrant
  93. int startRow = regionRow*3;
  94. for(int irow = 0; irow<3; irow++){
  95. for(int icol = 0; icol<3; icol++){
  96. if(sudoku[startRow+irow][startCol+icol] == value){
  97. return false;
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103.  
  104. public int value(int row, int col){
  105. return sudoku[row][col];
  106. }
  107.  
  108. public void displaySudoku()
  109. {
  110. for(int i=0;i<9;i++)
  111. {
  112. if(i%3==0 && i!=0)
  113. {
  114. System.out.println("----------------------------------\n");
  115. }
  116. for(int j=0;j<9;j++)
  117. {
  118. if(j%3==0 && j!=0)
  119. {
  120. System.out.print(" | ");
  121. }
  122. System.out.print(" " + sudoku[i][j] + " ");
  123.  
  124. }
  125.  
  126. System.out.println();
  127. }
  128. System.out.println("\n\n__________________________________________\n\n");
  129. }
  130.  
  131. public static void main(String[] args) {
  132.  
  133. double startTime = System.nanoTime();
  134.  
  135. int[][] test = new int[9][9];
  136. test [0][2]=1;
  137. test [3][6]=4;
  138. test [4][2]=9;
  139. test [3][4]=8;
  140. Solver s1 = new Solver(test);
  141. s1.solve();
  142. s1.displaySudoku();
  143.  
  144. double endTime = System.nanoTime();
  145. double totalTime = (endTime - startTime)/(1000000000);
  146. System.out.println(totalTime + "s");
  147.  
  148.  
  149. }
  150.  
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. import java.util.ArrayList;
  193.  
  194. import javafx.application.Application;
  195. import javafx.geometry.Pos;
  196. import javafx.scene.Node;
  197. import javafx.scene.Scene;
  198. import javafx.scene.control.Button;
  199. import javafx.scene.control.TextField;
  200. import javafx.scene.layout.BorderPane;
  201. import javafx.scene.layout.HBox;
  202. import javafx.scene.layout.TilePane;
  203. import javafx.stage.Stage;
  204.  
  205. /**
  206. *
  207. */
  208. public class SudokuGUI extends Application {
  209.  
  210. /**
  211. * @param args the command line arguments
  212. */
  213. public static void main(String[] args) {
  214. launch(args);
  215. }
  216.  
  217.  
  218.  
  219.  
  220. @Override
  221. public void start(Stage primaryStage) {
  222.  
  223. // Skapar BP TF och TP
  224. primaryStage.setTitle("Sudoku");
  225. BorderPane BP = new BorderPane();
  226. TextField[][] TF = new TextField[9][9];
  227. TilePane TP = new TilePane();
  228. TP.setMaxSize(450, 450);
  229.  
  230. // Lägger till knapparna
  231. HBox hbox = new HBox(80);
  232. Button Solve = new Button("Solve");
  233. Button Clear = new Button("Clear");
  234. Button Print = new Button("Print");
  235. hbox.getChildren().addAll(Solve,Clear,Print);
  236.  
  237. for(int col = 0; col<9; col++){
  238. for(int row = 0; row<9; row++){
  239. TF[row][col] = new TextField("0");
  240. TF[row][col].setPrefSize(50, 50);
  241. TP.getChildren().add(TF[row][col]);
  242. }
  243. }
  244.  
  245. BP.setCenter(TP);
  246. BP.setBottom(hbox);
  247. Scene scene = new Scene(BP,500,500);
  248. primaryStage.setScene(scene);
  249. primaryStage.show();
  250.  
  251.  
  252.  
  253.  
  254.  
  255. Solve.setOnAction(Event-> {
  256.  
  257. int[][] Sudoku = new int[9][9];
  258.  
  259. for(int col = 0; col<9; col++){
  260. for(int row = 0; row<9; row++){
  261. Sudoku[row][col] = Integer.parseInt(TF[row][col].getText());
  262. }
  263. }
  264.  
  265. Solver solve = new Solver(Sudoku);
  266. solve.solve();
  267. //solve.displaySudoku();
  268.  
  269. for(int col = 0; col<9; col++){
  270. for(int row = 0; row<9; row++){
  271. TF[row][col].setText(Integer.toString(solve.value(row, col)));
  272. }
  273. }
  274.  
  275.  
  276. });
  277.  
  278.  
  279.  
  280. Clear.setOnAction(Event-> {
  281. for(int col = 0; col<9; col++){
  282. for(int row = 0; row<9; row++){
  283. TF[row][col].setText("0");
  284. }
  285. }
  286. System.out.println("Cleared");
  287.  
  288.  
  289. });
  290.  
  291.  
  292. Print.setOnAction(Event-> {
  293. StringBuilder SB = new StringBuilder();
  294. for(int col = 0; col<9; col++){
  295. for(int row = 0; row<9; row++){
  296. SB.append(TF[row][col].getText());
  297. }
  298. }
  299. System.out.println(SB);
  300. });
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement