From 5814899d806d4eddebcb385f73e8499a8b7dde1a Mon Sep 17 00:00:00 2001
From: Josua Oppermann <josua.oppermann@gmail.com>
Date: Thu, 9 Jan 2025 16:03:08 +0100
Subject: [PATCH] Random Images and Reset fix

---
 GameUI.java | 37 +++++++++++++++++++++++++++++++++++--
 Tile.java   | 14 +-------------
 2 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/GameUI.java b/GameUI.java
index 76fe410..feb6899 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,24 @@ 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);
+            }
+            stage.setScene(game);
+        });
+        
         
         //winning/tie screen
         winnerLabel.setAlignment(Pos.CENTER);
@@ -104,7 +136,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 863cd73..51a200d 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
-- 
GitLab