mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
feat: ajout fonction observer et while dans paused
This commit is contained in:
@@ -22,7 +22,7 @@ public class Game
|
||||
private Random random = new Random();
|
||||
private boolean paused = false;
|
||||
|
||||
public static Game create()
|
||||
public static Game getInstance()
|
||||
{
|
||||
if (game == null) game = new Game();
|
||||
return game;
|
||||
@@ -41,6 +41,7 @@ public class Game
|
||||
final Car CAR = new Car(loop);
|
||||
final int I = i;
|
||||
cars[i] = CAR;
|
||||
|
||||
// Observer pour avancer
|
||||
obs.add(() -> {
|
||||
if (CAR.getFuel() > 0)
|
||||
@@ -49,7 +50,7 @@ public class Game
|
||||
int rand = random.nextInt(interval[0], interval[1]);
|
||||
|
||||
CAR.makeMove(rand).consumeFuel();
|
||||
System.out.println("car " + I + " score: " + CAR.getScore());
|
||||
System.out.println("car " + I + " score: " + CAR.getScore() + " random: " + rand);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -59,21 +60,14 @@ public class Game
|
||||
return this;
|
||||
}
|
||||
|
||||
public Game defaultInit()
|
||||
public void addObserver(GameObserver game)
|
||||
{
|
||||
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));
|
||||
obs.add(game);
|
||||
}
|
||||
|
||||
public void removeObserver(GameObserver game)
|
||||
{
|
||||
obs.remove(game);
|
||||
}
|
||||
|
||||
private boolean isFinish()
|
||||
@@ -92,8 +86,11 @@ public class Game
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
// pause du jeu
|
||||
if (paused) wait();
|
||||
// pause du jeu, while si on notifyall sans faire exprès un autre bout de code
|
||||
while (paused)
|
||||
{
|
||||
wait();
|
||||
}
|
||||
|
||||
boolean isSuccess = o.apply();
|
||||
if (!isSuccess)
|
||||
@@ -120,7 +117,10 @@ public class Game
|
||||
public void run()
|
||||
{
|
||||
if (this.cars == null || this.map == null)
|
||||
defaultInit();
|
||||
{
|
||||
System.out.println("Lancement du init par défaut.");
|
||||
GFactory.defaultInit();
|
||||
}
|
||||
|
||||
while (!isFinish())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user