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.util.List;
import model.Game;
import model.car.BasicCar;
import model.car.DrunkCar;
import model.car.HybridCar;
import model.map.Map;
public class Main {
@@ -22,7 +22,7 @@ public class Main {
});
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)))
.track("Piste Formule 1", 1000, 500, 1, 1)
.rankboard("Score", 200, 200, 0, 510)

View File

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

View File

@@ -134,10 +134,8 @@ public class BasicCar implements Car {
* @param move nombre de positions à avancer
* @return cette même instance (pour chaînage fluide)
*/
@Override
public void move() {
if (decreaseDamage()) {
System.out.println(NAME + " est en\taccident " + damageRound);
return;
}
@@ -150,7 +148,6 @@ public class BasicCar implements Car {
Circuit element = map.getElement(point.x, point.y);
if (hasAccident(element, jump)) {
System.out.println(NAME + " a un\taccident");
setDamage();
return;
}
@@ -167,7 +164,6 @@ public class BasicCar implements Car {
*
* @return cette même instance pour chaînage
*/
@Override
public void consumeFuel() {
fuel = state.fuelConsumption(fuel);
if (fuel < 0)
@@ -190,6 +186,7 @@ public class BasicCar implements Car {
public boolean apply() {
if (fuel > 0) {
move();
consumeFuel();
}
return !hasFinished();
}
@@ -203,7 +200,7 @@ public class BasicCar implements Car {
* @return la position actuelle dans la boucle
*/
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 consumeFuel();
public void reverse(boolean active);
public Color getColor();
@@ -31,4 +29,6 @@ public interface Car extends Observer {
public State getState();
public void setMap(Map map);
public void consumeFuel();
}

View File

@@ -26,16 +26,9 @@ public abstract class CarDecorator implements Car {
car.move();
}
@Override
public void consumeFuel() {
car.consumeFuel();
}
@Override
public boolean apply() {
boolean response = car.apply();
car.consumeFuel();
return response;
return car.apply();
}
@Override
@@ -82,4 +75,9 @@ public abstract class CarDecorator implements Car {
public void setMap(Map 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) {
super(car);
}
@Override
// 50% : fait la bonne action,
// 50% : fait l'inverse
public void move() {
@Override
public boolean apply() {
car.reverse(RANDOM.nextBoolean());
car.move();
car.apply();
car.reverse(false);
return true;
}
}

View File

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

View File

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

View File

@@ -62,10 +62,12 @@ public class Dashboard extends GameView {
this.car = car;
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
frame.setBackground(car.getColor());

View File

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

View File

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