update javafx's tp

master
antoine.perederii 1 year ago
parent fe3f65775b
commit bd35ce46b2

@ -1,25 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Text?>
<BorderPane xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
<right>
<Button fx:id="buttonHold" mnemonicParsing="false" text="H" BorderPane.alignment="CENTER" />
</right>
<left>
<Button fx:id="buttonRoll" mnemonicParsing="false" text="R" BorderPane.alignment="CENTER" />
</left>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.HBox?>
<BorderPane xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.Fenetre">
<top>
<Text fx:id="topText" text="Fight de Cedric et Jerome" BorderPane.alignment="CENTER" />
</top>
<bottom>
<Text fx:id="bottomText" text="Jerome à fait 1, il pert ! Gros noob !" BorderPane.alignment="CENTER" />
</bottom>
<center>
<Text fx:id="centerText" text="Jérome lance le dé, il fait 1" BorderPane.alignment="CENTER" />
<HBox spacing="10" alignment="CENTER">
<Button fx:id="buttonHold" mnemonicParsing="false" text="Hold" onAction="#onButtonHoldClicked" />
<ImageView fx:id="diceImage" fitHeight="150.0" fitWidth="150.0" preserveRatio="true" />
<Button fx:id="buttonRoll" mnemonicParsing="false" text="Roll" onAction="#onButtonRollClicked"/>
</HBox>
</center>
<bottom>
<Text fx:id="bottomText" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>

@ -2,76 +2,90 @@ package view;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import launcher.ConsoleGame;
import model.Game;
import model.Player;
import java.util.Scanner;
import java.util.Objects;
public class Fenetre {
@FXML
private Button buttonHold;
@FXML
private Button buttonRoll;
@FXML
private Text topText;
@FXML
private Text bottomText;
@FXML
private ImageView diceImage;
private boolean hasRolled = false;
private Game game;
private final Player p1 = new Player("Cedrice");
private final Player p2 = new Player("Jerome");
public void initialize() {
game = new Game();
game.addPlayer(p1);
game.addPlayer(p2);
updateUI();
}
@FXML
private Text centerText;
// private static final String PROMPT = ", enter R to roll the dice or H to hold your score: ";
// private static final String ALLOWED_KEYS = "[rRhH]";
//
// private enum Choice {ROLL, HOLD, QUIT}
////
// public Choice readChoice(String playerName) {
// if(this.buttonHold.getOnMouseClicked()) {
// return Choice.HOLD;
// } else if(this.buttonRoll.getOnMouseClicked()) {
// return Choice.ROLL;
// }
// return null;
// }
// public static void main(String[] args) {
// Game teacherFight = new Game();
// teacherFight.addPlayer(new Player("Laurent"));
// teacherFight.addPlayer(new Player("Cédric"));
//
// System.out.println("Laurent and Cédric are about to start a dice fight !");
//
// while (!teacherFight.isGameOver()) {
// Player currentPlayer = teacherFight.getCurrentPlayer();
// System.out.println(currentPlayer);
// ConsoleGame.Choice choice = readChoice(currentPlayer.getName());
// switch (choice) {
// case ROLL -> {
// teacherFight.rollDice();
// int diceValue = 0;
// try {
// diceValue = teacherFight.getDiceValue();
// } catch (IllegalAccessException e) {
// teacherFight.rollDice();
// }
// System.out.println(currentPlayer.getName() + " is rolling the dice… and get a " + diceValue);
// if (diceValue == Game.LOSE_DICE_VALUE)
// System.out.println(currentPlayer.getName() + " loses his turn.");
// }
// case HOLD -> {
// teacherFight.passToNextPlayer();
// System.out.println(currentPlayer.getName() + " decides to hold his score and has now " + currentPlayer.getTotalScore());
// }
// case QUIT -> {
// System.out.println(currentPlayer.getName() + " quits.");
// return;
// }
// }
// }
//
// Player winner = teacherFight.getCurrentPlayer();
// System.out.println(winner.getName() + " wins the game !");
// }
private void onButtonHoldClicked() {
game.passToNextPlayer();
hasRolled = true;
updateUI();
}
@FXML
private void onButtonRollClicked() {
hasRolled = true;
game.rollDice();
int diceValue = 0;
try {
diceValue = game.getDiceValue();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
String imagePath = "/dice/" + diceValue + ".png";
Image image = new Image(getClass().getResourceAsStream(imagePath));
diceImage.setImage(image);
updateUI();
// Si le dé affiche la valeur 1, passez au joueur suivant et mettre à jour l'interface graphique.
if (diceValue == 1) {
topText.setText("Le joueur précedent à perdue\n" + game.getCurrentPlayer().getName() + ", press R to roll the dice or H to hold your score.");
imagePath = "/dice/0.png";
image = new Image(getClass().getResourceAsStream(imagePath));
diceImage.setImage(image);
hasRolled = false;
}
}
private void updateUI() {
Player currentPlayer = game.getCurrentPlayer();
topText.setText(currentPlayer.getName() + ", press R to roll the dice or H to hold your score.");
bottomText.setText(currentPlayer.getName() + "(" + currentPlayer.getTotalScore() + ")" + " has currently " + currentPlayer.getCurrentScore());
if (!hasRolled) {
String imagePath = "/dice/0.png";
Image image = new Image(getClass().getResourceAsStream(imagePath));
diceImage.setImage(image);
}
}
}

Loading…
Cancel
Save