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

add close button

parent 913d97e7
No related branches found
No related tags found
2 merge requests!6Add final version of the game to main branch,!5Add basic implementation
......@@ -2,6 +2,7 @@ package com.example.tictactoe_game;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
......@@ -28,8 +29,7 @@ public class TicTacToeFX extends Application {
public void start(Stage primaryStage) {
EventHandler<GameEvent> gameEventHandler = gameEvent -> {
String currentPlayer = isXTurn ? "X" : "O";
String currentPlayer = this.isXTurn ? "X" : "O";
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(primaryStage);
......@@ -45,6 +45,12 @@ public class TicTacToeFX extends Application {
dialogVbox.getChildren().add(new Text("This is a Draw!"));
}
Button closeButton = new Button();
closeButton.setText("Close");
closeButton.setOnAction(event -> primaryStage.close());
dialogVbox.getChildren().add(closeButton);
dialogVbox.setAlignment(Pos.CENTER);
Scene dialogScene = new Scene(dialogVbox, 200, 100);
dialog.setScene(dialogScene);
dialog.show();
......@@ -75,14 +81,14 @@ public class TicTacToeFX extends Application {
grid.add(turnCounter, 3, 1);
// Szene und Stage erstellen
Scene scene1 = new Scene(grid, 300, 300); // Größe der Szene
Scene scene1 = new Scene(grid, 400, 300); // Größe der Szene
primaryStage.setTitle("Tic-Tac-Toe");
primaryStage.setScene(scene1);
primaryStage.show();
}
private void handleButtonClick(Button button) {
if(button.getText().isEmpty() && !this.isGameOver) {
if (button.getText().isEmpty() && !this.isGameOver) {
if (this.isXTurn) {
button.setText("X");
this.currentTurn++;
......@@ -94,8 +100,7 @@ public class TicTacToeFX extends Application {
if (checkForWin()) {
this.isGameOver = true;
button.fireEvent(new GameEvent(GameEvent.VICTORY_EVENT));
}
else if(checkForDraw()){
} else if (checkForDraw()) {
this.isGameOver = true;
button.fireEvent(new GameEvent(GameEvent.DRAW_EVENT));
}
......@@ -106,21 +111,21 @@ public class TicTacToeFX extends Application {
}
}
private boolean checkForWin(){
for(int row = 0; row < 3; row++){
private boolean checkForWin() {
for (int row = 0; row < 3; row++) {
String text1 = buttons[row][0].getText();
String text2 = buttons[row][1].getText();
String text3 = buttons[row][2].getText();
if(!text1.isEmpty() && text1.equals(text2) && text1.equals(text3)){
if (!text1.isEmpty() && text1.equals(text2) && text1.equals(text3)) {
return true;
}
}
for(int col = 0; col < 3; col++){
for (int col = 0; col < 3; col++) {
String text1 = buttons[0][col].getText();
String text2 = buttons[1][col].getText();
String text3 = buttons[2][col].getText();
if(!text1.isEmpty() && text1.equals(text2) && text1.equals(text3)){
if (!text1.isEmpty() && text1.equals(text2) && text1.equals(text3)) {
return true;
}
}
......@@ -131,21 +136,21 @@ public class TicTacToeFX extends Application {
String upperRightCorner = buttons[0][2].getText();
String lowerLeftCorner = buttons[2][0].getText();
if(!upperLeftCorner.isEmpty() && upperLeftCorner.equals(center) && upperLeftCorner.equals(lowerRightCorner)){
if (!upperLeftCorner.isEmpty() && upperLeftCorner.equals(center) && upperLeftCorner.equals(lowerRightCorner)) {
return true;
}
if(!upperRightCorner.isEmpty() && upperRightCorner.equals(center) && upperRightCorner.equals(lowerLeftCorner)){
if (!upperRightCorner.isEmpty() && upperRightCorner.equals(center) && upperRightCorner.equals(lowerLeftCorner)) {
return true;
}
return false;
}
private boolean checkForDraw(){
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++){
if(buttons[row][col].getText().isEmpty()){
private boolean checkForDraw() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (buttons[row][col].getText().isEmpty()) {
return false;
}
}
......
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