ajout des actions accélérer/rallentir et correction de la condition de dérapage (>=) (Car)

This commit is contained in:
Chahrazad650
2025-12-13 08:00:08 +01:00
parent c222ec3c8d
commit bf41c6269e
2 changed files with 46 additions and 2 deletions

View File

@@ -47,7 +47,7 @@ public class Car {
public static enum State {
/**
* L'état NORMAL du Vehicule avance selon un chiffre au alentour de 1 à 6 cases
* par tour. Il consomme 6 unités de carburant à chaque tour. Si l'on
* par tour. Il consomme 2 unités de carburant à chaque tour. Si l'on
* accelere, il passe à l'état BOOST. Si on Rallenti, il passe à l'état LOW.
*/
// @formatter:off
@@ -202,6 +202,44 @@ public class Car {
return fuel;
}
/** Retourne l'état courant de la voiture. */
public State getState() {
return state;
}
/** Clique sur "Accelerer" : change d'état et retourne un message (si nécessaire). */
public String accelerate() {
// Si endommagée => l'énoncé dit qu'on ne peut pas bouger
if (state.isDamaged()) {
return "Voiture endommagée : impossible d'accélérer";
}
State next = state.accelerate();
// Énoncé : en BOOST, si on accélère encore => message "déjà max"
if (state == State.BOOST && next == State.BOOST) {
return "Déjà à la vitesse maximale";
}
state = next;
return "";
}
/** Clique sur "Rallentir" : change d'état et retourne un message (si nécessaire). */
public String decelerate() {
if (state.isDamaged()) {
return "Voiture endommagée : impossible de ralentir";
}
State next = state.decelerate();
// Énoncé : en STOPPED, si on ralentit encore => message "déjà arrêtée"
if (state == State.STOPPED && next == State.STOPPED) {
return "Déjà arrêtée";
}
state = next;
return "";
}
/**
* Fait avancer la voiture d'un certain nombre de positions.
* <p>
@@ -227,7 +265,7 @@ public class Car {
Point point = MAP.getPath(pos);
Map.Circuit element = MAP.getElement(point.x, point.y);
if (element.isYRoad() && element.getValue() < jump) {
if (element.isYRoad() && element.getValue() <= jump) {
System.out.println(NAME + " a un\taccident");
setDamage();
return;

View File

@@ -54,8 +54,11 @@ public class Dashboard extends GameView {
*/
public Dashboard(Game game, Car car, String title, int width, int height, int x, int y) {
super(game, "Dashboard: " + title, width, height, x, y);
this.car = car;
// Fond de la fenêtre = couleur de la voiture
frame.setBackground(car.getColor());
@@ -64,6 +67,8 @@ public class Dashboard extends GameView {
centerPanel.add(infoLabel);
centerPanel.add(stateLabel);
centerPanel.add(messageLabel);
//pour rendre le panel transparent
centerPanel.setOpaque(false);
// ----- Partie boutons -----
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
@@ -71,6 +76,7 @@ public class Dashboard extends GameView {
buttonPanel.add(pauseButton);
buttonPanel.add(accelerateButton);
setLayout(new BorderLayout());
add(centerPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);