diff --git a/bin/coursework/Cage.class b/bin/coursework/Cage.class index f93cf6cd92afb4721aa5581143ec6bcac4afbf0f..70a114efa9b35343e040b2214c124d1c0de03eb7 100644 Binary files a/bin/coursework/Cage.class and b/bin/coursework/Cage.class differ diff --git a/bin/coursework/ConstraintsHandler.class b/bin/coursework/ConstraintsHandler.class index 3925deed9d581baf2a3e504ec054b790a6711334..d80f886ace8cb45794c9249a022cb37de2a66d45 100644 Binary files a/bin/coursework/ConstraintsHandler.class and b/bin/coursework/ConstraintsHandler.class differ diff --git a/bin/coursework/GameState.class b/bin/coursework/GameState.class index 05a8e904dce51e954e51bf717fb058b235294275..7da95ce3340cd26d28aaa90c45379d71f8c06b7d 100644 Binary files a/bin/coursework/GameState.class and b/bin/coursework/GameState.class differ diff --git a/bin/coursework/Main.class b/bin/coursework/Main.class index 7f230ff2765f64c4bfeaa4d51e685ceb5a8f0205..eebd54f556d3480d151a72b3fd5448dad7431061 100644 Binary files a/bin/coursework/Main.class and b/bin/coursework/Main.class differ diff --git a/src/coursework/ConstraintsHandler.java b/src/coursework/ConstraintsHandler.java index 3eb230683ab9f6bd8dc10a381fac252f2b801be1..be5667258d9cecb55d0ab14649c137a4f7ca9725 100644 --- a/src/coursework/ConstraintsHandler.java +++ b/src/coursework/ConstraintsHandler.java @@ -135,4 +135,32 @@ public class ConstraintsHandler { } } + public void checkWinCondition() { + if (checkCellsFull() && checkConstraintErrors()) { + // win condition fulfilled + // animation and pop up + System.out.println("You won"); + } + } + + private boolean checkCellsFull() { + for (TextField textField : gridNumbers) { + if (!textField.getText().equals("")) { + return false; + } + } + return true; + } + + private boolean checkConstraintErrors() { + for (boolean[] cellErrors : gridErrors) { + for (boolean errorVal : cellErrors) { + if (errorVal) { + return false; + } + } + } + return true; + } + } diff --git a/src/coursework/Main.java b/src/coursework/Main.java index 087ea753cd0e2a06620b31b8f89029122a604207..3416de1a951f792bb230dafa7a20866ea2c94eeb 100644 --- a/src/coursework/Main.java +++ b/src/coursework/Main.java @@ -34,7 +34,7 @@ import javafx.stage.Stage; public class Main extends Application { - final static public int N = 6; + final static public int N = 5; final private double GRID_PERCENTAGE = 0.75; private Scene scene; @@ -62,7 +62,7 @@ public class Main extends Application { private Button undoButton; private Button redoButton; - + ////////////////////////////////////////////////////////////////////////////////// public static Square[] getGridSquares() { return gridSquares; } @@ -86,11 +86,12 @@ public class Main extends Application { public static boolean[][] getGridErrors() { return gridErrors; } + ////////////////////////////////////////////////////////////////////////////////// private GridPane setupGrid() { GridPane gridPane = new GridPane(); gridPane.setAlignment(Pos.CENTER); - double size = scene.getWidth() * GRID_PERCENTAGE / N; + double size = (scene.getWidth() * GRID_PERCENTAGE) / N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { @@ -124,45 +125,6 @@ public class Main extends Application { return gridPane; } - private void textFieldClickEvent(int i, int j) { - if (!clearing && !undoing) { - undoStack.push(new GameState(GameState.getCurrentGameState(gridNumbers))); - if (undoButton.isDisabled()) { - undoButton.setDisable(false); - } - } - constraints.checkConstraints(j, i, showMistakes);; - checkWinCondition(); - } - - private void checkWinCondition() { - if (checkCellsFull() && checkConstraintErrors()) { - // win condition fulfilled - // animation and pop up - System.out.println("You won"); - } - } - - private boolean checkCellsFull() { - for (TextField textField : gridNumbers) { - if (!textField.getText().equals("")) { - return false; - } - } - return true; - } - - private boolean checkConstraintErrors() { - for (boolean[] cellErrors : gridErrors) { - for (boolean errorVal : cellErrors) { - if (errorVal) { - return false; - } - } - } - return true; - } - private VBox setupGridVBox(Square square, int i, int j) { VBox vbox = new VBox(); Label cageInfoLabel = new Label(""); @@ -213,27 +175,11 @@ public class Main extends Application { topHBox.getChildren().addAll(loadGameFileButton, showMistakesCheck, loadGameTextInputButton); showMistakesCheck.selectedProperty().addListener((obs, oldVal, newVal) -> { - showMistakes = showMistakesCheck.isSelected(); - constraints.highlightCells(showMistakes); + showMistakesCheckEvent(showMistakesCheck); }); loadGameFileButton.setOnAction(e -> { - try { - FileChooser fileChooser = new FileChooser(); - fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt")); - File selectedFile = fileChooser.showOpenDialog(stage); - BufferedReader file = new BufferedReader(new FileReader(selectedFile)); - - String line; - while ((line = file.readLine()) != null) { - createNewCage(line); - } - file.close(); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } catch (IOException e2) { - e2.printStackTrace(); - } + loadFileButtonClickEvent(); }); return topHBox; @@ -275,29 +221,10 @@ public class Main extends Application { redoButton = new Button("", new ImageView(new Image("file:Images/redo_icon.png"))); Button clearButton = new Button("Clear"); undoButton.setOnAction(e -> { - undoing = true; - - redoStack.push(undoStack.pop()); - updateGameState(undoStack.peek()); - - if (!redoStack.empty()) { - redoButton.setDisable(false); - } - if (undoStack.size() == 1) { - undoButton.setDisable(true); - } - undoing = false; + undoButtonClickEvent(); }); redoButton.setOnAction(e -> { - GameState gameState = redoStack.pop(); - updateGameState(gameState); - - if (!undoStack.empty()) { - undoButton.setDisable(false); - } - if (redoStack.empty()) { - redoButton.setDisable(true); - } + redoButtonClickEvent(); }); undoButton.setDisable(true); @@ -310,6 +237,147 @@ public class Main extends Application { return bottomHBox; } + private VBox setupNumbersVBox() { + VBox vbox = new VBox(); + for (int i = 1; i <= N; i++) { + Button button = new Button(i + ""); + double cellWidth = gridSquares[0].getRectangle().getWidth() / 2; + double fontSize = cellWidth * 0.4; + + button.setPrefSize(cellWidth, cellWidth); + button.setMaxSize(150, 150); + button.setStyle("-fx-font-size:"+fontSize+"pt"); + button.setOnAction(e -> { + numbersButtonClickEvent(button); + }); + vbox.getChildren().add(button); + } + vbox.setSpacing(10); + vbox.setAlignment(Pos.CENTER); + return vbox; + } + + private VBox setupFontSizeHBox() { + VBox vbox = new VBox(); + vbox.getChildren().addAll(setupButton("Tiny", 10), setupButton("Small", 14), setupButton("Medium", 18), setupButton("Large", 22), setupButton("Huge", 26)); + vbox.setSpacing(10); + vbox.setAlignment(Pos.CENTER); + return vbox; + } + + private Button setupButton(String name, int size) { + Button button = new Button(name); + button.setStyle("-fx-font-size:" + size + "px;"); + button.setOnAction(e -> { + changeFontSizeEvent(size); + }); + return button; + } + + /////////////////////////////////////////////////////////// + private void loadFileButtonClickEvent() { + try { + FileChooser fileChooser = new FileChooser(); + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text Files", "*.txt")); + File selectedFile = fileChooser.showOpenDialog(stage); + BufferedReader file = new BufferedReader(new FileReader(selectedFile)); + + String line; + while ((line = file.readLine()) != null) { + createNewCage(line); + } + file.close(); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e2) { + e2.printStackTrace(); + } + } + + private void showMistakesCheckEvent(CheckBox showMistakesCheck) { + showMistakes = showMistakesCheck.isSelected(); + constraints.highlightCells(showMistakes); + } + + private void undoButtonClickEvent() { + undoing = true; + + redoStack.push(undoStack.pop()); + updateGameState(undoStack.peek()); + + if (!redoStack.empty()) { + redoButton.setDisable(false); + } + if (undoStack.size() == 1) { + undoButton.setDisable(true); + } + undoing = false; + } + + private void redoButtonClickEvent() { + GameState gameState = redoStack.pop(); + updateGameState(gameState); + + if (!undoStack.empty()) { + undoButton.setDisable(false); + } + if (redoStack.empty()) { + redoButton.setDisable(true); + } + } + + private void numbersButtonClickEvent(Button button) { + if (previousSquare != null) { + for (int i = 0; i < gridSquares.length; i++) { + if (previousSquare.equals(gridSquares[i])) { + gridNumbers[i].setText(button.getText()); + } + } + } + } + + private void changeFontSizeEvent(int size) { + fontSize = size; + for (TextField textField : gridNumbers) { + textField.setStyle("-fx-focus-color: transparent; -fx-text-box-border: transparent;" + + "-fx-background-color: -fx-text-box-border, -fx-control-inner-background;" + + "-fx-font-size:" + size*1.8 + ";"); + } + for (Label label : cageLabels) { + label.setStyle("-fx-font-size:" + size*0.9 + ";"); + } + } + + private void stageWidthResizeEvent(Stage stage, GridPane gridPane) { + stage.widthProperty().addListener((obs, oldVal, newVal) -> { + if (newVal.doubleValue() < stage.getHeight()) { + isWider = false; + resizeGrid(gridPane.getLayoutX(), gridPane.getLayoutY(), gridPane.getWidth()); + } + }); + } + + private void stageHeightResizeEvent(Stage stage, GridPane gridPane) { + stage.heightProperty().addListener((obs, oldVal, newVal) -> { + if (newVal.doubleValue() < stage.getWidth()) { + isWider = true; + resizeGrid(gridPane.getLayoutX(), gridPane.getLayoutY(), gridPane.getWidth()); + } + }); + } + + public void resizeGrid(double gridX, double gridY, double gridWidth) { + double newValue = isWider ? scene.getHeight() : scene.getWidth(); + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + Rectangle rectangle = gridSquares[i*N + j].getRectangle(); + rectangle.setWidth(newValue * GRID_PERCENTAGE / N); + rectangle.setHeight(newValue * GRID_PERCENTAGE / N); + gridSquares[i*N + j].resizeLines(newValue * GRID_PERCENTAGE / N); + } + } + } + private void paneClickSquareEvent(int i, int j, Square square) { gridBoxes[i*N + j].addEventHandler(MouseEvent.MOUSE_CLICKED, e -> { if (previousPane != null) { @@ -372,6 +440,17 @@ public class Main extends Application { }); } + private void textFieldClickEvent(int i, int j) { + if (!clearing && !undoing) { + undoStack.push(new GameState(GameState.getCurrentGameState(gridNumbers))); + if (undoButton.isDisabled()) { + undoButton.setDisable(false); + } + } + constraints.checkConstraints(j, i, showMistakes);; + constraints.checkWinCondition(); + } + private boolean lastOperationClear() { GameState gameState = undoStack.peek(); boolean emptyGrid = true; @@ -383,123 +462,12 @@ public class Main extends Application { return emptyGrid; } - private void stageWidthResizeEvent(Stage stage, GridPane gridPane) { - stage.widthProperty().addListener((obs, oldVal, newVal) -> { - if (newVal.doubleValue() < stage.getHeight()) { - isWider = false; - resizeGrid(gridPane.getLayoutX(), gridPane.getLayoutY(), gridPane.getWidth()); - } - }); - } - - private void stageHeightResizeEvent(Stage stage, GridPane gridPane) { - stage.heightProperty().addListener((obs, oldVal, newVal) -> { - if (newVal.doubleValue() < stage.getWidth()) { - isWider = true; - resizeGrid(gridPane.getLayoutX(), gridPane.getLayoutY(), gridPane.getWidth()); - } - }); - } - - public void resizeGrid(double gridX, double gridY, double gridWidth) { - double newValue = isWider ? scene.getHeight() : scene.getWidth(); - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - Rectangle rectangle = gridSquares[i*N + j].getRectangle(); - rectangle.setWidth(newValue * GRID_PERCENTAGE / N); - rectangle.setHeight(newValue * GRID_PERCENTAGE / N); - gridSquares[i*N + j].resizeLines(newValue * GRID_PERCENTAGE / N); - } - } - } - - private HBox setupCenterHBox() { - HBox hbox = new HBox(); - for (int i = 1; i <= N; i++) { - Button button = new Button(i + ""); - double cellWidth = gridSquares[0].getRectangle().getWidth() / 2; - double fontSize = cellWidth * 0.4; - - button.setPrefSize(cellWidth, cellWidth); - button.setMaxSize(150, 150); - button.setStyle("-fx-font-size:"+fontSize+"pt"); - button.setOnAction(e -> { - numbersButtonClickEvent(button); - }); - hbox.getChildren().add(button); - } - hbox.setSpacing(10); - hbox.setAlignment(Pos.CENTER); - return hbox; - } - - private void numbersButtonClickEvent(Button button) { - if (previousSquare != null) { - for (int i = 0; i < gridSquares.length; i++) { - if (previousSquare.equals(gridSquares[i])) { - gridNumbers[i].setText(button.getText()); - } - } - } - } - - private HBox setupFontSizeHBox() { - HBox hbox = new HBox(); - Button tiny = new Button("Tiny"); - Button small = new Button("Small"); - Button medium = new Button("Medium"); - Button large = new Button("Large"); - Button huge = new Button("Huge"); - - - - tiny.setStyle("-fx-font-size:12px;"); - tiny.setOnAction(e -> { - changeFontSize(10); - }); - - small.setStyle("-fx-font-size:16px;"); - small.setOnAction(e -> { - changeFontSize(12); - }); - - medium.setStyle("-fx-font-size:20px;"); - medium.setOnAction(e -> { - changeFontSize(18); - }); - - large.setStyle("-fx-font-size:24px;"); - large.setOnAction(e -> { - changeFontSize(22); - }); - - huge.setStyle("-fx-font-size:28px;"); - huge.setOnAction(e -> { - changeFontSize(26); - }); - - hbox.getChildren().addAll(tiny, small, medium, large, huge); - hbox.setSpacing(10); - hbox.setAlignment(Pos.CENTER); - return hbox; - } - - private void changeFontSize(int size) { - fontSize = size; - for (TextField textField : gridNumbers) { - textField.setStyle("-fx-focus-color: transparent; -fx-text-box-border: transparent;" - + "-fx-background-color: -fx-text-box-border, -fx-control-inner-background;" - + "-fx-font-size:" + size*1.8 + ";"); - } - for (Label label : cageLabels) { - label.setStyle("-fx-font-size:" + size*0.9 + ";"); - } - } + //////////////////////////////////////////////////////// @Override public void start(Stage stage) throws Exception { VBox vBox = new VBox(); - scene = new Scene(vBox,400+50*N,450+50*N); + scene = new Scene(vBox,500+50*N,500+50*N); vBox.setSpacing(10); vBox.setPadding(new Insets(20)); vBox.setAlignment(Pos.CENTER); @@ -508,19 +476,24 @@ public class Main extends Application { undoStack.push(new GameState(GameState.getCurrentGameState(gridNumbers))); fontSize = 18; - changeFontSize(fontSize); - HBox fontSizeHBox = setupFontSizeHBox(); + changeFontSizeEvent(fontSize); HBox topHBox = setupTopHBox(); - HBox centerHBox = setupCenterHBox(); + + VBox fontSizeVBox = setupFontSizeHBox(); + VBox numbersVBox = setupNumbersVBox(); + HBox gridHBox = new HBox(); + gridHBox.getChildren().addAll(fontSizeVBox, gridPane, numbersVBox); + gridHBox.setSpacing(20); + HBox bottomHBox = setupBottomHBox(); - vBox.getChildren().addAll(topHBox, gridPane, centerHBox, bottomHBox, fontSizeHBox); + vBox.getChildren().addAll(topHBox, gridHBox, bottomHBox); stageWidthResizeEvent(stage, gridPane); stageHeightResizeEvent(stage, gridPane); stage.setMinWidth(500 + 50*N); - stage.setMinHeight(500 + 50*(N+1)); + stage.setMinHeight(500 + 50*N); stage.setScene(scene); stage.setTitle("Mathdoku"); stage.show();