diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000000000000000000000000000000000..2520c752367f7ad06a2838ab1d048b1380296e6f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ +<component name="InspectionProjectProfileManager"> + <profile version="1.0"> + <option name="myName" value="Project Default" /> + <inspection_tool class="TrivialIf" enabled="true" level="WARNING" enabled_by_default="true"> + <option name="ignoreChainedIf" value="true" /> + </inspection_tool> + </profile> +</component> \ No newline at end of file diff --git a/src/main/java/com/example/tictactoe_game/TicTacToeFX.java b/src/main/java/com/example/tictactoe_game/TicTacToeFX.java index 031f95a0dd6686fc17558fdcd3a32a3e766548f3..7b08c5e49bff0debf4d16186d679439192c07aba 100644 --- a/src/main/java/com/example/tictactoe_game/TicTacToeFX.java +++ b/src/main/java/com/example/tictactoe_game/TicTacToeFX.java @@ -1,9 +1,13 @@ package com.example.tictactoe_game; import javafx.application.Application; +import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.GridPane; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.Modality; import javafx.stage.Stage; public class TicTacToeFX extends Application { @@ -14,6 +18,30 @@ public class TicTacToeFX extends Application { @Override public void start(Stage primaryStage) { + + EventHandler<GameEvent> handler = new EventHandler<GameEvent>() { + @Override + public void handle(GameEvent gameEvent) { + + String currentPlayer = isXTurn ? "X" : "O"; + + final Stage dialog = new Stage(); + dialog.initModality(Modality.APPLICATION_MODAL); + dialog.initOwner(primaryStage); + VBox dialogVbox = new VBox(20); + if(gameEvent.getEventType() == GameEvent.VICTORY_EVENT) { + dialogVbox.getChildren().add(new Text("This is a victory for player " + currentPlayer + "!")); + } + else if(gameEvent.getEventType() == GameEvent.DRAW_EVENT) { + dialogVbox.getChildren().add(new Text("This is a Draw!")); + } + Scene dialogScene = new Scene(dialogVbox, 150, 150); + dialog.setScene(dialogScene); + dialog.show(); + } + }; + + // Erstelle das das Spielfeld GridPane grid = new GridPane(); @@ -24,7 +52,8 @@ public class TicTacToeFX extends Application { button.setPrefSize(100, 100); // Größe der Buttons grid.add(button, col, row); // Hinzufügen zum Grid buttons[row][col] = button; - + button.addEventHandler(GameEvent.VICTORY_EVENT, handler); + button.addEventHandler(GameEvent.DRAW_EVENT, handler); button.setOnAction(event -> handleButtonClick(button)); } } @@ -40,17 +69,17 @@ public class TicTacToeFX extends Application { if(button.getText().isEmpty()) { if (this.isXTurn) { button.setText("X"); - this.isXTurn = false; } else { button.setText("O"); - this.isXTurn = true; } if (checkForWin()) { - buttons[1][1].setText("juhu"); + button.fireEvent(new GameEvent(GameEvent.VICTORY_EVENT)); } else if(checkForDraw()){ - buttons[1][1].setText("draw"); + button.fireEvent(new GameEvent(GameEvent.DRAW_EVENT)); } + + this.isXTurn = !this.isXTurn; } }