From 0b60550e0b049e7cfd1cdf5c25053154cc3d21fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sun, 9 Nov 2025 16:02:43 +0100 Subject: [PATCH] feat: ajout et correction multiple classe --- src/Dashboard.java | 3 + src/Game.java | 247 ++++++++++++++++++++++++++++----------------- src/Main.java | 33 +++--- src/Rankboard.java | 10 +- src/Track.java | 14 ++- 5 files changed, 195 insertions(+), 112 deletions(-) diff --git a/src/Dashboard.java b/src/Dashboard.java index 2f27b07..7a2f1cd 100644 --- a/src/Dashboard.java +++ b/src/Dashboard.java @@ -54,6 +54,7 @@ public class Dashboard extends GameView { isPaused = fn.get(); updateButtonText(); + GameView.update(); } }; @@ -68,5 +69,7 @@ public class Dashboard extends GameView + "Nombre de Tour: " + car.getRound() + "" ); + + updateButtonText(); } } diff --git a/src/Game.java b/src/Game.java index 31ff090..46682b8 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,95 +1,169 @@ -// import java.awt.Color; -// import java.util.ArrayList; +import java.awt.Color; +import java.util.ArrayList; -// public class Game -// { -// private boolean paused; -// private Car[] cars; -// private Map map; -// private ArrayList colors; +public class Game +{ + public static record CarInfo(String name, Color color) {} -// private final int time = 1000; + public static class Builder + { + /** + * Liste des voitures ajoutées au jeu. + * On utilise ArrayList pour pouvoir ajouter/supprimer des voitures dynamiquement. + */ + private ArrayList cars = new ArrayList<>(); + private State state = new State(); + private int time = 1000; -// public static class Builder -// { -// private int pnumber = 3; -// private Map map = null; -// private State state = new State(); -// private ArrayList colors = new ArrayList<>(3); + private Map map = null; -// public Builder setPlayers(int pnumber) -// { -// this.pnumber = pnumber; -// return this; -// } + public Builder addCar(CarInfo car) + { + cars.add(car); + return this; + } -// public Builder addColor(Color color) -// { -// colors.add(color); -// return this; -// } - -// public Builder addColor(Color[] c) -// { -// for (Color color : c) -// colors.add(color); -// return this; -// } - -// public Builder remColor(Color color) -// { -// colors.remove(color); -// return this; -// } + public Builder remCar(CarInfo car) + { + cars.remove(car); + return this; + } + + public Builder setMap(Map map) + { + this.map = map; + return this; + } + + public Builder setState(State s) + { + this.state = s; + return this; + } + + public Builder setTime(int time) + { + this.time = time; + return this; + } + + public Game build() + { + if (map == null) + { + System.err.println("Vous devez declarer la map avant de build!"); + System.exit(1); + } + return new Game(map, cars, state, time); + } + } + + private final Map map; + private final State state; + private final int time; + + private final ArrayList cars = new ArrayList<>(); + private final ArrayList obs = new ArrayList<>(); + + private boolean isPaused = false; + + public Game(Map map, ArrayList cars, State state, int time) + { + this.map = map; + this.state = state; + this.time = time; + + init(cars); + } -// public Builder setMap(Map map) -// { -// this.map = map; -// return this; -// } - -// public Builder setState(State s) -// { -// this.state = s; -// return this; -// } - -// public Builder defaultMap() -// { -// Integer[][] map = new Integer[][] -// { -// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 5, 0 }, -// { 0, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 0, 0, -1, 0, 0, -1, 0 }, -// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 }, -// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0 }, -// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, -1, -1, 2, 0, 0, -1, 0 }, -// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 }, -// { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0 }, -// { 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -3, 0 }, -// { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, -// }; - -// this.map = Map.fromInts(map); -// return this; -// } - -// public Game build() -// { -// if (map == null) defaultMap(); -// return new Game(pnumber, map, state, colors); -// } -// } -// public Game(int pnumber, Map map, State state, ArrayList colors) -// { -// this.map = map; -// this.colors = colors; + private Game init(ArrayList carInfos) + { + new Track(map, cars, "Piste Formule 1", 1000, 500, 1, 1); + new Rankboard("Score", cars, 200, 200, 0, 510); + + final int loop = map.getPathSize(); + + int i = 0; + for (CarInfo ci : carInfos) + { + Car car = new Car(ci.name, ci.color, loop, state); + new Dashboard(car, car.toString(), this::togglePause, 300, 200, 1000, 200*i); + cars.add(car); + i++; + } -// cars = new Car[pnumber]; + GameView.update(); + return this; + } + + private boolean isFinish() + { + for (Car car : cars) + { + if (car.getFuel() == 0) + return true; + } + return false; + } + + public Game addObserver(GObserver o) + { + obs.add(o); + return this; + } + + public Game remObserver(GObserver o) + { + obs.remove(o); + return this; + } + + public synchronized boolean togglePause() + { + if (isPaused) notifyAll(); + isPaused = !isPaused; + GameView.update(); + return isPaused; + } + + private synchronized void step() throws InterruptedException + { + for (int i = 0; i < cars.size(); i++) + { + // pause du jeu, while si on notifyall sans faire exprès un autre bout de code + while (isPaused) + wait(); + + cars.get(i).run(); + + for (GObserver o : obs) + { + if (!o.apply()) + { + System.err.println("Une erreur s'est produite pendant le jeu."); + System.exit(1); + } + } + } + } + + public void run() + { + while (!isFinish()) + { + try + { + step(); + GameView.update(); + Thread.sleep(time); + } + catch (InterruptedException e) + { e.printStackTrace(); } + } + } +} -// init(pnumber, state); -// } // private Game init(int pnumber, State state) // { @@ -119,13 +193,6 @@ // return false; // } -// public synchronized boolean togglePause() -// { -// if (paused) notifyAll(); -// paused = !paused; -// GameView.update(); -// return paused; -// } // private void step() throws InterruptedException // { diff --git a/src/Main.java b/src/Main.java index d29e1f5..0a73849 100644 --- a/src/Main.java +++ b/src/Main.java @@ -24,19 +24,28 @@ public class Main { Map m = Map.fromInts(map); - // game.run(); + // // game.run(); - // Dashboard d = new Dashboard(new Car(10, new State()), "Voiture 1", null, Color.BLUE, 500, 500, 10, 10); - Car[] cars = new Car[] { new Car(m.getPathSize(), new State(), Color.PINK)}; - System.out.println(cars[0].getLoop()); - Track t = new Track(m, cars, null, 1080, 512, 0, 0); - Dashboard d = new Dashboard(cars[0], null, () -> true, 300, 300, 0, 0); - while(cars[0].getRound() < 1) - { + // // Dashboard d = new Dashboard(new Car(10, new State()), "Voiture 1", null, Color.BLUE, 500, 500, 10, 10); + // Car[] cars = new Car[] { new Car("Voiture 1", Color.PINK, m.getPathSize(), new State())}; + // System.out.println(cars[0].getLoop()); + // Track t = new Track(m, cars, null, 1080, 512, 0, 0); + // Dashboard d = new Dashboard(cars[0], null, () -> true, 300, 300, 0, 0); + // Rankboard r = new Rankboard(null, 100, 100, 0, 0, cars); + // while(cars[0].getRound() < 1) + // { - cars[0].run(); - GameView.update(); - Thread.sleep(500); - } + // cars[0].run(); + // GameView.update(); + // Thread.sleep(500); + // } + + Game game = new Game.Builder() + .addCar(new Game.CarInfo("Voiture 1", Color.BLUE)) + .addCar(new Game.CarInfo("Voiture 2", Color.PINK)) + .addCar(new Game.CarInfo("Voiture 3", Color.RED)) + .setMap(m) + .build(); + game.run(); } } \ No newline at end of file diff --git a/src/Rankboard.java b/src/Rankboard.java index 4de273f..0494a12 100644 --- a/src/Rankboard.java +++ b/src/Rankboard.java @@ -1,14 +1,14 @@ import java.awt.BorderLayout; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Comparator; import javax.swing.JLabel; public class Rankboard extends GameView { - Car[] cars; + ArrayList cars; private final JLabel label; - public Rankboard(String title, int width, int height, int x, int y, Car[] cars) + public Rankboard(String title, ArrayList cars, int width, int height, int x, int y) { super(title, width, height, x, y); this.cars = cars; @@ -18,9 +18,9 @@ public class Rankboard extends GameView private void updateRankText() { - Arrays.sort(cars, Comparator.comparingInt(Car::getScore).reversed()); + cars.sort(Comparator.comparingInt(Car::getScore).reversed()); StringBuilder s = new StringBuilder(); - s.append(""); + s.append("
Score
"); for (Car c : cars) { s.append(""); diff --git a/src/Track.java b/src/Track.java index a33f1c5..3b2a893 100644 --- a/src/Track.java +++ b/src/Track.java @@ -3,14 +3,15 @@ import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; +import java.util.ArrayList; public class Track extends GameView { private Map map; - private Car[] cars; + private ArrayList cars; private final int scale = 75; - protected Track(Map map, Car[] cars, String title, int width, int height, int x, int y) { + protected Track(Map map, ArrayList cars, String title, int width, int height, int x, int y) { super(title, width, height, x, y); this.map = map; this.cars = cars; @@ -66,13 +67,16 @@ public class Track extends GameView { private void drawCars(Graphics g, int[] cellCoord) { + int size = 1; for (Car c : cars) { // index de pathMap int i = c.getPos(); Point p = this.map.getPath(i); int[] ncoord = new int[] {p.x, p.y}; - drawInnerBlock(g, ncoord, cellCoord, c.getColor()); + drawInnerBlock(g, ncoord, cellCoord, c.getColor(), scale - size); + // ne jamais avoir la meme taille pour les voitures + size += 3; } } @@ -112,7 +116,7 @@ public class Track extends GameView { g.drawString(s, cx, cy); } - private void drawInnerBlock(Graphics g, int[] coord, int[] cellCoord, Color c) + private void drawInnerBlock(Graphics g, int[] coord, int[] cellCoord, Color c, int scale) { // scale de la taille du bloc int w = cellCoord[0] * scale / 100; @@ -140,7 +144,7 @@ public class Track extends GameView { g.setColor(getCellColor(Circuit.Cell.ROAD)); g.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight); - drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType())); + drawInnerBlock(g, coord, cellCoord, getCellColor(cell.getType()), scale); break; default: g.setColor(getCellColor(cell.getType()));
" + c + ": " + c.getScore() + "%