From 34f48a3a60ad6724ebac7ca0a7f011d4fb18ff3f Mon Sep 17 00:00:00 2001 From: David Reiser <david.060903@gmail.com> Date: Thu, 9 Jan 2025 15:19:43 +0100 Subject: [PATCH] mistake adjusted --- GameUI.java | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++-- Tile.java | 48 ++++------------ 2 files changed, 161 insertions(+), 44 deletions(-) diff --git a/GameUI.java b/GameUI.java index 9a91fdf..76fe410 100644 --- a/GameUI.java +++ b/GameUI.java @@ -1,19 +1,164 @@ import javafx.application.Application; +import javafx.geometry.*; import javafx.scene.Scene; -import javafx.scene.layout.GridPane; +import javafx.scene.image.*; +import javafx.scene.control.*; +import javafx.scene.layout.*; import javafx.stage.Stage; public class GameUI extends Application{ + Scene game, winningScene; + BorderPane gameBorderPane, winningBorderPane; + private Board board = new Board(); + private Label playerLabel = new Label("Player X's turn"); + private Label winnerLabel = new Label("Won"); + private Label scoreLabelX = new Label("Player X: 0"); + private Label scoreLabelO= new Label("Player O: 0"); + private Button[] buttons = new Button[9]; + + /** + * Provides styles allignment and the scenes for labels, buttons and boxes. + * + * @param stage starts the scene and is the main stage for the application + */ @Override public void start(Stage stage){ - GridPane GridPane = new GridPane(); - Scene scene = new Scene(GridPane, 300, 300); - stage.setScene(scene); - stage.setTitle("Tic-Tac-Toe"); + playerLabel.setStyle("-fx-font-size: 30px; -fx-font-family: 'Impact'; -fx-text-fill: blue;"); + winnerLabel.setStyle("-fx-font-size: 150px; -fx-font-family: 'Impact'; -fx-text-fill: blue;"); + scoreLabelX.setStyle("-fx-font-size: 30px; -fx-font-family: 'Impact'; -fx-text-fill: black;"); + scoreLabelO.setStyle("-fx-font-size: 30px; -fx-font-family: 'Impact'; -fx-text-fill: black;"); + + gameBorderPane = new BorderPane(); + winningBorderPane = new BorderPane(); + GridPane gridPane = new GridPane(); + + for(int i = 0; i < 9; i++){ + int index = i; + buttons[i] = new Button(); + buttons[i].setPrefSize(150, 150); + buttons[i].setOnAction(e -> handleTurn(index, stage)); + gridPane.add(buttons[i], i % 3, i / 3); + } + + gameBorderPane.setCenter(gridPane); + + //HBox for the labels + HBox box = new HBox(20); + box.setAlignment(Pos.CENTER); + playerLabel.setAlignment(Pos.TOP_CENTER); + playerLabel.setPadding(new Insets(10, 0, 15, 15)); + box.getChildren().add(playerLabel); + + //space between the labels + Region spacer = new Region(); + HBox.setHgrow(spacer, Priority.ALWAYS); + box.getChildren().add(spacer); + + //score labels design + scoreLabelX.setAlignment(Pos.TOP_CENTER); + scoreLabelO.setAlignment(Pos.TOP_CENTER); + VBox scoreBox = new VBox(15); + scoreBox.setPadding(new Insets(15, 25, 5, 5)); + scoreBox.setAlignment(Pos.TOP_RIGHT); + scoreBox.getChildren().addAll(scoreLabelX, scoreLabelO); + + box.getChildren().add(scoreBox); + gameBorderPane.setTop(box); + + //board allignment + gridPane.setPadding(new Insets(10)); + gridPane.setHgap(30); + gridPane.setVgap(30); + gridPane.setAlignment(Pos.CENTER); + + //reset button + Button reset = new Button("Reset"); + reset.setStyle("-fx-font-size: 30px; -fx-font-family: 'Impact'; -fx-text-fill: black;"); + reset.setOnAction(e -> stage.setScene(game)); + + //winning/tie screen + winnerLabel.setAlignment(Pos.CENTER); + VBox finalBox = new VBox(35); + finalBox.setAlignment(Pos.CENTER); + finalBox.getChildren().addAll(winnerLabel, reset); + winningBorderPane.setCenter(finalBox); + winningScene = new Scene(winningBorderPane, 1500,850); + + //main Scene + game = new Scene(gameBorderPane, 1500, 850); + stage.setScene(game); + stage.setTitle("Tick-Tack-Trauma"); stage.show(); + } + + /** + * method for the game updating its state including changing turn of the player and check for win or tie. Images are set as symbols. + * + * @param index of the selectet tile + * @param stage current scene + */ + private void handleTurn(int index, Stage stage){ + if (board.tiles[index].exist_owner()) { + return; + } + board.tiles[index].set_owner(board.currentPlayer); + + //replace symbols with images + String imageSymbols = board.currentPlayer == 0 ? getClass().getResource("/textures/1_0.png").toExternalForm() : getClass().getResource("/textures/0_0.png").toExternalForm(); + Image image = new Image(""+ imageSymbols); + ImageView imageView = new ImageView(image); + + //size of the image + imageView.setFitWidth(135); + imageView.setFitHeight(135); + imageView.setPreserveRatio(true); + + buttons[index].setGraphic(imageView); + + board.turnEnd(); + updatePlayerTurn(board.currentPlayer); + + //changes label state for the winning player and updates its score + if (board.victoryCheck()) { + winnerLabel.setText("Player " + (board.currentPlayer == 0 ? "X" : "O") + " wins!"); + updatePlayerScore(); + stage.setScene(winningScene); + return; + } + + //changes label state to draw + if (board.marks == 9) { + winnerLabel.setText("It's a draw!"); + stage.setScene(winningScene); + return; + } } + /** + * updates the label of the current player + * + * @param currentPlayer player whose turn it is + */ + private void updatePlayerTurn(int currentPlayer) { + String playerSymbol = (currentPlayer == 0) ? "X" : "O"; + playerLabel.setText("Player " + playerSymbol + "'s turn"); + } + + /** + * updates the score of a player who won + * + */ + private void updatePlayerScore(){ + scoreLabelX.setText("Player X: " + board.players[0].getPlayerScore()); + scoreLabelO.setText("Player O: " + board.players[1].getPlayerScore()); + } + + /** + * starts the javafx application + * + * @param args + */ public void call(String[] args){ - launch(args); + launch(args); } } diff --git a/Tile.java b/Tile.java index 651c4b8..863cd73 100644 --- a/Tile.java +++ b/Tile.java @@ -1,14 +1,6 @@ -import java.io.FileInputStream; -import java.io.IOException; import java.util.Random; -import javax.imageio.ImageIO; -import javafx.scene.image.Image; -import javafx.scene.image.ImageView; -import java.awt.image.BufferedImage; - - -public class Tile extends ImageView{ +public class Tile { /**These Values are being set when initialized @@ -28,39 +20,19 @@ public class Tile extends ImageView{ /**Paths for the images * */ - BufferedImage Image; + String imagePathPlayer = "./textures/" + owner + "_" + rnd.nextInt(3); + String imagePathEmpty = "./textures/empty"; + - Board board; - - /**Initializer for Tile Objects * @param size Sets the Size of the Image * @param posx Sets the x-Position of the Image * @param posy Sets the y-Position of the Image */ - public Tile(Board board, int size, int posx, int posy){ - super(); - setImage(new Image(Tile.class.getClassLoader().getResourceAsStream("textures/empty.png"))); - this.size = size; - this.posx = posx; - this.posy = posy; - setOnMouseClicked( e -> clicked() ); - - this.board = board; - - - //super(Tile.class.getClassLoader().getResourceAsStream("textures/empty.png")); - - /*try { - Image = ImageIO.read(Tile.class.getClassLoader().getResourceAsStream("textures/empty.png")); - } catch (IOException e) { - e.printStackTrace(); - } //"/textures/" + owner + "_" + rnd.nextInt(3)*/ - } - - private void clicked() { - owner = board.currentPlayer; - board.turnEnd(); + public Tile(int size, int posx, int posy){ + size = this.size; + posx = this.posx; + posy = this.posy; } /**Lets you set a new owner; @@ -89,7 +61,7 @@ public class Tile extends ImageView{ /**Whether this Tile is owned by a Player */ public boolean exist_owner(){ - return owner > 0; + return owner != -1; } -} +} \ No newline at end of file -- GitLab