feat: ajout Menu selection

This commit is contained in:
2025-12-19 16:18:40 +01:00
parent 203ed3c94d
commit 25c3e8dbd7
12 changed files with 254 additions and 80 deletions

View File

@@ -5,6 +5,7 @@ import model.car.BasicCar;
import model.car.DrunkCar;
import model.car.HybridCar;
import model.map.Map;
import visual.SelectionView;
public class Main {
public static void main(String[] args) throws InterruptedException {
@@ -21,14 +22,12 @@ public class Main {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
});
Game game = new Game.Builder()
.car(new HybridCar(new BasicCar("Luwik", Color.RED)))
.car(new DrunkCar(new BasicCar("Charazade", Color.PINK)))
SelectionView game = new SelectionView(3);
game.getGameBuilder()
.track("Piste Formule 1", 1000, 500, 1, 1)
.rankboard("Score", 200, 200, 0, 510)
.dashboards(300, 200, 1000, 0)
.map(map)
.build();
game.run();
.map(map);
}
}

View File

@@ -39,19 +39,19 @@ public class Game {
public Builder rankboard(String title, int width,
int height, int x, int y) {
this.OBSERVERS.add(new Rankboard(null, title, width, height, x, y));
this.OBSERVERS.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 Track(null, title, width, height, x, y));
this.OBSERVERS.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 Dashboard(null, car, title, width, height, x, y));
this.OBSERVERS.add(new DashboardView(null, car, title, width, height, x, y));
return this;
}

View File

