Rework ChoseAction when currentPlayer is Human(now respect M-VC(terminal) model), but not implemented with Window

This commit is contained in:
Quentin Legot 2021-12-08 11:58:40 +01:00
parent 74b8489854
commit 609966d9af
9 changed files with 127 additions and 76 deletions

View File

@ -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<ReunionSameAction> generateAvailableActions() {
List<ReunionSameAction> 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<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

@ -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<ReunionSameAction> actions = generateAvailableActions();
List<Action> 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;
}
}

View File

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

View File

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

View File

@ -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<ReunionSameAction> actions = generateAvailableActions();
List<Action> 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())

View File

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

View File

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

View File

@ -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<ReunionSameAction> 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<Action> 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;
}
}

View File

@ -8,8 +8,6 @@ import java.util.List;
public interface Player {
Action choseAction();
Point getPosition();
boolean isAlive();