feat: ajout et correction multiple classe

This commit is contained in:
2025-11-09 16:02:43 +01:00
parent 9e54622ae8
commit 0b60550e0b
5 changed files with 195 additions and 112 deletions

View File

@@ -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<Color> 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<CarInfo> 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<Color> 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<Car> cars = new ArrayList<>();
private final ArrayList<GObserver> obs = new ArrayList<>();
private boolean isPaused = false;
public Game(Map map, ArrayList<CarInfo> 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<Color> colors)
// {
// this.map = map;
// this.colors = colors;
private Game init(ArrayList<CarInfo> 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
// {