fix: correction bug

This commit is contained in:
2025-12-19 16:59:57 +01:00
parent 25c3e8dbd7
commit 1bbccc4acc
8 changed files with 56 additions and 64 deletions

View File

@@ -1,9 +1,3 @@
import java.awt.Color;
import model.Game;
import model.car.BasicCar;
import model.car.DrunkCar;
import model.car.HybridCar;
import model.map.Map;
import visual.SelectionView;
@@ -24,7 +18,8 @@ public class Main {
SelectionView game = new SelectionView(3);
game.getGameBuilder()
game.getSelection()
.getGameBuilder()
.track("Piste Formule 1", 1000, 500, 1, 1)
.rankboard("Score", 200, 200, 0, 510)
.dashboards(300, 200, 1000, 0)

View File

@@ -2,14 +2,14 @@ package model;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import model.car.Car;
import model.map.Map;
import visual.*;
public class Game {
@FunctionalInterface
public static interface Observer {
public static interface Observer extends Consumer<Game> {
/**
*
* @return si False le programme s'arrete, sinon il continue
@@ -18,12 +18,21 @@ public class Game {
}
public static class Builder {
private final List<Game.Observer> OBSERVERS = new ArrayList<>();
private static record DashboardConfig(int width, int height, int x, int y) {
public void initDashboard(Builder builder, Car car, int index) {
builder.dashboard(car, car.getName(), width, height, x, y + (height * index));
}
}
private final List<Car> cars = new ArrayList<>();
private final List<GameView> views = new ArrayList<>();
private DashboardConfig dashboardConfig = null;
private int time = 500;
private Map map = null;
public Builder car(Car car) {
this.OBSERVERS.add(car);
cars.add(car);
return this;
}
@@ -37,50 +46,44 @@ public class Game {
return this;
}
public Builder rankboard(String title, int width,
int height, int x, int y) {
this.OBSERVERS.add(new RankboardView(null, title, width, height, x, y));
public Builder rankboard(String title, int width, int height, int x, int y) {
views.add(new RankboardView(null, title, width, height, x, y));
return this;
}
public Builder track(String title, int width,
int height, int x, int y) {
this.OBSERVERS.add(new TrackView(null, title, width, height, x, y));
public Builder track(String title, int width, int height, int x, int y) {
views.add(new TrackView(null, title, width, height, x, y));
return this;
}
public Builder dashboard(Car car, String title, int width,
int height, int x, int y) {
this.OBSERVERS.add(new DashboardView(null, car, title, width, height, x, y));
public Builder dashboard(Car car, String title, int width, int height, int x, int y) {
views.add(new DashboardView(null, car, title, width, height, x, y));
return this;
}
public Builder dashboards(int width, int height, int x, int y) {
List<Car> cars = OBSERVERS.stream()
.filter(o -> o instanceof Car)
.map(o -> (Car) o)
.toList();
int index = 0;
for (Car car : cars) {
dashboard(car, car.getName(), width, height, x, y + (height * index++));
}
dashboardConfig = new DashboardConfig(width, height, x, y);
return this;
}
public Game build() {
Game game = new Game(this.map, this.time, this.OBSERVERS);
for (Game.Observer observer : new ArrayList<>(this.OBSERVERS)) {
switch (observer) {
case GameView gameView -> {
gameView.setGame(game);
}
default -> {
}
if (dashboardConfig != null) {
int index = 0;
for (Car car : new ArrayList<>(cars)) {
dashboardConfig.initDashboard(this, car, index++);
}
}
List<Game.Observer> observers = new ArrayList<>();
observers.addAll(cars);
observers.addAll(views);
Game game = new Game(map, time, observers);
for (Game.Observer obs : new ArrayList<>(observers)) {
obs.accept(game);
}
return game;
}
}
@@ -98,16 +101,6 @@ 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) {

View File

@@ -4,6 +4,7 @@ import java.awt.Color;
import java.awt.Point;
import java.util.Random;
import model.Game;
import model.map.Circuit;
import model.map.Map;
@@ -254,7 +255,7 @@ public void reverse(boolean active) {
}
@Override
public void setMap(Map map) {
this.map = map;
public void accept(Game game) {
this.map = game.getMap();
}
}

View File

@@ -1,9 +1,8 @@
package model.car;
import java.awt.Color;
import model.Game.Observer;
import model.map.Map;
public interface Car extends Observer {
public String accelerate();
@@ -29,7 +28,4 @@ public interface Car extends Observer {
public State getState();
public Color getColor();
public void setMap(Map map);
}

View File

@@ -2,7 +2,7 @@ package model.car;
import java.awt.Color;
import model.map.Map;
import model.Game;
public abstract class CarDecorator implements Car {
protected final Car car;
@@ -77,7 +77,7 @@ public abstract class CarDecorator implements Car {
}
@Override
public void setMap(Map map) {
car.setMap(map);
public void accept(Game game) {
car.accept(game);
}
}

View File

@@ -11,6 +11,10 @@ public class Selection {
private final Game.Builder gameBuilder;
private final List<Car> cars = new ArrayList<>();
public Game.Builder getGameBuilder() {
return gameBuilder;
}
public Selection(Game.Builder gameBuilder) {
this.gameBuilder = gameBuilder;
}
@@ -66,14 +70,17 @@ public class Selection {
return car;
}
public Game.Builder select() {
private Game.Builder select() {
for (Car car : cars) {
gameBuilder.car(car);
System.out.println("hello world");
}
return gameBuilder;
}
public void run() {
new Thread(() -> select().build().run()).start();
}
public int size() {
return cars.size();
}

View File

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

View File

@@ -15,8 +15,8 @@ public class SelectionView extends GameView {
private Selection selection = new Selection(gameBuilder);
private JPanel buttonPanel = new JPanel();
public Game.Builder getGameBuilder() {
return gameBuilder;
public Selection getSelection() {
return selection;
}
public SelectionView(int ncar) {
@@ -74,7 +74,7 @@ public class SelectionView extends GameView {
JButton startBtn = new JButton("Start");
startBtn.addActionListener((ActionEvent e) -> {
frame.dispose();
new Thread(() -> selection.select().build().run()).start();
selection.run();
});
this.frame.add(startBtn, BorderLayout.SOUTH);