rework: ajout plusieurs classes et modification logique

This commit is contained in:
2025-11-08 17:27:35 +01:00
parent e32805bcd6
commit 09302dd880
8 changed files with 256 additions and 139 deletions

View File

@@ -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()

70
src/Dashboard.java Normal file
View File

@@ -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<Boolean> 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<Boolean> 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(
"<html><table><tr><td>Carburant Restant: " + car.getFuel()
+ "</td></tr><tr><td>Nombre de Tour: " + car.getRound()
+ "</td></tr></table></html>"
);
}
}

View File

@@ -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();
}

View File

@@ -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<GObserver> obs;
private final int time = 1000;
// public class Game
// {
// private boolean paused;
// private Car[] cars;
// private Map map;
// private ArrayList<Color> 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<Color> 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<Color> 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(); }
// }
// }
// }

View File

@@ -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<GameView> 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();
}

View File

@@ -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);
}
}

3
src/Rankboard.java Normal file
View File

@@ -0,0 +1,3 @@
public class Rankboard {
}

3
src/Track.java Normal file
View File

@@ -0,0 +1,3 @@
public class Track {
}