From 09302dd880dae1f420b0f4b256689671690675e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sat, 8 Nov 2025 17:27:35 +0100 Subject: [PATCH] rework: ajout plusieurs classes et modification logique --- src/Car.java | 7 +- src/Dashboard.java | 70 ++++++++++++ src/GObserver.java | 13 --- src/Game.java | 265 +++++++++++++++++++++++++-------------------- src/GameView.java | 30 ++++- src/Main.java | 4 + src/Rankboard.java | 3 + src/Track.java | 3 + 8 files changed, 256 insertions(+), 139 deletions(-) create mode 100644 src/Dashboard.java delete mode 100644 src/GObserver.java create mode 100644 src/Rankboard.java create mode 100644 src/Track.java diff --git a/src/Car.java b/src/Car.java index 9e4ea1c..70b7b97 100644 --- a/src/Car.java +++ b/src/Car.java @@ -5,7 +5,7 @@ import java.util.Random; * Chaque appel à {@link #makeMove()} avance la voiture d'une position. * Quand la position atteint la fin de la boucle, un nouveau tour est compté. */ -public class Car implements GObserver +public class Car { /** Ajout de la classe Random (Evite de le recreer a chaque fois) */ private Random rand = new Random(); @@ -97,8 +97,7 @@ public class Car implements GObserver return this; } - @Override - public boolean apply() + public void run() { if (this.fuel > 0) { @@ -106,8 +105,6 @@ public class Car implements GObserver int random = rand.nextInt(interval[0], interval[1]); makeMove(random).consumeFuel(); } - - return true; } public int getPos() diff --git a/src/Dashboard.java b/src/Dashboard.java new file mode 100644 index 0000000..52dbb73 --- /dev/null +++ b/src/Dashboard.java @@ -0,0 +1,70 @@ +import java.awt.Color; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.function.Function; +import java.util.function.Supplier; + +import javax.swing.JButton; +import javax.swing.JLabel; + + +/** + * bouton en bas, couleur choisi de la fenetre + * affiche: carburant restant et tour de piste + */ +public class Dashboard extends GameView +{ + private final JLabel label; + private final JButton button; + private final Car car; + + public Dashboard(Car car, String title, Supplier fn, Color bg, int width, int height, int x, int y) + { + super("Dashboard: " + title, width, height, x, y); + + this.car = car; + this.label = new JLabel(); + this.button = new JButton(); + + // ajout background + super.window.setBackground(bg); + + init(fn); + + super.window.add(label); + super.window.add(button); + } + + // fonction uniquement pour l'affichage dynamique + public void updateButtonText(boolean isPaused) + { + button.setText(isPaused ? "En Pause" : "En Cours"); + } + + private void init(Supplier fn) + { + // de base, le jeu est en cours + updateButtonText(false); + + // classe pour le bouton + MouseAdapter ma = new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) + { + fn.get(); + } + }; + + this.button.addMouseListener(ma); + } + + @Override + public void run() + { + label.setText( + "
Carburant Restant: " + car.getFuel() + + "
Nombre de Tour: " + car.getRound() + + "
" + ); + } +} diff --git a/src/GObserver.java b/src/GObserver.java deleted file mode 100644 index cb2e4f6..0000000 --- a/src/GObserver.java +++ /dev/null @@ -1,13 +0,0 @@ -@FunctionalInterface -/** - * L'interface utilisée pour Game. - */ -public interface GObserver -{ - /** - * - * @return true si la fonction s'est bien passé sinon false (le programme va se - * stopper) - */ - public boolean apply(); -} \ No newline at end of file diff --git a/src/Game.java b/src/Game.java index b37b047..31ff090 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,132 +1,165 @@ -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 obs; - private final int time = 1000; +// public class Game +// { +// private boolean paused; +// private Car[] cars; +// private Map map; +// private ArrayList colors; - public static class Builder - { - private int pnumber = 3; - private Map map = null; - private State state = new State(); +// private final int time = 1000; - public Builder setPlayers(int pnumber) - { - this.pnumber = pnumber; - return this; - } +// public static class Builder +// { +// private int pnumber = 3; +// private Map map = null; +// private State state = new State(); +// private ArrayList colors = new ArrayList<>(3); + +// public Builder setPlayers(int pnumber) +// { +// this.pnumber = pnumber; +// return this; +// } + +// public Builder addColor(Color color) +// { +// colors.add(color); +// return this; +// } - public Builder setMap(Map map) - { - this.map = map; - return this; - } +// public Builder addColor(Color[] c) +// { +// for (Color color : c) +// colors.add(color); +// return this; +// } - public Builder setState(State s) - { - this.state = s; - return this; - } +// public Builder remColor(Color color) +// { +// colors.remove(color); +// return this; +// } + +// public Builder setMap(Map map) +// { +// this.map = map; +// 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 }, - }; +// 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; - } +// this.map = Map.fromInts(map); +// return this; +// } - public Game build() - { - if (map == null) defaultMap(); - return new Game(pnumber, map, state); - } - } +// public Game build() +// { +// if (map == null) defaultMap(); +// return new Game(pnumber, map, state, colors); +// } +// } - public Game(int pnumber, Map map, State state) - { - int loop = map.getPathSize(); - this.map = map; +// public Game(int pnumber, Map map, State state, ArrayList colors) +// { +// this.map = map; +// this.colors = colors; - cars = new Car[pnumber]; - obs = new ArrayList<>(); +// cars = new Car[pnumber]; - for (int i = 0; i < pnumber; i++) - { - Car car = new Car(loop, state); - cars[i] = car; +// init(pnumber, state); +// } - // Observer pour avancer - obs.add(car); - } - } +// private Game init(int pnumber, State state) +// { +// // ajout de voiture +// int loop = map.getPathSize(); - private boolean isFinish() - { - for (Car car : cars) - { - if (car.getFuel() == 0) - return true; - } - return false; - } +// for (int i = 0; i < pnumber; i++) +// { +// cars[i] = new Car(loop, state); - public synchronized boolean togglePause() - { - if (paused) notifyAll(); - paused = !paused; - return paused; - } +// Color color = (i < colors.size()) ? colors.get(i) : Color.BLUE; +// // new Dashboard(cars[i], "Voiture " + (i+1), this::togglePause, color, 300, 300, 300, 300*i); +// } + +// // new Track(); +// // new Rankboard(); +// return this; +// } - private void step() throws InterruptedException - { - for (GObserver o : obs) - { - synchronized (this) - { - // pause du jeu, while si on notifyall sans faire exprès un autre bout de code - while (paused) - { - wait(); - } - } - boolean isSuccess = o.apply(); - if (!isSuccess) - { - System.err.println("Une erreur s'est produite pendant le jeu."); - System.exit(1); - } - } - } +// private boolean isFinish() +// { +// for (Car car : cars) +// { +// if (car.getFuel() == 0) +// return true; +// } +// return false; +// } - public void run() - { - while (!isFinish()) - { - try - { - step(); - Thread.sleep(time); - } - catch (InterruptedException e) - { e.printStackTrace(); } - } - } -} +// public synchronized boolean togglePause() +// { +// if (paused) notifyAll(); +// paused = !paused; +// GameView.update(); +// return paused; +// } + +// private void step() throws InterruptedException +// { +// for (Car c : cars) +// { +// synchronized (this) +// { +// // pause du jeu, while si on notifyall sans faire exprès un autre bout de code +// while (paused) +// { +// wait(); +// } +// } +// c.run(); + +// if (!isSuccess) +// { +// System.err.println("Une erreur s'est produite pendant le jeu."); +// System.exit(1); +// } +// } +// } + +// public void run() +// { +// while (!isFinish()) +// { +// try +// { +// step(); +// Thread.sleep(time); +// } +// catch (InterruptedException e) +// { e.printStackTrace(); } +// } +// } +// } diff --git a/src/GameView.java b/src/GameView.java index 806f8f2..335e8e3 100644 --- a/src/GameView.java +++ b/src/GameView.java @@ -1,11 +1,14 @@ import java.awt.Container; +import java.util.ArrayList; import javax.swing.JFrame; -public class GameView extends JFrame implements GObserver { +public abstract class GameView extends JFrame +{ + private static ArrayList vobs = new ArrayList<>(); protected Container window; - public GameView(String title, int width, int height, int x, int y) + protected GameView(String title, int width, int height, int x, int y) { // la fenetre JFrame frame = new JFrame(title); @@ -23,11 +26,28 @@ public class GameView extends JFrame implements GObserver { // acceder a l'interieur de la fenetre window = frame.getContentPane(); + register(); } - @Override - public boolean apply() + protected GameView register() { - return true; + vobs.add(this); + return this; + } + + protected GameView unregister() + { + vobs.remove(this); + return this; + } + + public static void update() + { + for (GameView o : vobs) + { + o.run(); + } } + + protected abstract void run(); } \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 597e3c0..535a849 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import java.awt.Color; + public class Main { public static void main(String[] args) throws InterruptedException { @@ -7,5 +9,7 @@ public class Main { .build(); game.run(); + + // Dashboard d = new Dashboard(new Car(10, new State()), "Voiture 1", Color.BLUE, 500, 500, 10, 10); } } \ No newline at end of file diff --git a/src/Rankboard.java b/src/Rankboard.java new file mode 100644 index 0000000..8c55ab5 --- /dev/null +++ b/src/Rankboard.java @@ -0,0 +1,3 @@ +public class Rankboard { + +} diff --git a/src/Track.java b/src/Track.java new file mode 100644 index 0000000..f193251 --- /dev/null +++ b/src/Track.java @@ -0,0 +1,3 @@ +public class Track { + +}