mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-31 04:11:33 +00:00
fix: ajout multiple fonction Map / Game / Car
This commit is contained in:
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(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user