fix: ajout multiple fonction Map / Game / Car

This commit is contained in:
2025-10-30 19:08:56 +01:00
parent 23f56113b7
commit 974fa21c3d
4 changed files with 108 additions and 26 deletions

View File

@@ -35,12 +35,14 @@ public class Car {
* *
* @return cette même instance (pour chaînage fluide) * @return cette même instance (pour chaînage fluide)
*/ */
public Car makeMove() public Car makeMove(int move)
{ {
pos++; pos += move;
if (pos == loop) if (pos == loop)
{
round++; round++;
pos %= (loop + 1); // Repart de 0 après avoir dépassé loop pos = 0;
}
return this; return this;
} }

View File

@@ -1,42 +1,121 @@
import java.util.ArrayList;
import java.util.Random;
public class Game public class Game
{ {
private Car[] car; @FunctionalInterface
private Map map; private static interface GameObserver
public Game(int playerNumber, Map map)
{ {
this.car = new Car[playerNumber]; /**
this.map = map; *
* @return true si la fonction s'est bien passé sinon false (le programme va se stopper)
init().run(); */
public boolean apply();
} }
private Game init() private static Game game;
private Car[] cars;
private Map map;
private ArrayList<GameObserver> obs = new ArrayList<>();
private Random random = new Random();
private boolean pause = false;
public static Game create()
{ {
if (game == null) game = new Game();
return game;
}
private Game() {}
public Game init(int playerNumber, Map map)
{
this.cars = new Car[playerNumber];
this.map = map;
// combien de route avant loop // combien de route avant loop
int loop = this.map.getPathSize(); int loop = this.map.getPathSize();
for (int i = 0; i < car.length; i++) for (int i = 0; i < cars.length; i++)
{ {
car[i] = new Car(loop); final Car CAR = new Car(loop);
final int I = i;
cars[i] = CAR;
// Observer pour avancer
obs.add(() -> {
if (CAR.getFuel() > 0)
{
int[] interval = State.get().getInterval();
int rand = random.nextInt(interval[0], interval[1]);
CAR.makeMove(rand).consumeFuel();
System.out.println("car " + I + " score: " + CAR.getScore());
}
return true;
});
} }
return this; return this;
} }
public Game defaultInit()
{
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 },
};
return init(3, Map.fromInts(map));
}
private boolean isFinish() private boolean isFinish()
{ {
for (int i = 0; i < car.length; i++) for (Car car : cars)
{ {
if (car[i].getFuel() == 0) if (car.getFuel() == 0)
return true; return true;
} }
return false; return false;
} }
private synchronized void step() throws InterruptedException
{
// if (pause) wait();
for (GameObserver o : obs)
{
if (!o.apply())
{
System.err.println("Une erreur s'est produite pendant le jeu.");
System.exit(1);
}
}
}
public void run() public void run()
{ {
while (isFinish()) if (this.cars == null || this.map == null)
{ defaultInit();
while (!isFinish())
{
// notifyAll();
try
{
step();
Thread.sleep(1000);
}
catch (InterruptedException e)
{ e.printStackTrace(); }
} }
} }
} }

View File

@@ -6,7 +6,8 @@ public class Main {
{'9', '#', 'F'}, {'9', '#', 'F'},
}); });
System.out.println(m.getPathSize()); Game.create()
Car car = new Car(m.getPathSize()); .init(3, m)
.run();
} }
} }

View File

@@ -53,9 +53,9 @@ public class Map
* Crée une map à partir d'un tableau d'entiers. * Crée une map à partir d'un tableau d'entiers.
* <ul> * <ul>
* <li>0 -> EMPTY</li> * <li>0 -> EMPTY</li>
* <li>1 -> ROAD</li> * <li>-1 -> ROAD</li>
* <li>2 -> START</li> * <li>-2 -> START</li>
* <li>3 -> FINISH</li> * <li>-3 -> FINISH</li>
* <li>autres -> YROAD avec la valeur associée</li> * <li>autres -> YROAD avec la valeur associée</li>
* </ul> * </ul>
* *
@@ -66,9 +66,9 @@ public class Map
{ {
return create((i) -> switch (i) { return create((i) -> switch (i) {
case 0 -> new Circuit(CircuitCell.EMPTY); case 0 -> new Circuit(CircuitCell.EMPTY);
case 1 -> new Circuit(CircuitCell.ROAD); case -1 -> new Circuit(CircuitCell.ROAD);
case 2 -> new Circuit(CircuitCell.START); case -2 -> new Circuit(CircuitCell.START);
case 3 -> new Circuit(CircuitCell.FINISH); case -3 -> new Circuit(CircuitCell.FINISH);
default -> new Circuit(CircuitCell.YROAD, i); default -> new Circuit(CircuitCell.YROAD, i);
}, map); }, map);
} }