add javadoc to client.view

This commit is contained in:
Quentin Legot 2021-12-09 15:39:45 +01:00
parent 94cd0bb030
commit 4caa131a4c
7 changed files with 118 additions and 16 deletions

View File

@ -6,7 +6,6 @@ import fr.lnl.game.server.listener.AbstractModelListening;
public class DisplayWinnerEvent extends AbstractModelListening { public class DisplayWinnerEvent extends AbstractModelListening {
// TODO: 07/12/2021 PROBLEM -> ViewManager retourne null
@Override @Override
public void updateModel(Object obj) { public void updateModel(Object obj) {
Player winner = (Player) obj; Player winner = (Player) obj;

View File

@ -1,12 +1,11 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.server.games.Game; 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.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 { public abstract class AbstractView implements View {
protected final Player player; protected final Player player;

View File

@ -2,36 +2,54 @@ package fr.lnl.game.client.view;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.*; 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.games.player.Player;
import fr.lnl.game.server.utils.Maths; import fr.lnl.game.server.utils.Maths;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
/**
* View terminal, use standard input and output
*/
public class Terminal extends AbstractView { 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) { public Terminal(Game game, Player player) {
super(game, player); super(game, player);
} }
/**
* Used to update view
*/
public void show() { public void show() {
System.out.println(game.getGrid().privateView(player)); 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 @Override
public void displayWinner(Player winner) { public void displayWinner(Player winner) {
System.out.println("\n\033[0;31m====== FIN DU JEU ======\033[0m"); System.out.println("\n\033[0;31m====== FIN DU JEU ======\033[0m");
System.out.println(game.getGrid().toString()); 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 * @return chosen action
* @see Terminal#choseReunionSameAction(List)
*/ */
public Action choseAction() { public Action choseAction() {
List<ReunionSameAction> actions = player.generateAvailableActions(); List<ReunionSameAction> actions = player.generateAvailableActions();
@ -67,6 +85,13 @@ public class Terminal extends AbstractView {
return action; 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<ReunionSameAction> actions) { private ReunionSameAction choseReunionSameAction(List<ReunionSameAction> actions) {
ReunionSameAction reunion = null; ReunionSameAction reunion = null;
String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size(); String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size();

View File

@ -1,11 +1,20 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
/**
* View interface, implemented by Terminal and Window.
*/
public interface View { public interface View {
/**
* used to update screen
*/
void show(); 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);
} }

View File

@ -9,6 +9,7 @@ import fr.lnl.game.server.games.player.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
import java.util.Scanner;
public final class ViewManager { public final class ViewManager {
private final Game game; private final Game game;
@ -30,6 +31,7 @@ public final class ViewManager {
} }
public void terminalView() { public void terminalView() {
Terminal.scanner = new Scanner(System.in);
DisplayWinnerEvent displayWinnerEvent = new DisplayWinnerEvent(); DisplayWinnerEvent displayWinnerEvent = new DisplayWinnerEvent();
while (true) { while (true) {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
@ -44,6 +46,7 @@ public final class ViewManager {
game.getSelectedAction().getClass().getSimpleName() + "\033[0m"); game.getSelectedAction().getClass().getSimpleName() + "\033[0m");
if (isOver) { if (isOver) {
displayWinnerEvent.updateModel(game.getWinner()); displayWinnerEvent.updateModel(game.getWinner());
Terminal.scanner.close();
System.exit(0); System.exit(0);
} }
} }

View File

@ -28,10 +28,13 @@ import javafx.stage.Stage;
import java.util.List; import java.util.List;
/**
* Window view, use mouse and keyboard to control interface
*/
public class Window extends AbstractView { 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 cellSize = 40;
public static final int width = 500; public static final int width = 500;
public static final int height = 160; public static final int height = 160;
@ -42,7 +45,7 @@ public class Window extends AbstractView {
private final Stage stage; private final Stage stage;
private Pane buttonPane; private Pane buttonPane;
private ReunionSameAction selectedReunionAction = null; 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) { public Window(Stage stage, Game game, Player player) {
@ -50,6 +53,9 @@ public class Window extends AbstractView {
this.stage = stage; this.stage = stage;
} }
/**
* used to update screen
*/
public void show() { public void show() {
Scene scene = new Scene(createContent()); Scene scene = new Scene(createContent());
stage.setScene(scene); stage.setScene(scene);
@ -60,16 +66,29 @@ public class Window extends AbstractView {
stage.show(); stage.show();
} }
/**
* Used to display the name of the winner
* @param winner the player who win the game, can be Null
*/
@Override @Override
public void displayWinner(Player winner) { public void displayWinner(Player winner) {
Alert alert = new Alert(Alert.AlertType.INFORMATION); Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Fin du jeu"); alert.setTitle("Fin du jeu");
alert.setHeaderText("La partie est termin\u00E9"); 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(); App.getViewManager().updateView();
alert.showAndWait(); 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) { public void choseDirectionAction(ReunionSameAction selectedReunionAction) {
for(int i = 0; i < selectedReunionAction.getActions().size(); ++i) { for(int i = 0; i < selectedReunionAction.getActions().size(); ++i) {
Action action = selectedReunionAction.getActions().get(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<ReunionSameAction> actions) { private void choseReunionSameAction(List<ReunionSameAction> actions) {
for (int i = 0; i < actions.size(); i++) { for (int i = 0; i < actions.size(); i++) {
ReunionSameAction action = actions.get(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<ActionEvent> listener, Pane pane, int offsetX, int offsetY) { private void addButtonToPane(String content, EventHandler<ActionEvent> listener, Pane pane, int offsetX, int offsetY) {
Button button = new Button(content); Button button = new Button(content);
button.setOnAction(listener); 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() { private Parent createContent() {
Pane principalPane = new Pane(); 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()) 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; 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) { public void addToPrincipalPanel(Object object, Pane principalPane, int i, int j) {
StackPane sp = Cell.setImageObject(object, game); StackPane sp = Cell.setImageObject(object, game);
sp.setLayoutY(i * cellSize); sp.setLayoutY(i * cellSize);
@ -170,6 +215,10 @@ public class Window extends AbstractView {
principalPane.getChildren().add(sp); 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){ public void putStatePlayerPane(Pane principalPane){
int Y = 0; int Y = 0;
for(int i=0;i<game.getPlayers().size();i++){ for(int i=0;i<game.getPlayers().size();i++){
@ -181,6 +230,12 @@ public class Window extends AbstractView {
} }
} }
/**
* Build left down pane (list all players information)
* @param type
* @param playerNumber
* @return
*/
public StackPane showStatePlayer(String type, int playerNumber){ public StackPane showStatePlayer(String type, int playerNumber){
StackPane subSp = new StackPane(); StackPane subSp = new StackPane();
String s = type + " " + (playerNumber+1) + "\n" + String s = type + " " + (playerNumber+1) + "\n" +
@ -201,12 +256,20 @@ public class Window extends AbstractView {
return subSp; return subSp;
} }
/**
* build left down pane and move it to its position
* @param principalPane the principal pane where we'll add the left down pane
*/
public void putMoveTextPane(Pane principalPane){ public void putMoveTextPane(Pane principalPane){
StackPane stateMoveTextPane = showMoveText(); StackPane stateMoveTextPane = showMoveText();
stateMoveTextPane.setLayoutY(480); stateMoveTextPane.setLayoutY(480);
principalPane.getChildren().add((stateMoveTextPane)); principalPane.getChildren().add((stateMoveTextPane));
} }
/**
* Build the left down pane (contains current player information)
* @return the built pane
*/
public StackPane showMoveText() { public StackPane showMoveText() {
StackPane subSp = new StackPane(); StackPane subSp = new StackPane();
String action = game.getSelectedAction() == null ? "null" : game.getSelectedAction().getClass().getSimpleName(); String action = game.getSelectedAction() == null ? "null" : game.getSelectedAction().getClass().getSimpleName();

View File

@ -0,0 +1,4 @@
/**
* View package, contains all classes about user view
*/
package fr.lnl.game.client.view;