From 4098e0163040f9fe0468131cd6bc0fde0f56ce4d Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Dec 2021 14:18:44 +0100 Subject: [PATCH] move playersList from App to ViewManager --- .../src/main/java/fr/lnl/game/client/App.java | 17 +++--- .../fr/lnl/game/client/view/ViewManager.java | 54 ++++++++++++++++--- client/src/main/java/module-info.java | 3 ++ 3 files changed, 59 insertions(+), 15 deletions(-) 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 7d4526a..c93c1e5 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -16,10 +16,12 @@ import javafx.stage.Stage; import java.lang.reflect.InvocationTargetException; import java.util.*; +/** + * Application starting point + */ public class App extends Application { private static LinkedList argsList; - public static HashMap playerList = new HashMap<>(); private static Game game; private static ViewManager viewManager; @@ -48,36 +50,33 @@ public class App extends Application { } } - public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, + public static void startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).gridDimensions(12, 12); game = new Game(builder, players); - for (Player player : game.getPlayers()) { - playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); - } } @Override public void start(Stage stage) { try { - startGame(player -> new Window(stage, game, player)); + startGame(); } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { throw new CrashException(e.getMessage(), e); } - viewManager = new ViewManager(playerList, game, Window.class); + viewManager = new ViewManager(game, Window.class, player -> new Window(stage, game, player)); viewManager.run(); } public static void launchTerminal() { try { - startGame(player -> new Terminal(game, player)); + startGame(); } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) { throw new CrashException(e.getMessage(), e); } - viewManager = new ViewManager(playerList, game, Terminal.class); + viewManager = new ViewManager(game, Terminal.class, player -> new Terminal(game, player)); viewManager.run(); } 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 8de5175..c33ac91 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,17 +1,29 @@ package fr.lnl.game.client.view; import fr.lnl.game.client.ClientPlayer; +import fr.lnl.game.client.ViewLambda; import fr.lnl.game.client.listener.DisplayWinnerEvent; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.player.HumanPlayer; import fr.lnl.game.server.games.player.Player; import java.util.HashMap; +import java.util.Objects; -public record ViewManager( - HashMap players, - Game game, Class viewType) { +public final class ViewManager { + private final Game game; + private final Class viewType; + public ViewManager(Game game, Class viewType, ViewLambda lambda) { + this.game = game; + this.viewType = viewType; + for (Player player : game.getPlayers()) { + players.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); + } + } + + + public HashMap players = new HashMap<>(); public void updateView() { players.get(game.getCurrentPlayer()).getView().show(); @@ -22,13 +34,13 @@ public record ViewManager( while (true) { Player player = game.getCurrentPlayer(); System.out.println("\n\033[0;34m====== Tour n°" + game.getNbrTurn() + " =======\033[0m"); - System.out.println("\nA \033[0;31m" + player + " " + player.getId() + "\033[0m de jouer"); + System.out.println("\nA \033[0;31m" + player + " " + player.getId() + "\033[0m de jouer"); players.get(game.getCurrentPlayer()).getView().show(); - if(game.getCurrentPlayer() instanceof HumanPlayer human) { + if (game.getCurrentPlayer() instanceof HumanPlayer human) { game.setSelectedAction(((Terminal) players.get(human).getView()).choseAction()); } boolean isOver = game.play(); - System.out.println("\n\033[0;31m" + player + " " + player.getId() + "\033[0m utilise l'action \033[0;36m"+ + System.out.println("\n\033[0;31m" + player + " " + player.getId() + "\033[0m utilise l'action \033[0;36m" + game.getSelectedAction().getClass().getSimpleName() + "\033[0m"); if (isOver) { displayWinnerEvent.updateModel(game.getWinner()); @@ -48,4 +60,34 @@ public record ViewManager( updateView(); } } + + public Game game() { + return game; + } + + public Class viewType() { + return viewType; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (ViewManager) obj; + return Objects.equals(this.game, that.game) && + Objects.equals(this.viewType, that.viewType); + } + + @Override + public int hashCode() { + return Objects.hash(game, viewType); + } + + @Override + public String toString() { + return "ViewManager[" + + "game=" + game + ", " + + "viewType=" + viewType + ']'; + } + } diff --git a/client/src/main/java/module-info.java b/client/src/main/java/module-info.java index 6ec1a87..6f92453 100644 --- a/client/src/main/java/module-info.java +++ b/client/src/main/java/module-info.java @@ -1,3 +1,6 @@ +/** + * Client module, include every view and controller classes from MVC model + */ module client { requires javafx.controls; requires transitive javafx.graphics;