Merge branch 'master' into conflit-2

This commit is contained in:
Quentin Legot 2021-12-08 19:11:18 +01:00 committed by GitHub
commit 3e2252a5ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 213 additions and 49 deletions

View File

@ -106,6 +106,15 @@ public class App extends Application {
} }
playerClass = RandomComputerPlayer.class; playerClass = RandomComputerPlayer.class;
} }
case "computerS" -> {
if(playerClass != null) {
playerList.add(createNewPlayer(playerClass,
classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size())
);
classPlayer = null;
}
playerClass = StrategyComputerPlayer.class;
}
case "default" -> classPlayer = ClassPlayer.DEFAULT; case "default" -> classPlayer = ClassPlayer.DEFAULT;
case "tank" -> classPlayer = ClassPlayer.TANK; case "tank" -> classPlayer = ClassPlayer.TANK;
case "dps" -> classPlayer = ClassPlayer.DPS; case "dps" -> classPlayer = ClassPlayer.DPS;
@ -113,6 +122,7 @@ public class App extends Application {
default -> throw new IllegalArgumentException("Unknown argument: " + str); default -> throw new IllegalArgumentException("Unknown argument: " + str);
} }
} }
System.out.println("oui");
if(playerClass != null) if(playerClass != null)
playerList.add(createNewPlayer(playerClass, playerList.add(createNewPlayer(playerClass,
classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size())

View File

@ -1,12 +1,9 @@
package fr.lnl.game.client.view; package fr.lnl.game.client.view;
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.ReunionSameAction; import fr.lnl.game.server.games.action.ReunionSameAction;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Maths; import fr.lnl.game.server.utils.Maths;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
@ -20,23 +17,6 @@ public abstract class AbstractView implements View {
this.player = player; 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) { protected ReunionSameAction choseReunionSameAction(List<ReunionSameAction> actions) {
ReunionSameAction reunion = null; ReunionSameAction reunion = null;
String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size(); String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size();

View File

@ -17,6 +17,7 @@ import javafx.scene.shape.Rectangle;
public class Cell extends Rectangle { public class Cell extends Rectangle {
private static final Image PLAYER_IMAGE = new Image("player.png"); private static final Image PLAYER_IMAGE = new Image("player.png");
private static final Image PLAYER_SHIELD_IMAGE = new Image("player_shield.png");
private static final Image ENERGY_BALL_IMAGE = new Image("energyBall.png"); private static final Image ENERGY_BALL_IMAGE = new Image("energyBall.png");
private static final Image BOMB_IMAGE = new Image("bomb.png"); private static final Image BOMB_IMAGE = new Image("bomb.png");
private static final Image MINE_IMAGE = new Image("mine.png"); private static final Image MINE_IMAGE = new Image("mine.png");
@ -36,7 +37,12 @@ public class Cell extends Rectangle {
StackPane sp = new StackPane(); StackPane sp = new StackPane();
Image in; Image in;
if(object instanceof Player){ if(object instanceof Player){
in = PLAYER_IMAGE; if(((Player) object).isShieldDeploy()){
in = PLAYER_SHIELD_IMAGE;
}
else{
in = PLAYER_IMAGE;
}
} else if(object instanceof EnergyBall){ } else if(object instanceof EnergyBall){
in = ENERGY_BALL_IMAGE; in = ENERGY_BALL_IMAGE;
} else if(object instanceof Bomb){ } else if(object instanceof Bomb){

View File

@ -24,12 +24,12 @@ public class Terminal extends AbstractView {
@Override @Override
public void displayWinner(Player winner) { public void displayWinner(Player winner) {
System.out.println("Le joueur " + winner + " a gagné la partie"); System.out.println(winner + " " + winner.getId() + " a gagné la partie");
} }
@Override @Override
public Action choseAction() { public Action choseAction() {
List<ReunionSameAction> actions = generateAvailableActions(); List<ReunionSameAction> actions = player.generateAvailableActions();
List<Action> listActions = choseReunionSameAction(actions).getActions(); List<Action> listActions = choseReunionSameAction(actions).getActions();
Action action = null; Action action = null;
String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + listActions.size(); String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + listActions.size();

View File

@ -27,7 +27,7 @@ public record ViewManager(
} }
boolean isOver = game.play(); boolean isOver = game.play();
System.out.println("Le joueur ordinateur numéro " + player.getId() + " a joué"); System.out.println("Le joueur ordinateur numéro " + player.getId() + " a joué");
System.out.println("Il a joué l'action: " + game.getSelectedAction()); System.out.println("Il a joué l'action: " + game.getSelectedAction().getClass().getSimpleName());
if (isOver) { if (isOver) {
displayWinnerEvent.updateModel(game.getWinner()); displayWinnerEvent.updateModel(game.getWinner());
System.exit(0); System.exit(0);

View File

@ -62,7 +62,7 @@ public class Window extends AbstractView {
@Override @Override
public Action choseAction() { public Action choseAction() {
List<ReunionSameAction> actions = generateAvailableActions(); List<ReunionSameAction> actions = player.generateAvailableActions();
List<Action> listActions = choseReunionSameAction(actions).getActions(); List<Action> listActions = choseReunionSameAction(actions).getActions();
Action action = null; Action action = null;
do { do {
@ -125,7 +125,7 @@ public class Window extends AbstractView {
public void putStatePlayerPane(Pane principalPane){ public void putStatePlayerPane(Pane principalPane){
int Y = 0; int Y = 0;
for(int i=0;i<game.getPlayers().size();i++){ for(int i=0;i<game.getPlayers().size();i++){
StackPane sp = showStatePlayer(i); StackPane sp = showStatePlayer(game.getPlayers().get(i).toString(),i);
sp.setLayoutX(480); sp.setLayoutX(480);
sp.setLayoutY(Y); sp.setLayoutY(Y);
Y+=90; Y+=90;
@ -133,9 +133,9 @@ public class Window extends AbstractView {
} }
} }
public StackPane showStatePlayer(int playerNumber){ public StackPane showStatePlayer(String type, int playerNumber){
StackPane subSp = new StackPane(); StackPane subSp = new StackPane();
String s = "Joueur " + (playerNumber+1) + "\n" + String s = type + " " + (playerNumber+1) + "\n" +
"Energie : " + game.getPlayers().get(playerNumber).getEnergy() + "\n" + "Energie : " + game.getPlayers().get(playerNumber).getEnergy() + "\n" +
"Arme : " + game.getPlayers().get(playerNumber).getWeapon().getClass().getSimpleName() + "\n"; "Arme : " + game.getPlayers().get(playerNumber).getWeapon().getClass().getSimpleName() + "\n";
Text t = new Text(s); Text t = new Text(s);
@ -161,8 +161,9 @@ public class Window extends AbstractView {
public StackPane showMoveText(){ public StackPane showMoveText(){
StackPane subSp = new StackPane(); StackPane subSp = new StackPane();
String s = "Joueur : " + (player.getId()+1) + "\n" + String action = game.getSelectedAction() == null ? "null" : game.getSelectedAction().getClass().getSimpleName();
"Vient de jouer : " + game.getSelectedAction() + "\n"; String s = player + " : " + (player.getId()+1) + "\n" +
"Vient de jouer : " + action + "\n";
Text t = new Text(s); Text t = new Text(s);
Rectangle r = new Rectangle(); Rectangle r = new Rectangle();
r.setWidth(478); r.setWidth(478);

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

View File

@ -47,7 +47,7 @@ public class Game {
public boolean play() { public boolean play() {
if(currentPlayer instanceof ComputerPlayer computer) if(currentPlayer instanceof ComputerPlayer computer)
// si le joueur est humain alors le choix se fait avant l'appel de play() // si le joueur est humain alors le choix se fait avant l'appel de play()
selectedAction = computer.choseAction(); selectedAction = computer.choseAction(this);
selectedAction.doAction(); selectedAction.doAction();
countdownGridElementsUpdate(); countdownGridElementsUpdate();
gridPlayersUpdate(); gridPlayersUpdate();

View File

@ -22,6 +22,7 @@ public interface Action {
* Used by {@link Move}, {@link Shot} and {@link DropObject} to list all direction where the action is possible * Used by {@link Move}, {@link Shot} and {@link DropObject} to list all direction where the action is possible
* @return a list a point where the action is possible (not block by a wall per example) * @return a list a point where the action is possible (not block by a wall per example)
*/ */
Point getPoint();
List<Point> getValidPoint(); List<Point> getValidPoint();
} }

View File

@ -32,6 +32,11 @@ public class DeployShield extends AbstractAction {
return true; return true;
} }
@Override
public Point getPoint() {
return null;
}
@Override @Override
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
return null; return null;

View File

@ -71,4 +71,8 @@ public abstract class DropObject extends AbstractAction {
return direction; return direction;
} }
@Override
public Point getPoint() {
return point;
}
} }

View File

@ -60,6 +60,7 @@ public class Move extends AbstractAction {
* We add a point to the list where there is nothing on the board. * We add a point to the list where there is nothing on the board.
* @see Action#getValidPoint() * @see Action#getValidPoint()
*/ */
@Override @Override
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
List<Point> listMoves = new ArrayList<>(); List<Point> listMoves = new ArrayList<>();
@ -81,6 +82,11 @@ public class Move extends AbstractAction {
return listMoves; return listMoves;
} }
@Override
public Point getPoint() {
return point;
}
public Direction getDirection() { public Direction getDirection() {
return direction; return direction;
} }

View File

@ -29,6 +29,11 @@ public class Nothing extends AbstractAction {
return true; return true;
} }
@Override
public Point getPoint() {
return null;
}
@Override @Override
public List<Point> getValidPoint() { public List<Point> getValidPoint() {
return null; return null;

View File

@ -1,7 +1,6 @@
package fr.lnl.game.server.games.action; package fr.lnl.game.server.games.action;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**

View File

@ -90,7 +90,7 @@ public class Grid {
str.append(" \033[0;34m").append(value.getA().getId()).append("\033[0m"); str.append(" \033[0;34m").append(value.getA().getId()).append("\033[0m");
} }
else if (value.getB() instanceof Wall) { else if (value.getB() instanceof Wall) {
str.append(" \033[0;32m\033[0m"); str.append(" \033[0;32m#\033[0m");
} }
else if(value.getB() instanceof EnergyBall){ else if(value.getB() instanceof EnergyBall){
str.append(" \033[0;31mE\033[0m"); str.append(" \033[0;31mE\033[0m");
@ -119,7 +119,7 @@ public class Grid {
str.append(" \033[0;34m").append(value.getA().getId()).append("\033[0m"); str.append(" \033[0;34m").append(value.getA().getId()).append("\033[0m");
} }
else if (value.getB() instanceof Wall) { else if (value.getB() instanceof Wall) {
str.append(" \033[0;32m\033[0m"); str.append(" \033[0;32m#\033[0m");
} }
else if(value.getB() instanceof EnergyBall){ else if(value.getB() instanceof EnergyBall){
str.append(" \033[0;31mE\033[0m"); str.append(" \033[0;31mE\033[0m");
@ -149,6 +149,14 @@ public class Grid {
return board; return board;
} }
public Player getGridPlayer(Point point){
return getBoard().get(point).getA();
}
public Box getGridBox(Point point){
return getBoard().get(point).getB();
}
public List<Player> getPlayers() { public List<Player> getPlayers() {
return players; return players;
} }

View File

@ -1,9 +1,11 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.ReunionSameAction;
import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class AbstractPlayer implements Player { public abstract class AbstractPlayer implements Player {
@ -25,6 +27,24 @@ public abstract class AbstractPlayer implements Player {
this.position = position; this.position = position;
} }
@Override
public List<ReunionSameAction> generateAvailableActions() {
List<ReunionSameAction> 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));
}
}
return actions;
}
@Override @Override
public boolean isAlive(){ public boolean isAlive(){
return energy > 0; return energy > 0;
@ -85,6 +105,7 @@ public abstract class AbstractPlayer implements Player {
return position; return position;
} }
@Override @Override
public void setPosition(/* NotNull */ Point position){ public void setPosition(/* NotNull */ Point position){
if(position == null){ if(position == null){
@ -95,7 +116,10 @@ public abstract class AbstractPlayer implements Player {
@Override @Override
public void decrementEnergy(int energy){ public void decrementEnergy(int energy){
this.energy -= energy; if(!isShieldDeploy()){
this.energy -= energy;
}
shieldDeploy = false;
} }
@Override @Override

View File

@ -1,6 +1,8 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.Nothing;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
/** /**
@ -16,5 +18,17 @@ public abstract class ComputerPlayer extends AbstractPlayer {
* Call when an AI need to choose an action to execute * Call when an AI need to choose an action to execute
* @return the chosen action * @return the chosen action
*/ */
public abstract Action choseAction(); public Action choseAction(Game game){
Action action;
switch (getActions().size()){
case 0 -> action = new Nothing();
case 1 -> action = getActions().get(0);
default -> {
return strategy(game);
}
}
return action;
}
public abstract Action strategy(Game game);
} }

View File

@ -14,4 +14,8 @@ public class HumanPlayer extends AbstractPlayer {
} }
@Override
public String toString() {
return "Human";
}
} }

View File

@ -1,6 +1,7 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.ReunionSameAction;
import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
@ -8,6 +9,8 @@ import java.util.List;
public interface Player { public interface Player {
List<ReunionSameAction> generateAvailableActions();
Point getPosition(); Point getPosition();
boolean isAlive(); boolean isAlive();

View File

@ -1,7 +1,9 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.Nothing; import fr.lnl.game.server.games.action.Nothing;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.Random; import java.util.Random;
@ -17,18 +19,17 @@ public class RandomComputerPlayer extends ComputerPlayer {
* @return an action between all available * @return an action between all available
*/ */
@Override @Override
public Action choseAction() { public Action strategy(Game game) {
Action action = null; Action action = null;
switch (getActions().size()){ Random random = new Random();
case 0 -> action = new Nothing(); while (action == null || !action.isPossible()) {
case 1 -> action = getActions().get(0); action = getActions().get(random.nextInt(0, getActions().size()));
default -> {
Random random = new Random();
while (action == null || !action.isPossible()) {
action = getActions().get(random.nextInt(0,getActions().size()));
}
}
} }
return action; return action;
} }
@Override
public String toString() {
return "Random";
}
} }

View File

@ -1,8 +1,16 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.elements.Box;
import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.elements.Explosive;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.List;
import java.util.Random;
public class StrategyComputerPlayer extends ComputerPlayer { public class StrategyComputerPlayer extends ComputerPlayer {
public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
@ -10,7 +18,92 @@ public class StrategyComputerPlayer extends ComputerPlayer {
} }
@Override @Override
public Action choseAction() { public Action strategy(Game game) {
return null; Action action = null;
Grid grid = game.getGrid();
for (Player player : game.getPlayers()) {
boolean danger = player.getActions().stream().anyMatch(a -> a instanceof Shot && a.getPoint().equals(getPosition()));
if(danger && (getEnergy() - getClassPlayer().getPenaltyShoot() <= 0)){
action = new DeployShield(this);
}
}
List<ReunionSameAction> actions = generateAvailableActions();
if(isInReunion(actions, Shot.class)){
ReunionSameAction reunion = extractReunionSameAction(actions, Shot.class);
List<Action> actionList = reunion.getActions();
action = actionList.get(0);
if(actionList.size() > 1){
for (int i = 1; i < actionList.size(); i++) {
Point point = actionList.get(i).getPoint();
if(grid.getGridPlayer(point).getEnergy() < grid.getGridPlayer(action.getPoint()).getEnergy()){
action = actionList.get(i);
}
}
}
return action;
}
if(isInReunion(actions, Move.class)){
ReunionSameAction reunion = extractReunionSameAction(actions, Move.class);
List<Action> actionList = reunion.getActions();
for (Action value : actionList) {
Point point = value.getPoint();
Box box = grid.getGridBox(point);
if(box instanceof EnergyBall){
return value;
}
System.out.println("after move " + action);
}
Random random = new Random();
int value = random.nextInt(0,2);
System.out.println(value);
if(value == 0){
System.out.println("oui");
do{
action = actionList.get(random.nextInt(0, actionList.size()));
Box box = game.getGrid().getGridBox(action.getPoint());
if(box instanceof Explosive) {
if (!((Explosive) box).getPlayer().equals(this)) {
action = null;
}
}
}while(action == null);
return action;
}
if(isInReunion(actions, Explosive.class)){
List<Action> explosiveActions = extractReunionSameAction(actions, Move.class).getActions();
if(explosiveActions.size() > 1){
action = explosiveActions.get(random.nextInt(0, explosiveActions.size()));
return action;
}
System.out.println("explo " + action);
}
}
if(isInReunion(actions, Explosive.class)){
Random random = new Random();
List<Action> explosiveActions = extractReunionSameAction(actions, Move.class).getActions();
if(explosiveActions.size() > 1){
action = explosiveActions.get(random.nextInt(0, explosiveActions.size()));
}
return action;
}
else {
action = new Nothing();
System.out.println("nothing " + action);
}
System.out.println("end " + action);
return action;
}
public boolean isInReunion(List<ReunionSameAction> actions, Class clazz){
return actions.stream().anyMatch(r -> r.getActionName().equals(clazz.getSimpleName()));
}
public ReunionSameAction extractReunionSameAction(List<ReunionSameAction> actions, Class clazz){
return actions.stream().filter(r -> r.getActionName().equals(clazz.getSimpleName())).findFirst().get();
}
@Override
public String toString() {
return "AI";
} }
} }