mirror of
https://github.com/guezoloic/L3-racing-game.git
synced 2026-03-28 19:13:41 +00:00
feat: ajout impl Car et rework State et Game
This commit is contained in:
164
src/Game.java
164
src/Game.java
@@ -1,73 +1,79 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
public class Game
|
||||
{
|
||||
@FunctionalInterface
|
||||
private static interface GameObserver
|
||||
private boolean paused;
|
||||
private Car[] cars;
|
||||
private Map map;
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @return true si la fonction s'est bien passé sinon false (le programme va se stopper)
|
||||
*/
|
||||
public boolean apply();
|
||||
}
|
||||
private int pnumber = 3;
|
||||
private Map map = null;
|
||||
private State state = new State();
|
||||
|
||||
private static Game game;
|
||||
|
||||
private Car[] cars;
|
||||
private Map map;
|
||||
private ArrayList<GameObserver> obs = new ArrayList<>();
|
||||
|
||||
private Random random = new Random();
|
||||
private boolean paused = false;
|
||||
|
||||
public static Game getInstance()
|
||||
{
|
||||
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 < cars.length; i++)
|
||||
public Builder setPlayers(int pnumber)
|
||||
{
|
||||
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() + " random: " + rand);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
this.pnumber = pnumber;
|
||||
return this;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addObserver(GameObserver game)
|
||||
|
||||
private ArrayList<GObserver> obs;
|
||||
|
||||
public Game(int pnumber, Map map, State state)
|
||||
{
|
||||
obs.add(game);
|
||||
}
|
||||
int loop = map.getPathSize();
|
||||
this.map = map;
|
||||
|
||||
public void removeObserver(GameObserver game)
|
||||
{
|
||||
obs.remove(game);
|
||||
cars = new Car[pnumber];
|
||||
obs = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < pnumber; i++)
|
||||
{
|
||||
Car car = new Car(loop, state);
|
||||
cars[i] = car;
|
||||
// Observer pour avancer
|
||||
obs.add(car);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isFinish()
|
||||
@@ -80,9 +86,16 @@ public class Game
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized boolean togglePause()
|
||||
{
|
||||
if (paused) notifyAll();
|
||||
paused = !paused;
|
||||
return paused;
|
||||
}
|
||||
|
||||
private void step() throws InterruptedException
|
||||
{
|
||||
for (GameObserver o : obs)
|
||||
for (GObserver o : obs)
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
@@ -91,37 +104,18 @@ public class Game
|
||||
{
|
||||
wait();
|
||||
}
|
||||
|
||||
boolean isSuccess = o.apply();
|
||||
if (!isSuccess)
|
||||
{
|
||||
System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
boolean isSuccess = o.apply();
|
||||
if (!isSuccess)
|
||||
{
|
||||
System.err.println("Une erreur s'est produite pendant le jeu.");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public synchronized boolean togglePause()
|
||||
{
|
||||
if (paused)
|
||||
{
|
||||
notifyAll();
|
||||
paused = false;
|
||||
}
|
||||
else paused = true;
|
||||
return paused;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
if (this.cars == null || this.map == null)
|
||||
{
|
||||
System.out.println("Lancement du init par défaut.");
|
||||
GFactory.defaultInit();
|
||||
}
|
||||
|
||||
while (!isFinish())
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user