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

@@ -54,6 +54,7 @@ public class Dashboard extends GameView
{
isPaused = fn.get();
updateButtonText();
GameView.update();
}
};
@@ -68,5 +69,7 @@ public class Dashboard extends GameView
+ "</td></tr><tr><td>Nombre de Tour: " + car.getRound()
+ "</td></tr></table></html>"
);
updateButtonText();
}
}

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
// {

View File

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

View File

@@ -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<Car> cars;
private final JLabel label;
public Rankboard(String title, int width, int height, int x, int y, Car[] cars)
public Rankboard(String title, ArrayList<Car> 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("<html><table><tr><td>Score</td></tr>");
s.append("<html><table>");
for (Car c : cars)
{
s.append("<tr><td>" + c + ": " + c.getScore() + "%</td></tr>");

View File

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