Skip to content
Snippets Groups Projects
GameUI.java 7.19 KiB
Newer Older
David Reiser's avatar
David Reiser committed
import javafx.application.Application;
David Reiser's avatar
David Reiser committed
import javafx.geometry.*;
David Reiser's avatar
David Reiser committed
import javafx.scene.Scene;
David Reiser's avatar
David Reiser committed
import javafx.scene.image.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
David Reiser's avatar
David Reiser committed
import javafx.stage.Stage;
import java.util.Random;
David Reiser's avatar
David Reiser committed

public class GameUI extends Application{
David Reiser's avatar
David Reiser committed
    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]; 
Josua Oppermann's avatar
Josua Oppermann committed
    /**
     * Random for Images 
    */
    Random rnd = new Random();
David Reiser's avatar
David Reiser committed

    /**
     * Provides styles allignment and the scenes for labels, buttons and boxes.
     * 
     * @param stage starts the scene and is the main stage for the application
     */
David Reiser's avatar
David Reiser committed
    @Override
    public void start(Stage stage){
David Reiser's avatar
David Reiser committed
        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));

            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);

David Reiser's avatar
David Reiser committed
            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 -> {
            board = new Board(board.getPlayers(), board.getFirstPlayer());
            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.getCurrentPlayer());
            stage.setScene(game);
        });
        
David Reiser's avatar
David Reiser committed
        
        //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");
David Reiser's avatar
David Reiser committed
        stage.show();
David Reiser's avatar
David Reiser committed
    } 
    
    /** 
     * 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){
David Reiser's avatar
David Reiser committed
            return; 
        }
        int currentPlayer = board.getCurrentPlayer();
        board.setTileOwner(index, currentPlayer);
David Reiser's avatar
David Reiser committed
       
        //replace symbols with images
Josua Oppermann's avatar
Josua Oppermann committed
        int i = rnd.nextInt(10);
        String imageSymbols = currentPlayer == 0 ? getClass().getResource("/textures/1_" + i + ".png").toExternalForm() :
                                                   getClass().getResource("/textures/0_" + i + ".png").toExternalForm();
David Reiser's avatar
David Reiser committed
        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);
        
David Reiser's avatar
David Reiser committed
                updatePlayerTurn(board.getCurrentPlayer());
David Reiser's avatar
David Reiser committed
        
            case 1:
                //changes label state for the winning player and updates its score
                winnerLabel.setText("Player " + (currentPlayer == 0 ? "X" : "O") + " wins!");
                updatePlayerScore();
                stage.setScene(winningScene);
                break;
            
            case 2:
                //changes label state to draw
                winnerLabel.setText("It's a draw!");
                stage.setScene(winningScene);
                break;
                //should never be reached
                System.out.println("How did we get here?");
                break;
David Reiser's avatar
David Reiser committed
        }
David Reiser's avatar
David Reiser committed
    /** 
     * 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.getPlayer(0).getPlayerScore());
        scoreLabelO.setText("Player O: " + board.getPlayer(1).getPlayerScore());
David Reiser's avatar
David Reiser committed
    }
    
    /** 
     * starts the javafx application
     * 
     * @param args 
     */
JimmyTheCat's avatar
JimmyTheCat committed
    public void call(String[] args){
David Reiser's avatar
David Reiser committed
       launch(args); 
David Reiser's avatar
David Reiser committed
    }
}