Add basis strategy on StrategyComputerPlayer and implementation on this player in the game, view.

This commit is contained in:
Katchan 2021-12-08 18:56:26 +01:00
parent 4c52bedc9f
commit 37d03e570a
7 changed files with 150 additions and 26 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

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

@ -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(Point position){ public void setPosition(Point position){
if(position == null){ if(position == null){
@ -95,8 +116,11 @@ public abstract class AbstractPlayer implements Player {
@Override @Override
public void decrementEnergy(int energy){ public void decrementEnergy(int energy){
if(!isShieldDeploy()){
this.energy -= energy; this.energy -= energy;
} }
shieldDeploy = false;
}
@Override @Override
public void incrementEnergy(int energy){ public void incrementEnergy(int energy){

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;
public abstract class ComputerPlayer extends AbstractPlayer { public abstract class ComputerPlayer extends AbstractPlayer {
@ -9,5 +11,17 @@ public abstract class ComputerPlayer extends AbstractPlayer {
super(id, point, false, classPlayer); super(id, point, false, classPlayer);
} }
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

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