@@ -195,6 +195,11 @@ public class BasicCar implements Car {
isConsuming = active;
}
@Override
public Car remove() {
return this;
}
@Override
public void reverse(boolean active) {
movement = (active) ? -1 : 1;

View File

@@ -14,6 +14,8 @@ public interface Car extends Observer {
public void consumption(boolean active);
public Car remove();
public String getName();
public int getPosition();
@@ -25,6 +27,7 @@ public interface Car extends Observer {
public int getFuel();
public State getState();
public Color getColor();
public void setMap(Map map);

View File

@@ -11,6 +11,11 @@ public abstract class CarDecorator implements Car {
this.car = car;
}
@Override
public Car remove() {
return car;
}
@Override
public String accelerate() {
return car.accelerate();

View File

@@ -1,7 +1,5 @@
package model.car;
import java.util.Random;
/**
* DrunkCar = décorateur "pilote ivre".
*
@@ -14,8 +12,7 @@ import java.util.Random;
* sans modifier la classe Car.
*/
public class DrunkCar extends CarDecorator {
/** Générateur de nombres aléatoires pour simuler la progression */
protected static final Random RANDOM = new Random();
private boolean reverse = false;
public DrunkCar(Car car) {
super(car);
@@ -25,9 +22,9 @@ public class DrunkCar extends CarDecorator {
// 50% : fait l'inverse
@Override
public boolean apply() {
car.reverse(RANDOM.nextBoolean());
car.reverse(reverse);
car.apply();
car.reverse(false);
reverse = !reverse;
return true;
}
}

View File

@@ -0,0 +1,80 @@
package model.car;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import model.Game;
public class Selection {
private final Game.Builder gameBuilder;
private final List<Car> cars = new ArrayList<>();
public Selection(Game.Builder gameBuilder) {
this.gameBuilder = gameBuilder;
}
public List<Car> getCars() {
return new ArrayList<>(cars);
}
public Car addCar(String name, Color color) {
Car car = new BasicCar(name, color);
cars.add(car);
return car;
}
public void removeCar(Car car) {
cars.remove(car);
}
private Car addDecorator(Function<Car, Car> fn, Car car) throws IllegalArgumentException {
int index = cars.indexOf(car);
if (index < 0) {
throw new IllegalArgumentException(
"Aucune voiture dans la classe interne.");
}
car = fn.apply(car);
cars.set(index, car);
return car;
}
public Car addHybridCarDecorator(Car car) throws IllegalArgumentException {
return addDecorator(HybridCar::new, car);
}
public Car addDrunkCarDecorator(Car car) throws IllegalArgumentException {
return addDecorator(DrunkCar::new, car);
}
public Car addBoostCarDecorator(Car car) throws IllegalArgumentException {
return addDecorator(BoostCar::new, car);
}
public Car removeDecorator(Car car) throws IllegalArgumentException {
int index = cars.indexOf(car);
if (index < 0) {
throw new IllegalArgumentException(
"Aucune voiture dans la classe interne.");
}
car = car.remove();
cars.set(index, car);
return car;
}
public Game.Builder select() {
for (Car car : cars) {
gameBuilder.car(car);
System.out.println("hello world");
}
return gameBuilder;
}
public int size() {
return cars.size();
}
}

View File

@@ -24,7 +24,7 @@ import model.car.HybridCar;
* - un bouton Accelerer
* - un bouton Rallentir
*/
public class Dashboard extends GameView {
public class DashboardView extends GameView {
/** Label affichant carburant et nombre de tours */
private final JLabel infoLabel = new JLabel();
@@ -58,7 +58,7 @@ public class Dashboard extends GameView {
* @param x position horizontale de la fenêtre
* @param y position verticale de la fenêtre
*/
public Dashboard(Game game, Car car, String title, int width, int height, int x, int y) {
public DashboardView(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;

View File

@@ -66,6 +66,7 @@ public abstract class GameView extends JComponent implements Game.Observer {
// mettre un layout (la disposition des elements de la fenetre)
frame.setLayout(new BorderLayout());
if (game != null)
game.addObserver(this);
}

View File

@@ -18,7 +18,7 @@ import model.car.Car;
* Les scores sont triés du plus grand au plus petit.
* </p>
*/
public class Rankboard extends GameView {
public class RankboardView extends GameView {
/** Liste des voitures à afficher */
List<Car> cars;
@@ -43,7 +43,7 @@ public class Rankboard extends GameView {
* @param x Position horizontale de la fenêtre
* @param y Position verticale de la fenêtre
*/
public Rankboard(Game game, String title, int width, int height, int x, int y) {
public RankboardView(Game game, String title, int width, int height, int x, int y) {
super(game, title, width, height, x, y);
if (game != null)
init(game);

View File

@@ -0,0 +1,84 @@
package visual;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import model.Game;
import model.car.Car;
import model.car.Selection;
public class SelectionView extends GameView {
private Game.Builder gameBuilder = new Game.Builder();
private Selection selection = new Selection(gameBuilder);
private JPanel buttonPanel = new JPanel();
public Game.Builder getGameBuilder() {
return gameBuilder;
}
public SelectionView(int ncar) {
super(null, "Menu de sélection", 500, 300, 400, 400);
init(null); // forcer le init
buttonPanel.setLayout(new GridLayout(0, 3, 5, 5));
frame.add(buttonPanel, BorderLayout.CENTER);
for (int i = 1; i <= ncar; i++) {
Car car = selection.addCar("Car " + i, Color.getHSBColor(i / 3f, 0.8f, 0.8f));
// bouton Hybrid
JButton hybridBtn = new JButton("Hybrid " + car.getName());
// bouton Drunk
JButton drunkBtn = new JButton("Drunk " + car.getName());
// bouton boost
JButton boostBtn = new JButton("Boost " + car.getName());
final int I = i;
hybridBtn.addActionListener((ActionEvent e) -> {
try {
Car current = selection.getCars().get(I-1);
selection.addHybridCarDecorator(current);
hybridBtn.setEnabled(false);
} catch (Exception ex) {
ex.printStackTrace();
}
});
drunkBtn.addActionListener((ActionEvent e) -> {
try {
Car current = selection.getCars().get(I-1);
selection.addDrunkCarDecorator(current);
drunkBtn.setEnabled(false);
} catch (Exception ex) {
ex.printStackTrace();
}
});
boostBtn.addActionListener((ActionEvent e) -> {
try {
Car current = selection.getCars().get(I-1);
selection.addBoostCarDecorator(current);
boostBtn.setEnabled(false);
} catch (Exception ex) {
ex.printStackTrace();
}
});
buttonPanel.add(hybridBtn);
buttonPanel.add(drunkBtn);
buttonPanel.add(boostBtn);
}
JButton startBtn = new JButton("Start");
startBtn.addActionListener((ActionEvent e) -> {
frame.dispose();
new Thread(() -> selection.select().build().run()).start();
});
this.frame.add(startBtn, BorderLayout.SOUTH);
frame.revalidate();
frame.repaint();
}
}

View File

@@ -23,7 +23,7 @@ import model.map.Map;
* est dessinée selon son type. Les voitures sont superposées sur la grille.
* </p>
*/
public class Track extends GameView {
public class TrackView extends GameView {
/** La carte du circuit */
private Map map;
/** Liste des voitures à dessiner */
@@ -49,7 +49,7 @@ public class Track extends GameView {
* @param x Position X de la fenêtre
* @param y Position Y de la fenêtre
*/
public Track(Game game, String title, int width, int height, int x, int y) {
public TrackView(Game game, String title, int width, int height, int x, int y) {
super(game, title, width, height, x, y);
if (game != null)
init(game);