From 9cfa56be8d85120d31eefc6c49eded98373d3fa7 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sat, 27 Nov 2021 17:47:05 +0100 Subject: [PATCH] implement Game#play --- .../src/main/java/fr/lnl/game/client/App.java | 20 +++---- .../game/client/listener/ButtonListener.java | 4 +- .../game/client/view/ClientEventHandler.java | 2 +- .../java/fr/lnl/game/server/games/Game.java | 55 +++++++++++++++++-- .../game/server/games/InterfaceAction.java | 7 +++ .../listener/AbstractModelListening.java | 4 +- .../lnl/game/server/listener/AwakeGame.java | 17 ++++++ .../game/server}/listener/ModelListener.java | 2 +- server/src/main/java/module-info.java | 1 + 9 files changed, 90 insertions(+), 22 deletions(-) create mode 100644 server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java rename {client/src/main/java/fr/lnl/game/client => server/src/main/java/fr/lnl/game/server}/listener/AbstractModelListening.java (82%) create mode 100644 server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java rename {client/src/main/java/fr/lnl/game/client => server/src/main/java/fr/lnl/game/server}/listener/ModelListener.java (72%) diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index bb9e049..598f4ef 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -35,10 +35,13 @@ public class App extends Application { } } - public static Game startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, + public static void startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); - return new Game(new Grid(12, 12, players), players); + for (Player player : game.getPlayers()) { + playerList.put(player, new ClientPlayer(player, new Terminal(game, player))); + } + game = new Game(new Grid(12, 12, players), players); } public static void updateView() { @@ -48,28 +51,23 @@ public class App extends Application { @Override public void start(Stage stage) { try { - game = startGame(); + startGame(); } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { throw new CrashException(e.getMessage(), e); } - for (Player player : game.getPlayers()) { - playerList.put(player, new ClientPlayer(player, new Window(stage, game, player))); - } + new Thread(() -> game.play()); updateView(); } public static void launchTerminal() { try { - game = startGame(); + startGame(); } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { throw new CrashException(e.getMessage(), e); } - - for (Player player : game.getPlayers()) { - playerList.put(player, new ClientPlayer(player, new Terminal(game, player))); - } + new Thread(() -> game.play()); updateView(); } diff --git a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java index 2986408..cbd7d47 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java +++ b/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java @@ -1,8 +1,10 @@ package fr.lnl.game.client.listener; import fr.lnl.game.client.App; +import fr.lnl.game.server.listener.AbstractModelListening; +import fr.lnl.game.server.listener.ModelListener; -public class ButtonListener extends AbstractModelListening{ +public class ButtonListener extends AbstractModelListening { @Override diff --git a/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java b/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java index 4cd204a..f587948 100644 --- a/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java +++ b/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java @@ -1,6 +1,6 @@ package fr.lnl.game.client.view; -import fr.lnl.game.client.listener.ModelListener; +import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.client.listener.ButtonListener; import javafx.event.ActionEvent; import javafx.event.EventHandler; diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index e73283b..8fae549 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -3,16 +3,22 @@ package fr.lnl.game.server.games; import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.listener.AwakeGame; +import fr.lnl.game.server.listener.ModelListener; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; public class Game { - Grid grid; - Player currentPlayer; - List players; + private final Grid grid; + private final List players; + private Player currentPlayer; + private InterfaceAction interfaceAction; + private Action selectedAction = null; + private final Object lock = new Object(); public Game(Grid grid, List players) throws IllegalArgumentException { if(players.size() < 2) @@ -22,8 +28,37 @@ public class Game { this.grid = grid; } - public void play() { + /** + * + * @return game winner + */ + public Player play() { + while(!isOver()) { + ModelListener awakeEvent = new AwakeGame(this); + generateAndGetPlayerActions(currentPlayer); + interfaceAction = InterfaceAction.SELECT_ACTION; + waitForInterfaceEvent(); + selectedAction.doAction(); + nextCurrentPlayer(); + } + return getWinner(); + } + private void waitForInterfaceEvent() { + synchronized (lock){ + try { + wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } + + public void resumeThread() { + synchronized (lock) { + lock.notifyAll(); + } } protected List generateAndGetPlayerActions(Player player) { @@ -46,13 +81,17 @@ public class Game { return actions; } + public Stream getPlayersAlive() { + return players.parallelStream().filter(Player::isAlive); + } + public boolean isOver() { - return players.parallelStream().filter(player -> !player.isAlive()).count() == 1; + return getPlayersAlive().count() <= 1; } public Player getWinner() { // On part du principe que isOver est forcément appelé avant d'appeler getWinner - return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElse(null); + return getPlayersAlive().findFirst().orElse(null); } public Player getCurrentPlayer() { @@ -86,4 +125,8 @@ public class Game { public List getPlayers() { return players; } + + public InterfaceAction getInterfaceAction() { + return interfaceAction; + } } \ No newline at end of file diff --git a/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java b/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java new file mode 100644 index 0000000..01591f1 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java @@ -0,0 +1,7 @@ +package fr.lnl.game.server.games; + +public enum InterfaceAction { + + SELECT_ACTION + +} diff --git a/client/src/main/java/fr/lnl/game/client/listener/AbstractModelListening.java b/server/src/main/java/fr/lnl/game/server/listener/AbstractModelListening.java similarity index 82% rename from client/src/main/java/fr/lnl/game/client/listener/AbstractModelListening.java rename to server/src/main/java/fr/lnl/game/server/listener/AbstractModelListening.java index d8efd38..3e71f58 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/AbstractModelListening.java +++ b/server/src/main/java/fr/lnl/game/server/listener/AbstractModelListening.java @@ -1,9 +1,9 @@ -package fr.lnl.game.client.listener; +package fr.lnl.game.server.listener; import java.util.List; public abstract class AbstractModelListening implements ModelListener { - List listeners; + protected List listeners; public void addListener(ModelListener e) { this.listeners.add(e); diff --git a/server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java b/server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java new file mode 100644 index 0000000..c5bc122 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/listener/AwakeGame.java @@ -0,0 +1,17 @@ +package fr.lnl.game.server.listener; + +import fr.lnl.game.server.games.Game; + +public class AwakeGame extends AbstractModelListening { + + private final Game game; + + public AwakeGame(Game game) { + this.game = game; + } + + @Override + public void updateModel(Object obj) { + game.resumeThread(); + } +} diff --git a/client/src/main/java/fr/lnl/game/client/listener/ModelListener.java b/server/src/main/java/fr/lnl/game/server/listener/ModelListener.java similarity index 72% rename from client/src/main/java/fr/lnl/game/client/listener/ModelListener.java rename to server/src/main/java/fr/lnl/game/server/listener/ModelListener.java index 46d4868..437bb2d 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ModelListener.java +++ b/server/src/main/java/fr/lnl/game/server/listener/ModelListener.java @@ -1,4 +1,4 @@ -package fr.lnl.game.client.listener; +package fr.lnl.game.server.listener; public interface ModelListener { //à peut-être revoir diff --git a/server/src/main/java/module-info.java b/server/src/main/java/module-info.java index 4e804c4..f52db89 100644 --- a/server/src/main/java/module-info.java +++ b/server/src/main/java/module-info.java @@ -6,4 +6,5 @@ module server { exports fr.lnl.game.server.games.weapon; exports fr.lnl.game.server.games.action; exports fr.lnl.game.server.utils; + exports fr.lnl.game.server.listener; } \ No newline at end of file