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 model.map.Map;
import visual.SelectionView; import visual.SelectionView;
@@ -24,7 +18,8 @@ public class Main {
SelectionView game = new SelectionView(3); SelectionView game = new SelectionView(3);
game.getGameBuilder() game.getSelection()
.getGameBuilder()
.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)
.dashboards(300, 200, 1000, 0) .dashboards(300, 200, 1000, 0)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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