feat: correction plupart des bug

- TODO: hybridCar a regler
This commit is contained in:
2025-12-18 21:00:13 +01:00
parent d210eedb5a
commit 6c75a5363f
11 changed files with 44 additions and 38 deletions

View File

@@ -1,9 +1,9 @@
import java.awt.Color; import java.awt.Color;
import java.util.List;
import model.Game; import model.Game;
import model.car.BasicCar; import model.car.BasicCar;
import model.car.DrunkCar; import model.car.DrunkCar;
import model.car.HybridCar;
import model.map.Map; import model.map.Map;
public class Main { public class Main {
@@ -22,7 +22,7 @@ public class Main {
}); });
Game game = new Game.Builder() Game game = new Game.Builder()
.car(new BasicCar("Luwik", Color.RED)) .car(new HybridCar(new BasicCar("Luwik", Color.RED)))
.car(new DrunkCar(new BasicCar("Charazade", Color.PINK))) .car(new DrunkCar(new BasicCar("Charazade", Color.PINK)))
.track("Piste Formule 1", 1000, 500, 1, 1) .track("Piste Formule 1", 1000, 500, 1, 1)
.rankboard("Score", 200, 200, 0, 510) .rankboard("Score", 200, 200, 0, 510)

View File

@@ -69,24 +69,18 @@ public class Game {
} }
public Game build() { public Game build() {
Game game = new Game(); Game game = new Game(this.map, this.time, this.OBSERVERS);
for (Game.Observer observer : this.OBSERVERS) { for (Game.Observer observer : new ArrayList<>(this.OBSERVERS)) {
switch (observer) { switch (observer) {
case GameView gameView -> { case GameView gameView -> {
gameView.setGame(game); gameView.setGame(game);
} }
case Car car -> {
car.setMap(map);
}
default -> { default -> {
} }
} }
} }
game.map = this.map;
game.time = this.time;
game.observers = this.OBSERVERS;
return game; return game;
} }
} }
@@ -104,6 +98,16 @@ public class Game {
this.map = map; this.map = map;
this.time = time; this.time = time;
this.observers = observer; this.observers = observer;
for (Game.Observer obs : this.observers) {
switch (obs) {
case Car car -> {
car.setMap(map);
}
default -> {
}
}
}
} }
public Game addObserver(Observer o) { public Game addObserver(Observer o) {
@@ -153,6 +157,7 @@ public class Game {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
} }
public synchronized boolean togglePause() { public synchronized boolean togglePause() {

View File

@@ -134,10 +134,8 @@ public class BasicCar implements Car {
* @param move nombre de positions à avancer * @param move nombre de positions à avancer
* @return cette même instance (pour chaînage fluide) * @return cette même instance (pour chaînage fluide)
*/ */
@Override
public void move() { public void move() {
if (decreaseDamage()) { if (decreaseDamage()) {
System.out.println(NAME + " est en\taccident " + damageRound);
return; return;
} }
@@ -150,7 +148,6 @@ public class BasicCar implements Car {
Circuit element = map.getElement(point.x, point.y); Circuit element = map.getElement(point.x, point.y);
if (hasAccident(element, jump)) { if (hasAccident(element, jump)) {
System.out.println(NAME + " a un\taccident");
setDamage(); setDamage();
return; return;
} }
@@ -167,7 +164,6 @@ public class BasicCar implements Car {
* *
* @return cette même instance pour chaînage * @return cette même instance pour chaînage
*/ */
@Override
public void consumeFuel() { public void consumeFuel() {
fuel = state.fuelConsumption(fuel); fuel = state.fuelConsumption(fuel);
if (fuel < 0) if (fuel < 0)
@@ -190,6 +186,7 @@ public class BasicCar implements Car {
public boolean apply() { public boolean apply() {
if (fuel > 0) { if (fuel > 0) {
move(); move();
consumeFuel();
} }
return !hasFinished(); return !hasFinished();
} }
@@ -203,7 +200,7 @@ public class BasicCar implements Car {
* @return la position actuelle dans la boucle * @return la position actuelle dans la boucle
*/ */
public int getPosition() { public int getPosition() {
return pos; return Math.floorMod(pos, map.getPathSize());
} }
/** /**

View File

@@ -12,8 +12,6 @@ public interface Car extends Observer {
public void move(); public void move();
public void consumeFuel();
public void reverse(boolean active); public void reverse(boolean active);
public Color getColor(); public Color getColor();
@@ -31,4 +29,6 @@ public interface Car extends Observer {
public State getState(); public State getState();
public void setMap(Map map); public void setMap(Map map);
public void consumeFuel();
} }

View File

@@ -26,16 +26,9 @@ public abstract class CarDecorator implements Car {
car.move(); car.move();
} }
@Override
public void consumeFuel() {
car.consumeFuel();
}
@Override @Override
public boolean apply() { public boolean apply() {
boolean response = car.apply(); return car.apply();
car.consumeFuel();
return response;
} }
@Override @Override
@@ -82,4 +75,9 @@ public abstract class CarDecorator implements Car {
public void setMap(Map map) { public void setMap(Map map) {
car.setMap(map); car.setMap(map);
} }
@Override
public void consumeFuel() {
car.consumeFuel();
}
} }

View File

@@ -20,13 +20,14 @@ public class DrunkCar extends CarDecorator {
public DrunkCar(Car car) { public DrunkCar(Car car) {
super(car); super(car);
} }
@Override
// 50% : fait la bonne action, // 50% : fait la bonne action,
// 50% : fait l'inverse // 50% : fait l'inverse
public void move() { @Override
public boolean apply() {
car.reverse(RANDOM.nextBoolean()); car.reverse(RANDOM.nextBoolean());
car.move(); car.apply();
car.reverse(false); car.reverse(false);
return true;
} }
} }

View File

@@ -1,7 +1,5 @@
package model.car; package model.car;
import java.awt.Color;
/** /**
* HybridCar = décorateur "voiture hybride". * HybridCar = décorateur "voiture hybride".
* *
@@ -22,17 +20,22 @@ public class HybridCar extends CarDecorator {
return energy; return energy;
} }
// TODO
@Override @Override
public boolean apply() { public boolean apply() {
boolean response = car.apply(); car.move();
consumeFuel();
return true;
}
// TODO
@Override
public void consumeFuel() {
if (energy > 0) { if (energy > 0) {
energy -= 10; energy -= 10;
} else { } else {
car.consumeFuel(); car.consumeFuel();
} }
return response;
} }
@Override @Override

View File

@@ -1,7 +1,5 @@
package model.car; package model.car;
import java.awt.Color;
/** /**
* Décorateur Sound : * Décorateur Sound :
* affiche un message sonore quand la voiture accélère. * affiche un message sonore quand la voiture accélère.

View File

@@ -62,10 +62,12 @@ public class Dashboard extends GameView {
this.car = car; this.car = car;
if (car != null && game != null) if (car != null && game != null)
init(); init(game);
} }
private void init() { @Override
protected void init(Game game) {
super.init(game);
// Fond de la fenêtre = couleur de la voiture // Fond de la fenêtre = couleur de la voiture
frame.setBackground(car.getColor()); frame.setBackground(car.getColor());

View File

@@ -34,6 +34,7 @@ public abstract class GameView extends JComponent implements Game.Observer {
public void setGame(Game game) { public void setGame(Game game) {
this.game = game; this.game = game;
init(game);
} }
/** /**

View File

@@ -57,6 +57,7 @@ public class Rankboard extends GameView {
* </p> * </p>
*/ */
private void updateRankText() { private void updateRankText() {
if (cars == null) return;
// cloner pour de modifier la classe principale // cloner pour de modifier la classe principale
List<Car> cars_clone = new ArrayList<>(cars); List<Car> cars_clone = new ArrayList<>(cars);
cars_clone.sort(Comparator.comparingInt(Car::getScore).reversed()); cars_clone.sort(Comparator.comparingInt(Car::getScore).reversed());