From 40fd8d160589e26f41f492e10a9f7f3df6846228 Mon Sep 17 00:00:00 2001 From: iliya <iliya.saroukha@hes-so.ch> Date: Wed, 31 Jan 2024 14:39:02 +0100 Subject: [PATCH] f all that honestly --- pom.xml | 2 +- src/main/java/hepia/BlackjackController.java | 136 ++++++++++++++++++ .../hepia/{MainUI.java => BlackjackUI.java} | 10 +- src/main/java/hepia/GameManager.java | 2 +- src/main/java/hepia/MainUIController.java | 20 --- src/main/resources/hepia/blackjack-ui.fxml | 69 +++++++++ src/main/resources/hepia/main-ui.fxml | 11 -- 7 files changed, 215 insertions(+), 35 deletions(-) create mode 100644 src/main/java/hepia/BlackjackController.java rename src/main/java/hepia/{MainUI.java => BlackjackUI.java} (57%) delete mode 100644 src/main/java/hepia/MainUIController.java create mode 100644 src/main/resources/hepia/blackjack-ui.fxml delete mode 100644 src/main/resources/hepia/main-ui.fxml diff --git a/pom.xml b/pom.xml index 62bde30..ab8a4da 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,7 @@ <artifactId>javafx-maven-plugin</artifactId> <version>0.0.8</version> <configuration> - <mainClass>hepia.MainUI</mainClass> + <mainClass>hepia.BlackjackUI</mainClass> </configuration> </plugin> <plugin> diff --git a/src/main/java/hepia/BlackjackController.java b/src/main/java/hepia/BlackjackController.java new file mode 100644 index 0000000..9697ca8 --- /dev/null +++ b/src/main/java/hepia/BlackjackController.java @@ -0,0 +1,136 @@ +package hepia; + +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.scene.layout.VBox; + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.io.OutputStream; +import java.util.Scanner; + +public class BlackjackController { + + private GameManager game; + + @FXML + private VBox initialForm; + + @FXML + private VBox blackjackTable; + + @FXML + private TextField humanPlayersField; + + @FXML + private TextField basicAIPlayersField; + + @FXML + private TextField advancedAIPlayersField; + + @FXML + private TextField decksField; + + @FXML + private TextArea outputTextArea; + + @FXML + private TextField inputTextField; + + @FXML + private VBox playerBetsVBox; + + private static Scanner scanner; + + @FXML + private void hit() { + // Logic for player hitting + } + + @FXML + private void stand() { + // Logic for player standing + } + + private void askPlayerBets(int nbHumanPlayers) { + // Clear previous player bets + playerBetsVBox.getChildren().clear(); + + // Add text fields and buttons for each player + for (int i = 1; i <= nbHumanPlayers; i++) { + TextField betTextField = new TextField(); + Button betButton = new Button("Place Bet"); + + // Add action to the button (you can customize this action) + int finalI = i; + betButton.setOnAction(event -> { + String bet = betTextField.getText(); + // Process the bet, e.g., validate and send to the game engine + System.out.println("Player " + finalI + " placed bet: " + bet); + }); + + // Add text field and button to the VBox + playerBetsVBox.getChildren().addAll(betTextField, betButton); + } + } + + @FXML + private void handleInput() { + // Read input from inputTextField and write it to System.out + String input = inputTextField.getText(); + System.out.println(input); + + // Clear the inputTextField + inputTextField.clear(); + } + + @FXML + private void startNewGame() throws IOException { + int nbHumans = Integer.parseInt(humanPlayersField.getText()); + int nbBasicAi = Integer.parseInt(basicAIPlayersField.getText()); + int nbAdvanced = Integer.parseInt(advancedAIPlayersField.getText()); + int nbDecks = Integer.parseInt(decksField.getText()); + + // Create a new game with the provided parameters + // Show blackjack table UI and hide initial form + initialForm.setVisible(false); + blackjackTable.setVisible(true); + + // Initialize the blackjack table UI with the new game + game = new GameManager(Game.BLACKJACK, nbHumans, nbBasicAi, nbAdvanced, nbDecks); + + askPlayerBets(nbHumans); + game.initBets(nbHumans, nbBasicAi, nbAdvanced); + + game.runGame(); + } + + @FXML + private void loadExistingGame() { + // Logic for loading an existing game + String cwd = new File("").getAbsolutePath(); + + File dir = new File(cwd + "/gameBackups"); + File[] match = dir.listFiles((dir1, name) -> name.endsWith(".ser")); + + if (match != null) { + try { + GameManager.restoreGame("gameBackups/" + match[0].getName()); + } catch (Exception e) { + System.err.println("Game couldn't be restored"); + System.exit(1); + } + } else { + System.err.println("No backup files were found"); + System.exit(1); + } + // Show blackjack table UI and hide initial form + initialForm.setVisible(false); + blackjackTable.setVisible(true); + + } +} diff --git a/src/main/java/hepia/MainUI.java b/src/main/java/hepia/BlackjackUI.java similarity index 57% rename from src/main/java/hepia/MainUI.java rename to src/main/java/hepia/BlackjackUI.java index 5d6fa28..3d5218d 100644 --- a/src/main/java/hepia/MainUI.java +++ b/src/main/java/hepia/BlackjackUI.java @@ -4,13 +4,18 @@ import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.TextArea; import javafx.stage.Stage; +import javafx.scene.layout.VBox; -public class MainUI extends Application { +import java.io.OutputStream; +import java.util.Objects; +import java.io.PrintStream; +public class BlackjackUI extends Application { @Override public void start(Stage stage) throws Exception { - Parent root = FXMLLoader.load(getClass().getResource("main-ui.fxml")); + Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("blackjack-ui.fxml"))); Scene scene = new Scene(root); @@ -22,3 +27,4 @@ public class MainUI extends Application { launch(args); } } + diff --git a/src/main/java/hepia/GameManager.java b/src/main/java/hepia/GameManager.java index 3e5ab3f..7e0ce56 100644 --- a/src/main/java/hepia/GameManager.java +++ b/src/main/java/hepia/GameManager.java @@ -59,7 +59,7 @@ public class GameManager implements Serializable { } - private void askPlayersForBets(int nbPlayers) { + public void askPlayersForBets(int nbPlayers) { System.out.println(); for (int i = 0; i < nbPlayers; i++) { int playerBet; diff --git a/src/main/java/hepia/MainUIController.java b/src/main/java/hepia/MainUIController.java deleted file mode 100644 index 883704a..0000000 --- a/src/main/java/hepia/MainUIController.java +++ /dev/null @@ -1,20 +0,0 @@ -package hepia; - -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; - -public class MainUIController { - @FXML - private Label label; - - public void initialize() { - label = new Label("Init gang"); - } - - @FXML - private void handleButton(ActionEvent e) { - label.setText("idk what's happening ngl..."); - } -} diff --git a/src/main/resources/hepia/blackjack-ui.fxml b/src/main/resources/hepia/blackjack-ui.fxml new file mode 100644 index 0000000..fe67a3d --- /dev/null +++ b/src/main/resources/hepia/blackjack-ui.fxml @@ -0,0 +1,69 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<?import javafx.scene.control.*?> +<?import javafx.scene.layout.*?> + +<VBox xmlns="http://javafx.com/javafx/21.0.1" + xmlns:fx="http://javafx.com/fxml/1" + fx:controller="hepia.BlackjackController" + alignment="CENTER" + spacing="10" + prefWidth="600" prefHeight="400"> + + <!-- Initial Form --> + <VBox fx:id="initialForm" alignment="CENTER" spacing="10"> + <Label text="Enter Game Settings" style="-fx-font-size: 18px;"/> + + <HBox spacing="10" alignment="CENTER"> + <Label text="Number of Human Players:"/> + <TextField fx:id="humanPlayersField"/> + </HBox> + + <HBox spacing="10" alignment="CENTER"> + <Label text="Number of Basic AI Players:"/> + <TextField fx:id="basicAIPlayersField"/> + </HBox> + + <HBox spacing="10" alignment="CENTER"> + <Label text="Number of Advanced AI Players:"/> + <TextField fx:id="advancedAIPlayersField"/> + </HBox> + + <HBox spacing="10" alignment="CENTER"> + <Label text="Number of Decks of Cards:"/> + <TextField fx:id="decksField"/> + </HBox> + + <Button text="Start New Game" onAction="#startNewGame"/> + <Button text="Load Existing Game" onAction="#loadExistingGame"/> + </VBox> + + <!-- Blackjack Table UI components here --> + <VBox fx:id="blackjackTable" alignment="CENTER" spacing="10" visible="false"> + <!-- Input TextField --> + <VBox fx:id="playerBetsVBox" spacing="10"> + <!-- Player bets will be dynamically added here --> + </VBox> + + <TextField fx:id="inputTextField" onAction="#handleInput" prefWidth="400" /> + + <HBox spacing="10" alignment="CENTER"> + <!-- Dealer Hand --> + <VBox fx:id="dealerHand" spacing="5"> + <Label text="Dealer Hand"/> + <!-- Dealer's cards will be dynamically added here --> + </VBox> + + <!-- Player Hands --> + <VBox spacing="10" fx:id="playerHands"> + <!-- Player hand sections will be dynamically added here based on the number of players --> + </VBox> + </HBox> + + <!-- Game Controls --> + <HBox spacing="10" alignment="CENTER"> + <Button text="Hit" onAction="#hit"/> + <Button text="Stand" onAction="#stand"/> + </HBox> + </VBox> +</VBox> diff --git a/src/main/resources/hepia/main-ui.fxml b/src/main/resources/hepia/main-ui.fxml deleted file mode 100644 index 8e1e780..0000000 --- a/src/main/resources/hepia/main-ui.fxml +++ /dev/null @@ -1,11 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<?import javafx.scene.control.*?> -<?import javafx.scene.layout.*?> - -<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hepia.MainUIController"> - <children> - <Button layoutX="190.0" layoutY="211.0" mnemonicParsing="false" onAction="#handleButton" prefHeight="78.0" prefWidth="221.0" text="Click" /> - <Label fx:id="label" layoutX="223.0" layoutY="119.0" prefHeight="18.0" prefWidth="155.0" text="Label" textAlignment="JUSTIFY" /> - </children> -</AnchorPane> -- GitLab