Skip to content
Snippets Groups Projects
Commit 64939e23 authored by jan-schw's avatar jan-schw
Browse files

add custom event handler for victories and draws

parent 4b2be6e0
No related branches found
No related tags found
2 merge requests!6Add final version of the game to main branch,!5Add basic implementation
<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
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;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment