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.EventHandler;
public class ClientEventHandler implements EventHandler<ActionEvent> {
private final ModelListener listener;
public ClientEventHandler(ButtonListener listener) {
this.listener = listener;
}
public record ClientEventHandler(
ModelListener listener) implements EventHandler<ActionEvent> {
@Override
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 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é");

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.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");
}
@Override
/**
* Used when current player is an isntance of {@link fr.lnl.game.server.games.player.HumanPlayer}
* @return chosen action
*/
public Action choseAction() {
List<ReunionSameAction> actions = player.generateAvailableActions();
List<Action> listActions = choseReunionSameAction(actions).getActions();
@ -64,4 +67,22 @@ public class Terminal extends AbstractView {
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 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");
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"+

View File

@ -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.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
@ -36,6 +40,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) {
@ -63,20 +70,47 @@ public class Window extends AbstractView {
alert.showAndWait();
}
@Override
public Action choseAction() {
List<ReunionSameAction> actions = player.generateAvailableActions();
List<Action> 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<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() {
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())
@ -111,13 +145,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);
principalPane.setBackground(bg);
return principalPane;
}
@ -169,11 +210,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);
t.setFill(Color.WHITE);
Rectangle r = new Rectangle();
@ -186,7 +231,11 @@ public class Window extends AbstractView {
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 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;
}
}