Merge pull request #3 from quentinlegot/conflit-3

Added buttons when player is human
This commit is contained in:
Quentin Legot 2021-12-09 12:52:30 +01:00 committed by GitHub
commit 35875a8107
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 172 additions and 62 deletions

View File

@ -4,13 +4,8 @@ import fr.lnl.game.server.listener.ModelListener;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
public class ClientEventHandler implements EventHandler<ActionEvent> { public record ClientEventHandler(
ModelListener listener) implements EventHandler<ActionEvent> {
private final ModelListener listener;
public ClientEventHandler(ButtonListener listener) {
this.listener = listener;
}
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {

View File

@ -7,13 +7,13 @@ import fr.lnl.game.server.listener.AbstractModelListening;
import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.server.listener.ModelListener;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
public class ButtonListener extends AbstractModelListening { public class NextPlayerButtonListener extends AbstractModelListening {
private final Game game; private final Game game;
private final DisplayWinnerEvent displayWinnerEvent; private final DisplayWinnerEvent displayWinnerEvent;
public ButtonListener(Game game) { public NextPlayerButtonListener(Game game) {
this.game = game; this.game = game;
this.displayWinnerEvent = new DisplayWinnerEvent(); this.displayWinnerEvent = new DisplayWinnerEvent();
} }
@ -31,7 +31,7 @@ public class ButtonListener extends AbstractModelListening {
@Override @Override
public void updateModel(Object event) { public void updateModel(Object event) {
Player player = game.getCurrentPlayer(); // Player player = game.getCurrentPlayer();
boolean isOver = game.play(); boolean isOver = game.play();
/* Alert alert = new Alert(Alert.AlertType.INFORMATION); /* Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Un joueur ordinateur a joué"); alert.setTitle("Un joueur ordinateur a joué");

View File

@ -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
}
}
}

View File

@ -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);
}
}

View File

@ -16,22 +16,4 @@ public abstract class AbstractView implements View {
this.game = game; this.game = game;
this.player = player; this.player = player;
} }
protected ReunionSameAction choseReunionSameAction(List<ReunionSameAction> 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;
}
} }

View File

@ -29,7 +29,10 @@ public class Terminal extends AbstractView {
System.out.println("\n\033[0;33mVictoire de " + winner + " " + winner.getId() + "\033[0m"); 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() { public Action choseAction() {
List<ReunionSameAction> actions = player.generateAvailableActions(); List<ReunionSameAction> actions = player.generateAvailableActions();
List<Action> listActions = choseReunionSameAction(actions).getActions(); List<Action> listActions = choseReunionSameAction(actions).getActions();
@ -64,4 +67,22 @@ public class Terminal extends AbstractView {
return action; return action;
} }
private ReunionSameAction choseReunionSameAction(List<ReunionSameAction> 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;
}
} }

View File

@ -8,10 +8,4 @@ public interface View {
void show(); void show();
void displayWinner(Player winner); 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();
} }

View File

@ -25,7 +25,7 @@ public record ViewManager(
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(); players.get(game.getCurrentPlayer()).getView().show();
if(game.getCurrentPlayer() instanceof HumanPlayer human) { 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(); 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"+

View File

@ -1,16 +1,20 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
import fr.lnl.game.client.App; 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.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.Game;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.action.ReunionSameAction;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.elements.*; 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.games.player.Player;
import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
@ -36,6 +40,9 @@ public class Window extends AbstractView {
private final Stage stage; 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) { public Window(Stage stage, Game game, Player player) {
@ -63,19 +70,46 @@ public class Window extends AbstractView {
alert.showAndWait(); alert.showAndWait();
} }
@Override public void choseDirectionAction(ReunionSameAction selectedReunionAction) {
public Action choseAction() { for(int i = 0; i < selectedReunionAction.getActions().size(); ++i) {
List<ReunionSameAction> actions = player.generateAvailableActions(); Action action = selectedReunionAction.getActions().get(i);
List<Action> listActions = choseReunionSameAction(actions).getActions(); if(action instanceof Move m){
Action action = null; addButtonToPane(m.getDirection().toString(),
do { new ClientEventHandler(new SelectDirectionListener(game, this, action)), buttonPane,
if(listActions.size() == 1){ i * 100 + 50, 0);
return listActions.get(0);
} }
// TODO: 08/12/2021 implémenter choix voir Terminal pour savoir comment faire else if(action instanceof DropObject o){
}while(action == null); addButtonToPane(o.getDirection().toString(), new ClientEventHandler(new SelectDirectionListener(game, this, action)), buttonPane, i * 100 + 50, 0);
return action;
} }
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<ReunionSameAction> 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<ActionEvent> 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() { private Parent createContent() {
Pane principalPane = new Pane(); Pane principalPane = new Pane();
@ -111,13 +145,20 @@ public class Window extends AbstractView {
} }
putStatePlayerPane(principalPane); putStatePlayerPane(principalPane);
putMoveTextPane(principalPane); putMoveTextPane(principalPane);
Button buttonNext = new Button("SUIVANT"); this.buttonPane = new Pane();
buttonNext .setOnAction(new ClientEventHandler(new ButtonListener(game))); buttonPane.setLayoutX(0);
buttonNext .setLayoutX(700); buttonPane.setLayoutY(600);
buttonNext .setLayoutY(600); if(game.getCurrentPlayer() instanceof HumanPlayer) {
buttonNext .setStyle("-fx-background-color: #a96806;"); if(this.selectedReunionAction == null) {
buttonNext .setTextFill(javafx.scene.paint.Color.WHITE); choseReunionSameAction(player.generateAvailableActions());
principalPane.getChildren().add(buttonNext); } else {
choseDirectionAction(selectedReunionAction);
}
} else {
addButtonToPane("SUIVANT", new ClientEventHandler(nextPlayerButtonListener), buttonPane, (int) (principalPane.getPrefWidth() / 2), 0);
}
principalPane.getChildren().add(buttonPane);
principalPane.setBackground(bg); principalPane.setBackground(bg);
return principalPane; return principalPane;
} }
@ -172,8 +213,12 @@ public class Window extends AbstractView {
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();
String s = player + " : " + (player.getId()+1) + "\n" + String s = "";
if(game.getPreviousPlayer() != null) {
s = game.getPreviousPlayer() + " : " + (game.getPreviousPlayer().getId()+1) + "\n" +
"Vient de jouer : " + action + "\n"; "Vient de jouer : " + action + "\n";
}
Text t = new Text(s); Text t = new Text(s);
t.setFill(Color.WHITE); t.setFill(Color.WHITE);
Rectangle r = new Rectangle(); Rectangle r = new Rectangle();
@ -186,7 +231,11 @@ public class Window extends AbstractView {
return subSp; return subSp;
} }
public void setSelectedReunionAction(ReunionSameAction selectedReunionAction) {
this.selectedReunionAction = selectedReunionAction;
}
public NextPlayerButtonListener getNextPlayerButtonListener() {
return nextPlayerButtonListener;
}
} }

View File

@ -21,6 +21,7 @@ public class Game {
private Player currentPlayer; private Player currentPlayer;
private Action selectedAction = null; private Action selectedAction = null;
private int nbrTurn; private int nbrTurn;
private Player previousPlayer;
/** /**
* @param buildStrategy used to build a grid * @param buildStrategy used to build a grid
@ -37,6 +38,7 @@ public class Game {
this.players = players; this.players = players;
this.currentPlayer = players.get(0); this.currentPlayer = players.get(0);
this.nbrTurn = 1; this.nbrTurn = 1;
this.previousPlayer = null;
initGame(buildStrategy); initGame(buildStrategy);
} }
@ -61,6 +63,7 @@ public class Game {
selectedAction.doAction(); selectedAction.doAction();
countdownGridElementsUpdate(); countdownGridElementsUpdate();
gridPlayersUpdate(); gridPlayersUpdate();
previousPlayer = currentPlayer;
nextCurrentPlayer(); nextCurrentPlayer();
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
nbrTurn++; nbrTurn++;
@ -199,4 +202,8 @@ public class Game {
public int getNbrTurn() { public int getNbrTurn() {
return nbrTurn; return nbrTurn;
} }
public Player getPreviousPlayer() {
return previousPlayer;
}
} }