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 c393153..0b6dff5 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 @@ -6,7 +6,6 @@ import fr.lnl.game.server.listener.AbstractModelListening; public class DisplayWinnerEvent extends AbstractModelListening { - // TODO: 07/12/2021 PROBLEM -> ViewManager retourne null @Override public void updateModel(Object obj) { Player winner = (Player) obj; diff --git a/client/src/main/java/fr/lnl/game/client/view/AbstractView.java b/client/src/main/java/fr/lnl/game/client/view/AbstractView.java index 0e291fa..ec020d6 100644 --- a/client/src/main/java/fr/lnl/game/client/view/AbstractView.java +++ b/client/src/main/java/fr/lnl/game/client/view/AbstractView.java @@ -1,12 +1,11 @@ package fr.lnl.game.client.view; import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.action.ReunionSameAction; import fr.lnl.game.server.games.player.Player; -import fr.lnl.game.server.utils.Maths; -import java.util.List; -import java.util.Scanner; +/** + * Abstract class implemented by every view classes + */ public abstract class AbstractView implements View { protected final Player player; diff --git a/client/src/main/java/fr/lnl/game/client/view/Terminal.java b/client/src/main/java/fr/lnl/game/client/view/Terminal.java index ef75eb5..86dbbac 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Terminal.java +++ b/client/src/main/java/fr/lnl/game/client/view/Terminal.java @@ -2,36 +2,54 @@ package fr.lnl.game.client.view; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.action.*; -import fr.lnl.game.server.games.player.HumanPlayer; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Maths; -import java.util.ArrayList; import java.util.List; import java.util.Scanner; +/** + * View terminal, use standard input and output + */ public class Terminal extends AbstractView { - public static Scanner scanner = new Scanner(System.in); + /** + * Standard input + */ + public static Scanner scanner; public Terminal(Game game, Player player) { super(game, player); } + /** + * Used to update view + */ public void show() { System.out.println(game.getGrid().privateView(player)); } + /** + * Used to display the winner of the game + * @param winner the player who win the game, can be Null + */ @Override public void displayWinner(Player winner) { System.out.println("\n\033[0;31m====== FIN DU JEU ======\033[0m"); System.out.println(game.getGrid().toString()); - System.out.println("\n\033[0;33mVictoire de " + winner + " " + winner.getId() + "\033[0m"); + if(winner == null) { + System.out.println("\n\033[0;Partie nulle, personne n'a gagné la partie\033[0m"); + } else { + System.out.println("\n\033[0;33mVictoire de " + winner + " " + winner.getId() + "\033[0m"); + } + } /** - * Used when current player is an isntance of {@link fr.lnl.game.server.games.player.HumanPlayer} + * Used when current player is an instance of {@link fr.lnl.game.server.games.player.HumanPlayer} and demand to it + * an action to do * @return chosen action + * @see Terminal#choseReunionSameAction(List) */ public Action choseAction() { List actions = player.generateAvailableActions(); @@ -67,6 +85,13 @@ public class Terminal extends AbstractView { return action; } + /** + * Used when current player is an instance of {@link fr.lnl.game.server.games.player.HumanPlayer} and demand to it + * a type of action to do + * @param actions the list of actions possible + * @return the type of action to execute + * @see Terminal#choseAction() + */ private ReunionSameAction choseReunionSameAction(List actions) { ReunionSameAction reunion = null; String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size(); diff --git a/client/src/main/java/fr/lnl/game/client/view/View.java b/client/src/main/java/fr/lnl/game/client/view/View.java index 2cf802e..3b22907 100644 --- a/client/src/main/java/fr/lnl/game/client/view/View.java +++ b/client/src/main/java/fr/lnl/game/client/view/View.java @@ -1,11 +1,20 @@ package fr.lnl.game.client.view; -import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.player.Player; +/** + * View interface, implemented by Terminal and Window. + */ public interface View { + /** + * used to update screen + */ void show(); - void displayWinner(Player winner); + /** + * Used to display the name of the winner + * @param winner the player who win the game, can be Null + */ + void displayWinner(/* Nullable */ Player 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 c33ac91..b3ccd57 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 @@ -9,6 +9,7 @@ import fr.lnl.game.server.games.player.Player; import java.util.HashMap; import java.util.Objects; +import java.util.Scanner; public final class ViewManager { private final Game game; @@ -30,6 +31,7 @@ public final class ViewManager { } public void terminalView() { + Terminal.scanner = new Scanner(System.in); DisplayWinnerEvent displayWinnerEvent = new DisplayWinnerEvent(); while (true) { Player player = game.getCurrentPlayer(); @@ -44,6 +46,7 @@ public final class ViewManager { game.getSelectedAction().getClass().getSimpleName() + "\033[0m"); if (isOver) { displayWinnerEvent.updateModel(game.getWinner()); + Terminal.scanner.close(); System.exit(0); } } diff --git a/client/src/main/java/fr/lnl/game/client/view/Window.java b/client/src/main/java/fr/lnl/game/client/view/Window.java index 8eb7c18..d517f07 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Window.java +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -28,10 +28,13 @@ import javafx.stage.Stage; import java.util.List; +/** + * Window view, use mouse and keyboard to control interface + */ public class Window extends AbstractView { - //il faut pouvoir trouver une formule responsive avec width et height + // il faut pouvoir trouver une formule responsive avec width et height public static final int cellSize = 40; public static final int width = 500; public static final int height = 160; @@ -42,7 +45,7 @@ public class Window extends AbstractView { private final Stage stage; private Pane buttonPane; private ReunionSameAction selectedReunionAction = null; - private NextPlayerButtonListener nextPlayerButtonListener = new NextPlayerButtonListener(game); + private final NextPlayerButtonListener nextPlayerButtonListener = new NextPlayerButtonListener(game); public Window(Stage stage, Game game, Player player) { @@ -50,6 +53,9 @@ public class Window extends AbstractView { this.stage = stage; } + /** + * used to update screen + */ public void show() { Scene scene = new Scene(createContent()); stage.setScene(scene); @@ -60,16 +66,29 @@ public class Window extends AbstractView { stage.show(); } + /** + * Used to display the name of the winner + * @param winner the player who win the game, can be Null + */ @Override public void displayWinner(Player winner) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Fin du jeu"); alert.setHeaderText("La partie est termin\u00E9"); - alert.setContentText("Un joueur " + winner + " a gagn\u00E9"); + if(winner == null) { + alert.setContentText("La partie est nulle, personne n'a gagn\u00E9"); + } else { + alert.setContentText("Le joueur " + winner + " " + winner.getId() + " a gagn\u00E9"); + } App.getViewManager().updateView(); alert.showAndWait(); } + /** + * Choose a direction between all possible from the previously selected action type + * @param selectedReunionAction previously selected type of action + * @see Window#choseReunionSameAction(List) + */ public void choseDirectionAction(ReunionSameAction selectedReunionAction) { for(int i = 0; i < selectedReunionAction.getActions().size(); ++i) { Action action = selectedReunionAction.getActions().get(i); @@ -90,6 +109,12 @@ public class Window extends AbstractView { } } + /** + * Used when {@link Game#getCurrentPlayer()} is an instance of {@link HumanPlayer}. + * Display button to demand to player to choose the type of action to execute + * @param actions the list of possible actions + * @see Window#choseDirectionAction(ReunionSameAction) + */ private void choseReunionSameAction(List actions) { for (int i = 0; i < actions.size(); i++) { ReunionSameAction action = actions.get(i); @@ -98,6 +123,16 @@ public class Window extends AbstractView { } } + /** + * called when we add a button in the interface + * @param content content of the button + * @param listener listener of the button + * @param pane pane where we add the button + * @param offsetX move the button from the base position of the pane to the left (when offsetX is negative) or on + * the right (when offsetY is positive) + * @param offsetY move the button from the base position of the pane to the up (when offsetX is negative) or on + * the down (when offsetY is positive) + */ private void addButtonToPane(String content, EventHandler listener, Pane pane, int offsetX, int offsetY) { Button button = new Button(content); button.setOnAction(listener); @@ -110,7 +145,10 @@ public class Window extends AbstractView { } - + /** + * Create content of the stage + * @return the parent element to set to the stage + */ private Parent createContent() { Pane principalPane = new Pane(); principalPane.setPrefSize(game.getGrid().getRow() * cellSize + width, game.getGrid().getColumn() * cellSize + height); // TODO: 04/12/2021 A corriger -> doit plutôt s'adapter à la taille de la grid (grid.getRow() et grid.getColumn()) @@ -163,6 +201,13 @@ public class Window extends AbstractView { return principalPane; } + /** + * Add grid element to the principal pane + * @param object object to add to the pane + * @param principalPane pane where we'll add the object + * @param i + * @param j + */ public void addToPrincipalPanel(Object object, Pane principalPane, int i, int j) { StackPane sp = Cell.setImageObject(object, game); sp.setLayoutY(i * cellSize); @@ -170,6 +215,10 @@ public class Window extends AbstractView { principalPane.getChildren().add(sp); } + /** + * Create the right pane + * @param principalPane principal pane where we'll add the left down pane + */ public void putStatePlayerPane(Pane principalPane){ int Y = 0; for(int i=0;i