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. * Chaque appel à {@link #makeMove()} avance la voiture d'une position.
* Quand la position atteint la fin de la boucle, un nouveau tour est compté. * 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) */ /** Ajout de la classe Random (Evite de le recreer a chaque fois) */
private Random rand = new Random(); private Random rand = new Random();
@@ -97,8 +97,7 @@ public class Car implements GObserver
return this; return this;
} }
@Override public void run()
public boolean apply()
{ {
if (this.fuel > 0) if (this.fuel > 0)
{ {
@@ -106,8 +105,6 @@ public class Car implements GObserver
int random = rand.nextInt(interval[0], interval[1]); int random = rand.nextInt(interval[0], interval[1]);
makeMove(random).consumeFuel(); makeMove(random).consumeFuel();
} }
return true;
} }
public int getPos() 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 // public class Game
{ // {
private boolean paused; // private boolean paused;
private Car[] cars; // private Car[] cars;
private Map map; // private Map map;
// private ArrayList<Color> colors;
private ArrayList<GObserver> obs; // private final int time = 1000;
private final int time = 1000;
public static class Builder // public static class Builder
{ // {
private int pnumber = 3; // private int pnumber = 3;
private Map map = null; // private Map map = null;
private State state = new State(); // private State state = new State();
// private ArrayList<Color> colors = new ArrayList<>(3);
public Builder setPlayers(int pnumber) // public Builder setPlayers(int pnumber)
{ // {
this.pnumber = pnumber; // this.pnumber = pnumber;
return this; // return this;
} // }
public Builder setMap(Map map) // public Builder addColor(Color color)
{ // {
this.map = map; // colors.add(color);
return this; // return this;
} // }
public Builder setState(State s) // public Builder addColor(Color[] c)
{ // {
this.state = s; // for (Color color : c)
return this; // colors.add(color);
} // return this;
// }
public Builder defaultMap() // public Builder remColor(Color color)
{ // {
Integer[][] map = new Integer[][] // colors.remove(color);
{ // return this;
{ 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); // public Builder setMap(Map map)
return this; // {
} // this.map = map;
// return this;
// }
public Game build() // public Builder setState(State s)
{ // {
if (map == null) defaultMap(); // this.state = s;
return new Game(pnumber, map, state); // return this;
} // }
}
public Game(int pnumber, Map map, State state) // public Builder defaultMap()
{ // {
int loop = map.getPathSize(); // Integer[][] map = new Integer[][]
this.map = map; // {
// { 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 },
// };
cars = new Car[pnumber]; // this.map = Map.fromInts(map);
obs = new ArrayList<>(); // return this;
// }
for (int i = 0; i < pnumber; i++) // public Game build()
{ // {
Car car = new Car(loop, state); // if (map == null) defaultMap();
cars[i] = car; // return new Game(pnumber, map, state, colors);
// }
// }
// Observer pour avancer // public Game(int pnumber, Map map, State state, ArrayList<Color> colors)
obs.add(car); // {
} // this.map = map;
} // this.colors = colors;
private boolean isFinish() // cars = new Car[pnumber];
{
for (Car car : cars)
{
if (car.getFuel() == 0)
return true;
}
return false;
}
public synchronized boolean togglePause() // init(pnumber, state);
{ // }
if (paused) notifyAll();
paused = !paused;
return paused;
}
private void step() throws InterruptedException // private Game init(int pnumber, State state)
{ // {
for (GObserver o : obs) // // ajout de voiture
{ // int loop = map.getPathSize();
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);
}
}
}
public void run() // for (int i = 0; i < pnumber; i++)
{ // {
while (!isFinish()) // cars[i] = new Car(loop, state);
{
try // 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);
step(); // }
Thread.sleep(time);
} // // new Track();
catch (InterruptedException e) // // new Rankboard();
{ e.printStackTrace(); } // return this;
} // }
}
} // private boolean isFinish()
// {
// for (Car car : cars)
// {
// if (car.getFuel() == 0)
// return true;
// }
// return false;
// }
// 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.awt.Container;
import java.util.ArrayList;
import javax.swing.JFrame; 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; 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 // la fenetre
JFrame frame = new JFrame(title); JFrame frame = new JFrame(title);
@@ -23,11 +26,28 @@ public class GameView extends JFrame implements GObserver {
// acceder a l'interieur de la fenetre // acceder a l'interieur de la fenetre
window = frame.getContentPane(); window = frame.getContentPane();
register();
} }
@Override protected GameView register()
public boolean apply()
{ {
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 class Main {
public static void main(String[] args) throws InterruptedException public static void main(String[] args) throws InterruptedException
{ {
@@ -7,5 +9,7 @@ public class Main {
.build(); .build();
game.run(); 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 {
}