update JavaFX's tp

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

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?> <?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
@ -8,17 +9,24 @@
<BorderPane style="-fx-background-color: #ecf0f1;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.Fenetre"> <BorderPane style="-fx-background-color: #ecf0f1;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.Fenetre">
<top> <top>
<Text fx:id="topText" text="Fight de Cedric et Jerome" BorderPane.alignment="CENTER" style="-fx-background-color: #3498db;" /> <Text fx:id="topText" style="-fx-background-color: #3498db;" text="Fight de Cedric et Jerome" BorderPane.alignment="CENTER" />
</top> </top>
<center> <center>
<HBox alignment="CENTER" spacing="10" style="-fx-background-color: #ecf0f1;"> <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" /> <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" /> <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" /> <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> </HBox>
</center> </center>
<bottom> <bottom>
<Text fx:id="bottomText" BorderPane.alignment="CENTER" style="-fx-background-color: #e74c3c;" /> <Text fx:id="bottomText" style="-fx-background-color: #e74c3c;" BorderPane.alignment="CENTER" />
</bottom> </bottom>
</BorderPane> </BorderPane>

@ -5,17 +5,15 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.io.IOException;
public class Launch extends Application { public class Launch extends Application {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Fenetre.fxml"));
public Launch() throws IOException {
}
@Override @Override
public void start(Stage primaryStage) throws Exception { 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(); primaryStage.show();
} }
} }

@ -1,12 +1,26 @@
package model; 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 { public class Player {
private static final String BASE_PLAYER_NAME_DEFAULT = "Player "; private static final String BASE_PLAYER_NAME_DEFAULT = "Player ";
private static int num = 0; private static int num = 0;
private StringProperty name = new SimpleStringProperty();
private String name; public String getName() { return name.get(); }
private int totalScore; public StringProperty nameProperty() { return name; }
private int currentScore; 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) { public Player(String name) {
setName(name); setName(name);
@ -16,30 +30,6 @@ public class Player {
this(BASE_PLAYER_NAME_DEFAULT + ++num); 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 @Override
public String toString() { public String toString() {
return name + " (" + totalScore + ") has currently " + currentScore; return name + " (" + totalScore + ") has currently " + currentScore;

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

Loading…
Cancel
Save