update JavaFX's tp

master
antoine.perederii 1 year ago
parent 6d6cc5b013
commit b5a5d619d4

@ -1,24 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Text?>
<BorderPane style="-fx-background-color: #ecf0f1;" 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" style="-fx-background-color: #3498db;" />
</top>
<center>
<HBox alignment="CENTER" spacing="10" style="-fx-background-color: #ecf0f1;">
<Button fx:id="buttonHold" mnemonicParsing="false" onAction="#onButtonHoldClicked" style="-fx-background-color: #f39c12;" text="Hold" />
<ImageView fx:id="diceImage" fitHeight="150.0" fitWidth="150.0" preserveRatio="true" />
<Button fx:id="buttonRoll" mnemonicParsing="false" onAction="#onButtonRollClicked" style="-fx-background-color: #2ecc71;" text="Roll" />
</HBox>
</center>
<bottom>
<Text fx:id="bottomText" BorderPane.alignment="CENTER" style="-fx-background-color: #e74c3c;" />
</bottom>
<top>
<Text fx:id="topText" style="-fx-background-color: #3498db;" text="Fight de Cedric et Jerome" BorderPane.alignment="CENTER" />
</top>
<center>
<HBox alignment="CENTER" spacing="10" style="-fx-background-color: #ecf0f1;">
<Button fx:id="buttonHold" mnemonicParsing="false" onAction="#onButtonHoldClicked" style="-fx-background-color: #f39c12;" text="Hold" />
<ImageView fx:id="diceImage" fitHeight="150.0" fitWidth="150.0" preserveRatio="true" />
<Button fx:id="buttonRoll" mnemonicParsing="false" onAction="#onButtonRollClicked" style="-fx-background-color: #2ecc71;" text="Roll" />
<BorderPane prefHeight="200.0" prefWidth="200.0">
<top>
<TextField fx:id="joueur1" BorderPane.alignment="CENTER" />
</top>
<center>
<TextField fx:id="joueur2" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</HBox>
</center>
<bottom>
<Text fx:id="bottomText" style="-fx-background-color: #e74c3c;" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>

@ -5,17 +5,15 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Launch extends Application {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Fenetre.fxml"));
public Launch() throws IOException {
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(root));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Fenetre.fxml"));
Parent root = loader.load();
primaryStage.setScene(new Scene(root, 700, 500));
primaryStage.show();
}
}

@ -1,12 +1,26 @@
package model;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Player {
private static final String BASE_PLAYER_NAME_DEFAULT = "Player ";
private static int num = 0;
private String name;
private int totalScore;
private int currentScore;
private StringProperty name = new SimpleStringProperty();
public String getName() { return name.get(); }
public StringProperty nameProperty() { return name; }
private void setName(String name) { this.name.set(name); }
private IntegerProperty totalScore = new SimpleIntegerProperty();
public Integer getTotalScore() { return totalScore.get(); }
public IntegerProperty totalScoreProperty() { return totalScore; }
public void setTotalScore(Integer totalScore) { this.totalScore.set(totalScore); }
private IntegerProperty currentScore = new SimpleIntegerProperty();
public Integer getCurrentScore() { return currentScore.get(); }
public IntegerProperty currentScoreProperty() { return currentScore; }
public void setCurrentScore(Integer currentScore) { this.currentScore.set(currentScore); }
public Player(String name) {
setName(name);
@ -16,30 +30,6 @@ public class Player {
this(BASE_PLAYER_NAME_DEFAULT + ++num);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int score) {
totalScore = score;
}
public int getCurrentScore() {
return currentScore;
}
public void setCurrentScore(int score) {
currentScore = score;
}
@Override
public String toString() {
return name + " (" + totalScore + ") has currently " + currentScore;

@ -3,13 +3,14 @@ package view;
import data.Stub;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import model.Game;
import model.Player;
import java.util.Objects;
import java.util.List;
public class Fenetre {
@ -24,6 +25,10 @@ public class Fenetre {
@FXML
private Text bottomText;
@FXML
private TextField joueur1;
@FXML
private TextField joueur2;
@FXML
private ImageView diceImage;
@ -32,66 +37,68 @@ public class Fenetre {
@FXML
private void onButtonHoldClicked() {
joueur1.setDisable(true);
joueur2.setDisable(true);
// hasRolled = true;
game.passToNextPlayer();
hasRolled = true;
updateUI();
}
@FXML
private void onButtonRollClicked() {
hasRolled = true;
joueur1.setDisable(true);
joueur2.setDisable(true);
// 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);
int diceValue = 0;
try {
// if(hasRolled) {
diceValue = game.getDiceValue();
// }
} catch (IllegalAccessException e) {
topText.setStyle("-fx-fill: black;");
topText.textProperty().unbind();
topText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(", press R to roll the dice or H to hold your score."));
bottomText.textProperty().unbind();
bottomText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(" (").concat(game.getCurrentPlayer().totalScoreProperty()).concat(") has currently ").concat(game.getCurrentPlayer().currentScoreProperty()));
// e.printStackTrace();
return;
}
// Vérifier si un joueur a gagné
if (currentPlayer.getTotalScore() >= Game.SCORE_TO_WIN) {
topText.setText(currentPlayer.getName() + " wins the game!");
topText.setStyle("-fx-fill: #e74c3c;"); // Définir la couleur du texte en rouge
if (currentPlayer.getTotalScore() == Game.LOSE_DICE_VALUE) {
game.passToNextPlayer();
topText.textProperty().unbind();
topText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(" à perdue\n").concat(game.getCurrentPlayer().nameProperty()).concat(", press R to roll the dice or H to hold your score."));
// hasRolled = false;
} else if (currentPlayer.getTotalScore() >= Game.SCORE_TO_WIN) {
topText.textProperty().unbind();
topText.textProperty().bind(currentPlayer.nameProperty().concat(" wins the game"));
topText.setStyle("-fx-fill: #e74c3c;");
buttonRoll.setDisable(true);
buttonHold.setDisable(true);
} else {
// Si le jeu n'est pas terminé, utilisez la couleur par défaut ou une autre couleur de votre choix
topText.setStyle("-fx-fill: black;");
topText.textProperty().unbind();
topText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(", press R to roll the dice or H to hold your score."));
bottomText.textProperty().unbind();
bottomText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(" (").concat(game.getCurrentPlayer().totalScoreProperty()).concat(") has currently ").concat(game.getCurrentPlayer().currentScoreProperty()));
}
String imagePath = "/dice/" + diceValue + ".png";
Image image = new Image(imagePath);
diceImage.setImage(image);
}
public void initialize() {
// topText.textProperty().bind(game.getCurrentPlayer());
joueur1.textProperty().bindBidirectional(game.getCurrentPlayer().nameProperty());
game.passToNextPlayer();
joueur2.textProperty().bindBidirectional(game.getCurrentPlayer().nameProperty());
updateUI();
}
}

Loading…
Cancel
Save