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 4ba892c..482ea7c 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,7 +1,14 @@ package fr.lnl.game.client.view; 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.player.Player; +import fr.lnl.game.server.utils.Maths; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; public abstract class AbstractView implements View { @@ -12,4 +19,39 @@ public abstract class AbstractView implements View { this.game = game; this.player = player; } + + protected List generateAvailableActions() { + List actions = new ArrayList<>(); + for (Action a : player.getActions()) { + ReunionSameAction reunionFilter = actions.stream() + .filter(r -> r.getActionName().equals(a.getClass().getSimpleName())) + .findFirst() + .orElse(null); + if(reunionFilter != null){ + reunionFilter.addAction(a); + } + else{ + actions.add(new ReunionSameAction(a.getClass().getSimpleName(),a)); + } + } + return actions; + } + + 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 b4a916c..5ad8f95 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 @@ -1,10 +1,19 @@ 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; public class Terminal extends AbstractView { + public static Scanner scanner = new Scanner(System.in); + public Terminal(Game game, Player player) { super(game, player); } @@ -18,4 +27,39 @@ public class Terminal extends AbstractView { System.out.println("Le joueur " + winner + " a gagné la partie"); } + @Override + public Action choseAction() { + List actions = generateAvailableActions(); + List listActions = choseReunionSameAction(actions).getActions(); + Action action = null; + String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + listActions.size(); + do { + if(listActions.size() == 1){ + return listActions.get(0); + } + System.out.println("Choisissez la cible :"); + for (int i = 0; i < listActions.size(); i++) { + Action a = listActions.get(i); + if(a instanceof Move m){ + System.out.println(i + 1 + " : " + m.getDirection()); + } + else if(a instanceof DropObject o){ + System.out.println(i + 1 + " : " + o.getDirection()); + } + else if(a instanceof Shot s){ + System.out.println(i + 1 + " : " + s.getPoint()); + } + else{ + System.out.println(i + 1 + " : " + a.getClass().getSimpleName()); + } + } + String entry = scanner.next(); + int value = Maths.testInteger(entry, scanner, error); + if (value >= 1 && value <= listActions.size()) { + action = listActions.get(value - 1); + } + } while (action == null); + return action; + } + } 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 17381eb..f2b8df9 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,5 +1,7 @@ package fr.lnl.game.client.view; +import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.games.player.HumanPlayer; import fr.lnl.game.server.games.player.Player; public interface View { @@ -7,4 +9,10 @@ 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 cafb5c4..0280a77 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 @@ -3,6 +3,7 @@ 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.HumanPlayer; import fr.lnl.game.server.games.player.Player; import java.util.HashMap; @@ -21,6 +22,9 @@ public record ViewManager( while (true) { Player player = game.getCurrentPlayer(); players.get(game.getCurrentPlayer()).getView().show(); + if(game.getCurrentPlayer() instanceof HumanPlayer human) { + game.setSelectedAction(players.get(human).getView().choseAction()); + } 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()); 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 951edfb..6c93e99 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 @@ -4,6 +4,8 @@ import fr.lnl.game.client.App; import fr.lnl.game.client.listener.ButtonListener; import fr.lnl.game.client.listener.ClientEventHandler; 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.grid.Grid; import fr.lnl.game.server.games.grid.elements.*; import fr.lnl.game.server.games.player.Player; @@ -17,11 +19,11 @@ import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; -import javafx.scene.text.Font; import javafx.scene.text.Text; -import javafx.scene.text.TextAlignment; import javafx.stage.Stage; +import java.util.List; + public class Window extends AbstractView { @@ -58,6 +60,20 @@ public class Window extends AbstractView { alert.showAndWait(); } + @Override + public Action choseAction() { + List actions = generateAvailableActions(); + List listActions = choseReunionSameAction(actions).getActions(); + Action action = null; + do { + if(listActions.size() == 1){ + return listActions.get(0); + } + // TODO: 08/12/2021 implémenter choix voir Terminal pour savoir comment faire + }while(action == null); + return action; + } + 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()) 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 5ed98bd..a96cedf 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 @@ -4,8 +4,8 @@ import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.grid.Grid; 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.ComputerPlayer; import fr.lnl.game.server.games.player.Player; -import fr.lnl.game.server.listener.AbstractModelListening; import java.util.ArrayList; import java.util.Arrays; @@ -45,7 +45,9 @@ public class Game { * @return true if game is over, false otherwise */ public boolean play() { - selectedAction = currentPlayer.choseAction(); + if(currentPlayer instanceof ComputerPlayer computer) + // si le joueur est humain alors le choix se fait avant l'appel de play() + selectedAction = computer.choseAction(); selectedAction.doAction(); countdownGridElementsUpdate(); gridPlayersUpdate(); @@ -134,4 +136,8 @@ public class Game { public Action getSelectedAction() { return selectedAction; } + + public void setSelectedAction(Action selectedAction) { + this.selectedAction = selectedAction; + } } \ No newline at end of file diff --git a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java index 9468333..a6fd6c5 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java @@ -1,5 +1,6 @@ package fr.lnl.game.server.games.player; +import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.utils.Point; public abstract class ComputerPlayer extends AbstractPlayer { @@ -7,4 +8,6 @@ public abstract class ComputerPlayer extends AbstractPlayer { public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { super(id, point, false, classPlayer); } + + public abstract Action choseAction(); } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java index 7abd96c..a5ff86d 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java @@ -1,82 +1,12 @@ package fr.lnl.game.server.games.player; -import fr.lnl.game.server.games.action.*; -import fr.lnl.game.server.utils.Maths; import fr.lnl.game.server.utils.Point; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - public class HumanPlayer extends AbstractPlayer { - private List actions; - public HumanPlayer(Integer id, Point point, ClassPlayer classPlayer) { super(id, point,false, classPlayer); } - @Override - // TODO: 07/12/2021 Retirer les scanners pour respecter le MVC - public Action choseAction() { - actions = new ArrayList<>(); - for (Action a : getActions()) { - ReunionSameAction reunionFilter = actions.stream().filter(r -> r.getActionName().equals(a.getClass().getSimpleName())).findFirst().orElse(null); - if(reunionFilter != null){ - reunionFilter.addAction(a); - } - else{ - actions.add(new ReunionSameAction(a.getClass().getSimpleName(),a)); - } - } - List listActions = choseReunionSameAction().getActions(); - Action action = null; - String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + listActions.size(); - Scanner scanner = new Scanner(System.in); - do{ - if(listActions.size() == 1){ - return listActions.get(0); - } - System.out.println("Choisissez la cible :"); - for (int i = 0; i < listActions.size(); i++) { - Action a = listActions.get(i); - if(a instanceof Move){ - System.out.println(i + 1 + " : " + ((Move) a).getDirection()); - } - else if(a instanceof DropObject){ - System.out.println(i + 1 + " : " + ((DropObject) a).getDirection()); - } - else if(a instanceof Shot){ - System.out.println(i + 1 + " : " + ((Shot) a).getPoint()); - } - else{ - System.out.println(i + 1 + " : " + a.getClass().getSimpleName()); - } - } - String entry = scanner.next(); - int value = Maths.testInteger(entry, scanner, error); - if (value >= 1 && value <= listActions.size()) { - action = listActions.get(value - 1); - } - }while (action == null); - return action; - } - public ReunionSameAction choseReunionSameAction(){ - 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/server/src/main/java/fr/lnl/game/server/games/player/Player.java b/server/src/main/java/fr/lnl/game/server/games/player/Player.java index 17c77e5..4eef35e 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/Player.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/Player.java @@ -8,8 +8,6 @@ import java.util.List; public interface Player { - Action choseAction(); - Point getPosition(); boolean isAlive();