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