From 74b84898549614aff1b1cb5ded7d27dc1d8e3327 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 8 Dec 2021 11:32:16 +0100 Subject: [PATCH] fix NullPointerException error when using Terminal view, fix MVC model not respected to display winner --- README.txt | 2 +- .../src/main/java/fr/lnl/game/client/App.java | 7 ++-- .../game/client/listener/ButtonListener.java | 8 ++++- .../client/listener/DisplayWinnerEvent.java | 4 ++- .../fr/lnl/game/client/view/ViewManager.java | 35 ++++++++++--------- .../java/fr/lnl/game/server/games/Game.java | 23 ++++-------- .../game/server/listener/GameFinishEvent.java | 23 ------------ 7 files changed, 40 insertions(+), 62 deletions(-) delete mode 100644 server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java diff --git a/README.txt b/README.txt index 1970c7c..fb4e17e 100644 --- a/README.txt +++ b/README.txt @@ -1,4 +1,4 @@ -Projet de Conception Logiciel concu par : +Projet de Conception Logiciel conçu par : LUCAS Valentin LEGOT Quentin 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 579fac2..9dd2092 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -1,12 +1,10 @@ package fr.lnl.game.client; -import fr.lnl.game.client.listener.DisplayWinnerEvent; import fr.lnl.game.client.view.AbstractView; import fr.lnl.game.client.view.Terminal; import fr.lnl.game.client.view.ViewManager; import fr.lnl.game.client.view.Window; import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.build.GridFactoryBuilder; import fr.lnl.game.server.games.grid.build.LockGridFactoryBuilder; import fr.lnl.game.server.games.player.*; @@ -44,9 +42,8 @@ public class App extends Application { public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); - Grid grid = new Grid(12, 12, players); GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12); - game = new Game(builder, players, new DisplayWinnerEvent()); + game = new Game(builder, players); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } @@ -61,6 +58,7 @@ public class App extends Application { throw new CrashException(e.getMessage(), e); } viewManager = new ViewManager(playerList, game, Window.class); + viewManager.run(); } public static void launchTerminal() { @@ -71,6 +69,7 @@ public class App extends Application { throw new CrashException(e.getMessage(), e); } viewManager = new ViewManager(playerList, game, Terminal.class); + viewManager.run(); } public static List parsePlayers() throws IllegalArgumentException, NoSuchMethodException, 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 0380c60..94d3478 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 @@ -11,9 +11,11 @@ public class ButtonListener extends AbstractModelListening { private final Game game; + private final DisplayWinnerEvent displayWinnerEvent; public ButtonListener(Game game) { this.game = game; + this.displayWinnerEvent = new DisplayWinnerEvent(); } @Override @@ -30,12 +32,16 @@ public class ButtonListener extends AbstractModelListening { @Override public void updateModel(Object event) { Player player = game.getCurrentPlayer(); - game.play(); + boolean isOver = game.play(); /* Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Un joueur ordinateur a joué"); alert.setHeaderText("Le joueur ordinateur numéro" + player.getId() + " a joué"); alert.setContentText("Il a joué l'action: " + game.getSelectedAction()); */ App.getViewManager().updateView(); + if(isOver) { + displayWinnerEvent.updateModel(game.getWinner()); + System.exit(0); + } //alert.showAndWait(); } } diff --git a/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java b/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java index d732f8e..c393153 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java +++ b/client/src/main/java/fr/lnl/game/client/listener/DisplayWinnerEvent.java @@ -1,6 +1,7 @@ package fr.lnl.game.client.listener; import fr.lnl.game.client.App; +import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.listener.AbstractModelListening; public class DisplayWinnerEvent extends AbstractModelListening { @@ -8,6 +9,7 @@ public class DisplayWinnerEvent extends AbstractModelListening { // TODO: 07/12/2021 PROBLEM -> ViewManager retourne null @Override public void updateModel(Object obj) { - App.getViewManager().displayWinner(App.getGame().getWinner()); + Player winner = (Player) obj; + App.getViewManager().displayWinner(winner); } } diff --git a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java index 164ad3c..cafb5c4 100644 --- a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java +++ b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java @@ -1,42 +1,45 @@ package fr.lnl.game.client.view; import fr.lnl.game.client.ClientPlayer; +import fr.lnl.game.client.listener.DisplayWinnerEvent; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.player.Player; import java.util.HashMap; -public class ViewManager { +public record ViewManager( + HashMap players, + Game game, Class viewType) { - private final HashMap players; - private final Game game; - - public ViewManager(HashMap players, Game game, Class viewType) { - this.players = players; - this.game = game; - if(viewType == Terminal.class) { - terminalView(); - } else { - updateView(); - } - } - public void updateView() { players.get(game.getCurrentPlayer()).getView().show(); } public void terminalView() { - while(true) { + DisplayWinnerEvent displayWinnerEvent = new DisplayWinnerEvent(); + while (true) { Player player = game.getCurrentPlayer(); players.get(game.getCurrentPlayer()).getView().show(); - game.play(); + boolean isOver = game.play(); System.out.println("Le joueur ordinateur numéro " + player.getId() + " a joué"); System.out.println("Il a joué l'action: " + game.getSelectedAction()); + if (isOver) { + displayWinnerEvent.updateModel(game.getWinner()); + System.exit(0); + } } } public void displayWinner(Player winner) { players.get(game.getCurrentPlayer()).getView().displayWinner(winner); } + + public void run() { + if (viewType == Terminal.class) { + terminalView(); + } else { + updateView(); + } + } } 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 2b31935..5ed98bd 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 @@ -6,13 +6,10 @@ import fr.lnl.game.server.games.grid.build.GridFactoryBuilder; import fr.lnl.game.server.games.grid.elements.CountdownBox; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.listener.AbstractModelListening; -import fr.lnl.game.server.listener.GameFinishEvent; -import fr.lnl.game.server.listener.ModelListener; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.function.Predicate.not; @@ -22,12 +19,10 @@ public class Game { private final GridFactoryBuilder buildStrategy; private final Grid grid; private final List players; - private final ModelListener gameFinishEvent; - private final AbstractModelListening displayWinnerEvent; private Player currentPlayer; private Action selectedAction = null; - public Game(GridFactoryBuilder buildStrategy, List players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException { + public Game(GridFactoryBuilder buildStrategy, List players) throws IllegalArgumentException { this.grid = buildStrategy.build(); if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); @@ -37,8 +32,6 @@ public class Game { this.buildStrategy = buildStrategy; this.players = players; this.currentPlayer = players.get(0); - this.gameFinishEvent = new GameFinishEvent(this); - this.displayWinnerEvent = displayWinnerEvent; initGame(); } @@ -47,16 +40,18 @@ public class Game { currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); } - public void play() { + /** + * + * @return true if game is over, false otherwise + */ + public boolean play() { selectedAction = currentPlayer.choseAction(); selectedAction.doAction(); countdownGridElementsUpdate(); gridPlayersUpdate(); nextCurrentPlayer(); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); - if(isOver()) { - gameFinishEvent.updateModel(null); - } + return isOver(); } private void gridPlayersUpdate(){ @@ -139,8 +134,4 @@ public class Game { public Action getSelectedAction() { return selectedAction; } - - public AbstractModelListening getDisplayWinnerEvent() { - return displayWinnerEvent; - } } \ No newline at end of file diff --git a/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java b/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java deleted file mode 100644 index dc7c5f0..0000000 --- a/server/src/main/java/fr/lnl/game/server/listener/GameFinishEvent.java +++ /dev/null @@ -1,23 +0,0 @@ -package fr.lnl.game.server.listener; - -import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.utils.CrashException; - -import java.util.concurrent.TimeUnit; - -public class GameFinishEvent extends AbstractModelListening { - - private final Object lock = new Object(); - private final Game game; - - public GameFinishEvent(Game game) { - this.game = game; - } - - // TODO: 02/12/2021 méthode a revoir, notamment surement mettre un dialog sur la view Window et fermer dès que l'utilisateur a cliqué sur ok - @Override - public void updateModel(Object obj) { - game.getDisplayWinnerEvent().updateModel(null); - System.exit(0); - } -}