diff --git a/GameUI.java b/GameUI.java index 76fe410b9b26129a71b6c275c9592ce836e75b59..555e552ad8ce3908acb549d831baf439a31714cb 100644 --- a/GameUI.java +++ b/GameUI.java @@ -5,6 +5,7 @@ import javafx.scene.image.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; +import java.util.Random; public class GameUI extends Application{ Scene game, winningScene; @@ -15,6 +16,9 @@ public class GameUI extends Application{ private Label scoreLabelX = new Label("Player X: 0"); private Label scoreLabelO= new Label("Player O: 0"); private Button[] buttons = new Button[9]; + + /**Random for Images */ + Random rnd = new Random(); /** * Provides styles allignment and the scenes for labels, buttons and boxes. @@ -37,6 +41,17 @@ public class GameUI extends Application{ buttons[i] = new Button(); buttons[i].setPrefSize(150, 150); buttons[i].setOnAction(e -> handleTurn(index, stage)); + + String imageSymbols = getClass().getResource("/textures/empty.png").toExternalForm(); + Image image = new Image("" + imageSymbols); + ImageView imageView = new ImageView(image); + + imageView.setFitWidth(135); + imageView.setFitHeight(135); + imageView.setPreserveRatio(true); + + buttons[index].setGraphic(imageView); + gridPane.add(buttons[i], i % 3, i / 3); } @@ -74,7 +89,25 @@ public class GameUI extends Application{ //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)); + reset.setOnAction(e -> { + board = new Board(board.players, board.firstPlayer); + for(int i = 0; i < 9; i++){ + int index = i; + + String imageSymbols = getClass().getResource("/textures/empty.png").toExternalForm(); + Image image = new Image("" + imageSymbols); + ImageView imageView = new ImageView(image); + + imageView.setFitWidth(135); + imageView.setFitHeight(135); + imageView.setPreserveRatio(true); + + buttons[index].setGraphic(imageView); + } + updatePlayerTurn(board.currentPlayer); + stage.setScene(game); + }); + //winning/tie screen winnerLabel.setAlignment(Pos.CENTER); @@ -104,7 +137,8 @@ public class GameUI extends Application{ 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(); + String imageSymbols = board.currentPlayer == 0 ? getClass().getResource("/textures/1_" + rnd.nextInt(3) + ".png").toExternalForm() : + getClass().getResource("/textures/0_" + rnd.nextInt(3) + ".png").toExternalForm(); Image image = new Image(""+ imageSymbols); ImageView imageView = new ImageView(image); diff --git a/Tile.java b/Tile.java index 863cd73ed2957cbdfaa370971c4eadd011a8de83..51a200d8ab0e32fc21d33c09c9856ef1cd51b904 100644 --- a/Tile.java +++ b/Tile.java @@ -1,4 +1,4 @@ -import java.util.Random; + public class Tile { @@ -11,18 +11,6 @@ public class Tile { * -1 = no one; 0 = Player 1; 1 = Player 2 */ int owner = -1; - - /**Random for Images - * - */ - Random rnd = new Random(); - - /**Paths for the images - * - */ - String imagePathPlayer = "./textures/" + owner + "_" + rnd.nextInt(3); - String imagePathEmpty = "./textures/empty"; - /**Initializer for Tile Objects * @param size Sets the Size of the Image