From 71f87a3f1c929bd3d0566cb6fa91723c8de9cf55 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Dec 2021 12:47:41 +0100 Subject: [PATCH] Added buttons when player is human --- .../client/listener/ClientEventHandler.java | 9 +- ...ner.java => NextPlayerButtonListener.java} | 6 +- .../client/listener/SelectActionButton.java | 34 ++++++ .../listener/SelectDirectionListener.java | 28 +++++ .../fr/lnl/game/client/view/AbstractView.java | 18 ---- .../fr/lnl/game/client/view/Terminal.java | 23 +++- .../java/fr/lnl/game/client/view/View.java | 6 -- .../fr/lnl/game/client/view/ViewManager.java | 2 +- .../java/fr/lnl/game/client/view/Window.java | 101 +++++++++++++----- .../java/fr/lnl/game/server/games/Game.java | 7 ++ 10 files changed, 172 insertions(+), 62 deletions(-) rename client/src/main/java/fr/lnl/game/client/listener/{ButtonListener.java => NextPlayerButtonListener.java} (88%) create mode 100644 client/src/main/java/fr/lnl/game/client/listener/SelectActionButton.java create mode 100644 client/src/main/java/fr/lnl/game/client/listener/SelectDirectionListener.java diff --git a/client/src/main/java/fr/lnl/game/client/listener/ClientEventHandler.java b/client/src/main/java/fr/lnl/game/client/listener/ClientEventHandler.java index 7c3d7ca..4407ea9 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ClientEventHandler.java +++ b/client/src/main/java/fr/lnl/game/client/listener/ClientEventHandler.java @@ -4,13 +4,8 @@ import fr.lnl.game.server.listener.ModelListener; import javafx.event.ActionEvent; import javafx.event.EventHandler; -public class ClientEventHandler implements EventHandler { - - private final ModelListener listener; - - public ClientEventHandler(ButtonListener listener) { - this.listener = listener; - } +public record ClientEventHandler( + ModelListener listener) implements EventHandler { @Override public void handle(ActionEvent event) { diff --git a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java b/client/src/main/java/fr/lnl/game/client/listener/NextPlayerButtonListener.java similarity index 88% rename from client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java rename to client/src/main/java/fr/lnl/game/client/listener/NextPlayerButtonListener.java index 94d3478..3bd230c 100644 --- a/client/src/main/java/fr/lnl/game/client/listener/ButtonListener.java +++ b/client/src/main/java/fr/lnl/game/client/listener/NextPlayerButtonListener.java @@ -7,13 +7,13 @@ import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.ModelListener; import javafx.scene.control.Alert; -public class ButtonListener extends AbstractModelListening { +public class NextPlayerButtonListener extends AbstractModelListening { private final Game game; private final DisplayWinnerEvent displayWinnerEvent; - public ButtonListener(Game game) { + public NextPlayerButtonListener(Game game) { this.game = game; this.displayWinnerEvent = new DisplayWinnerEvent(); } @@ -31,7 +31,7 @@ public class ButtonListener extends AbstractModelListening { @Override public void updateModel(Object event) { - Player player = game.getCurrentPlayer(); + // Player player = game.getCurrentPlayer(); boolean isOver = game.play(); /* Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Un joueur ordinateur a joué"); diff --git a/client/src/main/java/fr/lnl/game/client/listener/SelectActionButton.java b/client/src/main/java/fr/lnl/game/client/listener/SelectActionButton.java new file mode 100644 index 0000000..a4341e2 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/listener/SelectActionButton.java @@ -0,0 +1,34 @@ +package fr.lnl.game.client.listener; + +import fr.lnl.game.client.App; +import fr.lnl.game.client.view.Window; +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.action.ReunionSameAction; +import fr.lnl.game.server.listener.AbstractModelListening; + +public class SelectActionButton extends AbstractModelListening { + + private final Window window; + private final ReunionSameAction reunionSameAction; + private final Game game; + + public SelectActionButton(Game game, Window window, ReunionSameAction reunionSameAction) { + this.game = game; + this.window = window; + this.reunionSameAction = reunionSameAction; + } + + @Override + public void updateModel(Object obj) { + if(reunionSameAction.getActions().size() == 1){ + game.setSelectedAction(reunionSameAction.getActions().get(0)); + window.getNextPlayerButtonListener().updateModel(obj); + } else { + window.setSelectedReunionAction(reunionSameAction); + App.getViewManager().updateView(); // update screen + } + + + + } +} diff --git a/client/src/main/java/fr/lnl/game/client/listener/SelectDirectionListener.java b/client/src/main/java/fr/lnl/game/client/listener/SelectDirectionListener.java new file mode 100644 index 0000000..e967037 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/listener/SelectDirectionListener.java @@ -0,0 +1,28 @@ +package fr.lnl.game.client.listener; + +import fr.lnl.game.client.view.Window; +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.listener.AbstractModelListening; +import fr.lnl.game.server.listener.ModelListener; + +public class SelectDirectionListener extends AbstractModelListening { + + private final Game game; + private final Action action; + private final Window window; + + public SelectDirectionListener(Game game, Window window, Action action) { + this.game = game; + this.window = window; + this.action = action; + } + + @Override + public void updateModel(Object obj) { + game.setSelectedAction(action); + window.setSelectedReunionAction(null); + window.getNextPlayerButtonListener().updateModel(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 11b328a..0e291fa 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 @@ -16,22 +16,4 @@ public abstract class AbstractView implements View { this.game = game; this.player = player; } - - protected ReunionSameAction choseReunionSameAction(List actions) { - ReunionSameAction reunion = null; - String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size(); - Scanner scanner = new Scanner(System.in); - do { - System.out.println("Choisissez une action :"); - for (int i = 0; i < actions.size(); i++) { - System.out.println(i + 1 + " : " + actions.get(i).getActionName()); - } - String entry = scanner.next(); - int value = Maths.testInteger(entry, scanner, error); - if (value >= 1 && value <= actions.size()) { - reunion = actions.get(value - 1); - } - }while (reunion == null) ; - return reunion; - } } 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 816d87c..ef75eb5 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 @@ -29,7 +29,10 @@ public class Terminal extends AbstractView { System.out.println("\n\033[0;33mVictoire de " + winner + " " + winner.getId() + "\033[0m"); } - @Override + /** + * Used when current player is an isntance of {@link fr.lnl.game.server.games.player.HumanPlayer} + * @return chosen action + */ public Action choseAction() { List actions = player.generateAvailableActions(); List listActions = choseReunionSameAction(actions).getActions(); @@ -64,4 +67,22 @@ public class Terminal extends AbstractView { return action; } + private ReunionSameAction choseReunionSameAction(List actions) { + ReunionSameAction reunion = null; + String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size(); + Scanner scanner = new Scanner(System.in); + do { + System.out.println("Choisissez une action :"); + for (int i = 0; i < actions.size(); i++) { + System.out.println(i + 1 + " : " + actions.get(i).getActionName()); + } + String entry = scanner.next(); + int value = Maths.testInteger(entry, scanner, error); + if (value >= 1 && value <= actions.size()) { + reunion = actions.get(value - 1); + } + }while (reunion == null) ; + return reunion; + } + } 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 8bcfa2e..2cf802e 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 @@ -8,10 +8,4 @@ public interface View { void show(); void displayWinner(Player winner); - - /** - * Used when current player is an isntance of {@link fr.lnl.game.server.games.player.HumanPlayer} - * @return chosen action - */ - Action choseAction(); } 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 126fe5a..8de5175 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 @@ -25,7 +25,7 @@ public record ViewManager( 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) { - game.setSelectedAction(players.get(human).getView().choseAction()); + 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"+ 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 00ce2e2..ff92f7d 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 @@ -1,16 +1,20 @@ package fr.lnl.game.client.view; import fr.lnl.game.client.App; -import fr.lnl.game.client.listener.ButtonListener; +import fr.lnl.game.client.listener.NextPlayerButtonListener; import fr.lnl.game.client.listener.ClientEventHandler; +import fr.lnl.game.client.listener.SelectActionButton; +import fr.lnl.game.client.listener.SelectDirectionListener; import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.action.Action; -import fr.lnl.game.server.games.action.ReunionSameAction; +import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.elements.*; +import fr.lnl.game.server.games.player.HumanPlayer; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; @@ -34,6 +38,9 @@ public class Window extends AbstractView { private final Stage stage; + private Pane buttonPane; + private ReunionSameAction selectedReunionAction = null; + private NextPlayerButtonListener nextPlayerButtonListener = new NextPlayerButtonListener(game); public Window(Stage stage, Game game, Player player) { @@ -60,20 +67,47 @@ public class Window extends AbstractView { alert.showAndWait(); } - @Override - public Action choseAction() { - List actions = player.generateAvailableActions(); - List listActions = choseReunionSameAction(actions).getActions(); - Action action = null; - do { - if(listActions.size() == 1){ - return listActions.get(0); + public void choseDirectionAction(ReunionSameAction selectedReunionAction) { + for(int i = 0; i < selectedReunionAction.getActions().size(); ++i) { + Action action = selectedReunionAction.getActions().get(i); + if(action instanceof Move m){ + addButtonToPane(m.getDirection().toString(), + new ClientEventHandler(new SelectDirectionListener(game, this, action)), buttonPane, + i * 100 + 50, 0); } - // TODO: 08/12/2021 implémenter choix voir Terminal pour savoir comment faire - }while(action == null); - return action; + else if(action instanceof DropObject o){ + addButtonToPane(o.getDirection().toString(), new ClientEventHandler(new SelectDirectionListener(game, this, action)), buttonPane, i * 100 + 50, 0); + } + else if(action instanceof Shot s){ + addButtonToPane(s.getPoint().toString(), new ClientEventHandler(new SelectDirectionListener(game, this, action)), buttonPane, i * 100 + 50, 0); + } + else{ + addButtonToPane(action.getClass().getSimpleName(), new ClientEventHandler(new SelectDirectionListener(game, this, action)), buttonPane, i * 100 + 50, 0); + } + } } + private void choseReunionSameAction(List actions) { + for (int i = 0; i < actions.size(); i++) { + ReunionSameAction action = actions.get(i); + addButtonToPane(action.getActionName(), new ClientEventHandler(new SelectActionButton(game, this, action)), + buttonPane, i * 100 + 50, 0); + } + } + + private void addButtonToPane(String content, EventHandler listener, Pane pane, int offsetX, int offsetY) { + Button button = new Button(content); + button.setOnAction(listener); + button.setPrefSize(85, 35); + button.setStyle("-fx-background-color: #a96806;"); + button.setTextFill(javafx.scene.paint.Color.WHITE); + button.setLayoutX(offsetX - button.getPrefWidth() / 2); + button.setLayoutY(offsetY); + pane.getChildren().add(button); + } + + + 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()) @@ -102,13 +136,20 @@ public class Window extends AbstractView { } putStatePlayerPane(principalPane); putMoveTextPane(principalPane); - Button buttonNext = new Button("SUIVANT"); - buttonNext .setOnAction(new ClientEventHandler(new ButtonListener(game))); - buttonNext .setLayoutX(700); - buttonNext .setLayoutY(600); - buttonNext .setStyle("-fx-background-color: #a96806;"); - buttonNext .setTextFill(javafx.scene.paint.Color.WHITE); - principalPane.getChildren().add(buttonNext); + this.buttonPane = new Pane(); + buttonPane.setLayoutX(0); + buttonPane.setLayoutY(600); + if(game.getCurrentPlayer() instanceof HumanPlayer) { + if(this.selectedReunionAction == null) { + choseReunionSameAction(player.generateAvailableActions()); + } else { + choseDirectionAction(selectedReunionAction); + } + } else { + addButtonToPane("SUIVANT", new ClientEventHandler(nextPlayerButtonListener), buttonPane, (int) (principalPane.getPrefWidth() / 2), 0); + } + + principalPane.getChildren().add(buttonPane); return principalPane; } @@ -159,11 +200,15 @@ public class Window extends AbstractView { principalPane.getChildren().add((stateMoveTextPane)); } - public StackPane showMoveText(){ + public StackPane showMoveText() { StackPane subSp = new StackPane(); String action = game.getSelectedAction() == null ? "null" : game.getSelectedAction().getClass().getSimpleName(); - String s = player + " : " + (player.getId()+1) + "\n" + - "Vient de jouer : " + action + "\n"; + String s = ""; + if(game.getPreviousPlayer() != null) { + s = game.getPreviousPlayer() + " : " + (game.getPreviousPlayer().getId()+1) + "\n" + + "Vient de jouer : " + action + "\n"; + } + Text t = new Text(s); Rectangle r = new Rectangle(); r.setWidth(478); @@ -175,7 +220,11 @@ public class Window extends AbstractView { return subSp; } + public void setSelectedReunionAction(ReunionSameAction selectedReunionAction) { + this.selectedReunionAction = selectedReunionAction; + } - - + public NextPlayerButtonListener getNextPlayerButtonListener() { + return nextPlayerButtonListener; + } } 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 ea9cfd4..70f177a 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 @@ -21,6 +21,7 @@ public class Game { private Player currentPlayer; private Action selectedAction = null; private int nbrTurn; + private Player previousPlayer; /** * @param buildStrategy used to build a grid @@ -37,6 +38,7 @@ public class Game { this.players = players; this.currentPlayer = players.get(0); this.nbrTurn = 1; + this.previousPlayer = null; initGame(buildStrategy); } @@ -61,6 +63,7 @@ public class Game { selectedAction.doAction(); countdownGridElementsUpdate(); gridPlayersUpdate(); + previousPlayer = currentPlayer; nextCurrentPlayer(); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); nbrTurn++; @@ -199,4 +202,8 @@ public class Game { public int getNbrTurn() { return nbrTurn; } + + public Player getPreviousPlayer() { + return previousPlayer; + } } \ No newline at end of file