University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import java.io.File;
  2. import javafx.application.Application;
  3. import javafx.stage.Stage;
  4. import javafx.stage.FileChooser;
  5. import javafx.scene.Scene;
  6. import javafx.scene.paint.Color;
  7. import javafx.scene.text.Font;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.Alert;
  11. import javafx.scene.layout.HBox;
  12. import javafx.scene.layout.GridPane;
  13. import javafx.scene.layout.BorderPane;
  14. import javafx.scene.layout.AnchorPane;
  15. import javafx.scene.layout.Pane;
  16. import javafx.scene.layout.ColumnConstraints;
  17. import javafx.scene.layout.RowConstraints;
  18. import javafx.scene.input.KeyEvent;
  19. import javafx.scene.input.KeyCodeCombination;
  20. import javafx.scene.input.KeyCombination;
  21. import javafx.scene.input.KeyCode;
  22. import javafx.event.EventHandler;
  23. import javafx.event.ActionEvent;
  24. abstract class ActionHandler implements EventHandler<ActionEvent> {}
  25. abstract class KeyHandler implements EventHandler<KeyEvent> {}
  26. abstract class Util {
  27. static Brett loadBoard(Stage stage) {
  28. boolean loaded = false;
  29. Brett board = null;
  30. while (!loaded) {
  31. File f = getFile(stage);
  32. if (f == null)
  33. return null;
  34. try {
  35. board = new Brett(f);
  36. loaded = true;
  37. } catch (Exception ex) {
  38. error(ex.getMessage());
  39. }
  40. }
  41. return board;
  42. }
  43. static void error(String msg) {
  44. Alert alert = new Alert(Alert.AlertType.ERROR, msg);
  45. alert.showAndWait();
  46. }
  47. static File getFile(Stage stage) {
  48. FileChooser chooser = new FileChooser();
  49. return chooser.showOpenDialog(stage);
  50. }
  51. }
  52. class SolutionGrid {
  53. GridPane grid;
  54. SolutionGrid(Brett board, SudokuBeholder.Losning solution) {
  55. grid = new GridPane();
  56. grid.setMinSize(200, 200);
  57. // Add labels to grid
  58. int x = 0;
  59. int y = 0;
  60. for (SudokuBeholder.Losning.Verdi val: solution) {
  61. Label l;
  62. if (val.tall == -1) {
  63. l = new Label(" ");
  64. } else {
  65. l = new Label(""+val.tall);
  66. }
  67. l.setFont(Font.font("Sans-Serif", 16));
  68. if (val.original) {
  69. l.setTextFill(Color.web("red"));
  70. }
  71. // Rather ugly way of getting pretty lines
  72. String style = "-fx-border-style: solid;";
  73. style += "-fx-border-width: 3px;";
  74. String bColor = "-fx-border-color: ";
  75. bColor += "COLY ";
  76. bColor += (x == board.storrelse - 1 ? "COL " : "transparent ");
  77. bColor += (y == board.storrelse - 1 ? "COL " : "transparent ");
  78. bColor += "COLX ";
  79. if (x % board.antallBokserX == 0 && x != 0) {
  80. bColor = bColor.replace("COLX", "red");
  81. } else {
  82. bColor = bColor.replace("COLX", "grey");
  83. }
  84. if (y % board.antallBokserY == 0 && y != 0) {
  85. bColor = bColor.replace("COLY", "red");
  86. } else {
  87. bColor = bColor.replace("COLY", "grey");
  88. }
  89. bColor = bColor.replace("COL", "grey");
  90. style += bColor + ";";
  91. BorderPane bp = new BorderPane();
  92. bp.setStyle(style);
  93. bp.setCenter(l);
  94. grid.getChildren().add(bp);
  95. grid.setConstraints(bp, x, y);
  96. x += 1;
  97. if (x >= board.storrelse) {
  98. x = 0;
  99. y += 1;
  100. }
  101. }
  102. // Set constraints
  103. double percent = (1.0 / board.storrelse) * 100;
  104. for (int i = 0; i < board.storrelse; ++i) {
  105. ColumnConstraints cc = new ColumnConstraints();
  106. RowConstraints rc = new RowConstraints();
  107. cc.setPercentWidth(percent);
  108. rc.setPercentHeight(percent);
  109. grid.getColumnConstraints().add(cc);
  110. grid.getRowConstraints().add(rc);
  111. }
  112. }
  113. public Pane getPane() {
  114. return grid;
  115. }
  116. }
  117. class SolvedView {
  118. int index;
  119. Stage stage;
  120. Brett board;
  121. BorderPane root;
  122. SudokuBeholder sb;
  123. Label lblNumSolutions;
  124. Button btnPrev;
  125. Button btnNext;
  126. private void drawGrid() {
  127. Pane grid = new SolutionGrid(board, sb.hent(index)).getPane();
  128. root.setCenter(grid);
  129. lblNumSolutions.setText(
  130. "Solution "+(index+1)+"/"+sb.hentAntallLosninger()
  131. );
  132. }
  133. SolvedView(Stage stage, Brett board, SudokuBeholder sb) {
  134. index = 0;
  135. this.stage = stage;
  136. this.board = board;
  137. root = new BorderPane();
  138. this.sb = sb;
  139. // Menu
  140. HBox menu = new HBox(12);
  141. lblNumSolutions = new Label("");
  142. lblNumSolutions.setPrefHeight(30);
  143. menu.getChildren().add(lblNumSolutions);
  144. // Button prev
  145. btnPrev = new Button("Prev");
  146. btnPrev.setPrefHeight(30);
  147. if (sb.hentAntallLosninger() > 1) {
  148. menu.getChildren().add(btnPrev);
  149. }
  150. btnPrev.setOnAction(new ActionHandler() {
  151. public void handle(ActionEvent evt) {
  152. if (index < 1)
  153. return;
  154. index -= 1;
  155. drawGrid();
  156. }
  157. });
  158. // Button next
  159. btnNext = new Button("Next");
  160. btnNext.setPrefHeight(30);
  161. if (sb.hentAntallLosninger() > 1) {
  162. menu.getChildren().add(btnNext);
  163. }
  164. btnNext.setOnAction(new ActionHandler() {
  165. public void handle(ActionEvent evt) {
  166. if (index >= sb.hentAntallLosninger() - 1)
  167. return;
  168. index += 1;
  169. drawGrid();
  170. }
  171. });
  172. root.setTop(menu);
  173. drawGrid();
  174. }
  175. public void show() {
  176. stage.getScene().setRoot(root);
  177. stage.show();
  178. }
  179. }
  180. class UnsolvedView {
  181. Stage stage;
  182. BorderPane root;
  183. UnsolvedView(Stage stage, Brett board) {
  184. this.stage = stage;
  185. root = new BorderPane();
  186. // Menu
  187. HBox menu = new HBox(12);
  188. Button btnSolve = new Button("Solve");
  189. btnSolve.setPrefHeight(30);
  190. btnSolve.setOnAction(new ActionHandler() {
  191. @Override
  192. public void handle(ActionEvent ae) {
  193. SudokuBeholder sb = board.los();
  194. new SolvedView(stage, board, sb).show();
  195. }
  196. });
  197. menu.getChildren().add(btnSolve);
  198. root.setTop(menu);
  199. // Grid
  200. Pane grid = new SolutionGrid(board, board.tilLosning()).getPane();
  201. root.setCenter(grid);
  202. }
  203. public void show() {
  204. stage.getScene().setRoot(root);
  205. stage.show();
  206. }
  207. }
  208. public class Gui extends Application {
  209. @Override
  210. public void start(Stage stage) {
  211. Brett board = Util.loadBoard(stage);
  212. if (board == null) {
  213. System.exit(1);
  214. return;
  215. }
  216. stage.setTitle("Sudoku");
  217. SudokuBeholder.Losning unsolved = board.tilLosning();
  218. Scene scene = new Scene(new BorderPane(), 500, 500);
  219. stage.setScene(scene);
  220. KeyCodeCombination openFileComb = new KeyCodeCombination(
  221. KeyCode.O,
  222. KeyCombination.CONTROL_DOWN
  223. );
  224. scene.addEventHandler(KeyEvent.KEY_RELEASED, new KeyHandler() {
  225. @Override
  226. public void handle(KeyEvent evt) {
  227. if (openFileComb.match(evt)) {
  228. Brett board = Util.loadBoard(stage);
  229. if (board != null)
  230. new UnsolvedView(stage, board).show();
  231. }
  232. }
  233. });
  234. new UnsolvedView(stage, board).show();
  235. }
  236. public static void main(String[] args) {
  237. launch();
  238. }
  239. public Gui() {
  240. super();
  241. }
  242. }