mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
fix: ajout multiple fonction Map / Game / Car
This commit is contained in:
@@ -35,12 +35,14 @@ public class Car {
|
||||
*
|
||||
* @return cette même instance (pour chaînage fluide)
|
||||
*/
|
||||
public Car makeMove()
|
||||
public Car makeMove(int move)
|
||||
{
|
||||
pos++;
|
||||
pos += move;
|
||||
if (pos == loop)
|
||||
{
|
||||
round++;
|
||||
pos %= (loop + 1); // Repart de 0 après avoir dépassé loop
|
||||
pos = 0;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
109
src/Game.java
109
src/Game.java
@@ -1,42 +1,121 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class Game
|
||||
{
|
||||
private Car[] car;
|
||||
private Map map;
|
||||
|
||||
public Game(int playerNumber, Map map)
|
||||
@FunctionalInterface
|
||||
private static interface GameObserver
|
||||
{
|
||||
this.car = new Car[playerNumber];
|
||||
this.map = map;
|
||||
|
||||
init().run();
|
||||
/**
|
||||
*
|
||||
* @return true si la fonction s'est bien passé sinon false (le programme va se stopper)
|
||||
*/
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
for (int i = 0; i < car.length; i++)
|
||||
for (Car car : cars)
|
||||
{
|
||||
if (car[i].getFuel() == 0)
|
||||
if (car.getFuel() == 0)
|
||||
return true;
|
||||
}
|
||||
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()
|
||||
{
|
||||
while (isFinish())
|
||||
{
|
||||
if (this.cars == null || this.map == null)
|
||||
defaultInit();
|
||||
|
||||
while (!isFinish())
|
||||
{
|
||||
// notifyAll();
|
||||
try
|
||||
{
|
||||
step();
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{ e.printStackTrace(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ public class Main {
|
||||
{'9', '#', 'F'},
|
||||
});
|
||||
|
||||
System.out.println(m.getPathSize());
|
||||
Car car = new Car(m.getPathSize());
|
||||
Game.create()
|
||||
.init(3, m)
|
||||
.run();
|
||||
}
|
||||
}
|
||||
12
src/Map.java
12
src/Map.java
@@ -53,9 +53,9 @@ public class Map
|
||||
* Crée une map à partir d'un tableau d'entiers.
|
||||
* <ul>
|
||||
* <li>0 -> EMPTY</li>
|
||||
* <li>1 -> ROAD</li>
|
||||
* <li>2 -> START</li>
|
||||
* <li>3 -> FINISH</li>
|
||||
* <li>-1 -> ROAD</li>
|
||||
* <li>-2 -> START</li>
|
||||
* <li>-3 -> FINISH</li>
|
||||
* <li>autres -> YROAD avec la valeur associée</li>
|
||||
* </ul>
|
||||
*
|
||||
@@ -66,9 +66,9 @@ public class Map
|
||||
{
|
||||
return create((i) -> switch (i) {
|
||||
case 0 -> new Circuit(CircuitCell.EMPTY);
|
||||
case 1 -> new Circuit(CircuitCell.ROAD);
|
||||
case 2 -> new Circuit(CircuitCell.START);
|
||||
case 3 -> new Circuit(CircuitCell.FINISH);
|
||||
case -1 -> new Circuit(CircuitCell.ROAD);
|
||||
case -2 -> new Circuit(CircuitCell.START);
|
||||
case -3 -> new Circuit(CircuitCell.FINISH);
|
||||
default -> new Circuit(CircuitCell.YROAD, i);
|
||||
}, map);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user