diff --git a/src/Game.java b/src/Game.java index 4074796..07de966 100644 --- a/src/Game.java +++ b/src/Game.java @@ -20,7 +20,7 @@ public class Game private ArrayList obs = new ArrayList<>(); private Random random = new Random(); - private boolean pause = false; + private boolean paused = false; public static Game create() { @@ -86,21 +86,37 @@ public class Game return false; } - private synchronized void step() throws InterruptedException + private void step() throws InterruptedException { - // if (pause) wait(); - for (GameObserver o : obs) { - if (!o.apply()) + synchronized (this) { - System.err.println("Une erreur s'est produite pendant le jeu."); - System.exit(1); + // pause du jeu + if (paused) wait(); + + 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) @@ -108,7 +124,6 @@ public class Game while (!isFinish()) { - // notifyAll(); try { step(); diff --git a/src/Main.java b/src/Main.java index 390fe52..c251c8a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,11 +1,27 @@ public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { Map m = Map.fromChars(new Character[][] { {'3', '#', '2'}, {'#', ' ', 'S'}, {'9', '#', 'F'}, }); + Thread t = new Thread(() -> { + while (true) + { + try + { + Thread.sleep(5000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + System.out.println(Game.create().togglePause() ? "stop" : "fini"); + } + }); + + t.start(); Game.create() .init(3, m) .run();