Improve StrategyComputerPlayer

This commit is contained in:
Katchan 2021-12-08 21:46:37 +01:00
parent 37d03e570a
commit 417d311435
2 changed files with 78 additions and 45 deletions

View File

@ -29,6 +29,13 @@ public class Shot extends AbstractAction {
this.direction = direction; this.direction = direction;
} }
public Shot(Game game, Player player){
super(game, player);
List<Point> points = getValidPoint();
this.point = null;
this.direction = null;
}
/** /**
* We decrement player's energy and shot on every of its opponents on the chosen direction by decrementing its * We decrement player's energy and shot on every of its opponents on the chosen direction by decrementing its
* energy too * energy too

View File

@ -10,6 +10,7 @@ import fr.lnl.game.server.utils.Point;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.stream.Collectors;
public class StrategyComputerPlayer extends ComputerPlayer { public class StrategyComputerPlayer extends ComputerPlayer {
@ -19,79 +20,104 @@ public class StrategyComputerPlayer extends ComputerPlayer {
@Override @Override
public Action strategy(Game game) { public Action strategy(Game game) {
Action action = null; Action deployShield = choseDeployShield(game);
Grid grid = game.getGrid(); if(deployShield != null){
for (Player player : game.getPlayers()) { return deployShield;
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(); List<ReunionSameAction> actions = generateAvailableActions();
if(isInReunion(actions, Shot.class)){ Action shot = choseShot(actions,game);
if(shot != null){
return shot;
}
Action move = choseMove(actions,game);
if(move != null){
return move;
}
return choseExplosive(actions);
}
private Action choseExplosive(List<ReunionSameAction> actions){
Random random = new Random();
if(isInReunion(actions, DropBomb.class) || isInReunion(actions, DropMine.class)){
List<Action> explosiveActions = extractReunionSameAction(actions, DropMine.class).getActions();
explosiveActions.addAll(extractReunionSameAction(actions, DropBomb.class).getActions());
return explosiveActions.get(random.nextInt(0, explosiveActions.size()));
}
else{
return new Nothing();
}
}
private Action choseDeployShield(Game game){
for (Player player : game.getPlayers()) {
List<Point> shot = new Shot(game,player).getValidPoint();
boolean danger = shot.stream().anyMatch(p -> p.equals(getPosition()));
if(danger && (getEnergy() - getClassPlayer().getPenaltyShoot() <= 0)){
return new DeployShield(this);
}
}
return null;
}
private Action choseShot(List<ReunionSameAction> actions, Game game){
if(isInReunion(actions, Shot.class)) {
ReunionSameAction reunion = extractReunionSameAction(actions, Shot.class); ReunionSameAction reunion = extractReunionSameAction(actions, Shot.class);
List<Action> actionList = reunion.getActions(); List<Action> actionList = reunion.getActions();
action = actionList.get(0); Action action = actionList.get(0);
if(actionList.size() > 1){ if (actionList.size() > 1) {
for (int i = 1; i < actionList.size(); i++) { for (int i = 1; i < actionList.size(); i++) {
Point point = actionList.get(i).getPoint(); Point point = actionList.get(i).getPoint();
if(grid.getGridPlayer(point).getEnergy() < grid.getGridPlayer(action.getPoint()).getEnergy()){ if (game.getGrid().getGridPlayer(point).getEnergy() < game.getGrid().getGridPlayer(action.getPoint()).getEnergy()) {
action = actionList.get(i); action = actionList.get(i);
} }
} }
} }
return action; return action;
} }
if(isInReunion(actions, Move.class)){ return null;
}
private Action choseMove(List<ReunionSameAction> actions, Game game){
if(isInReunion(actions, Move.class)) {
ReunionSameAction reunion = extractReunionSameAction(actions, Move.class); ReunionSameAction reunion = extractReunionSameAction(actions, Move.class);
List<Action> actionList = reunion.getActions(); List<Action> actionList = reunion.getActions();
for (Action value : actionList) { for (Action value : actionList) {
Point point = value.getPoint(); Point point = value.getPoint();
Box box = grid.getGridBox(point); Box box = game.getGrid().getGridBox(point);
if(box instanceof EnergyBall){ if (box instanceof EnergyBall) {
return value; return value;
} }
System.out.println("after move " + action);
} }
Random random = new Random(); Random random = new Random();
int value = random.nextInt(0,2); int value = random.nextInt(0, 2);
System.out.println(value); if (value == 0) {
if(value == 0){ Action action = null;
System.out.println("oui"); do {
do{ Action move = actionList.get(random.nextInt(0, actionList.size()));
action = actionList.get(random.nextInt(0, actionList.size())); Box box = game.getGrid().getGridBox(move.getPoint());
Box box = game.getGrid().getGridBox(action.getPoint()); if (box instanceof Explosive) {
if(box instanceof Explosive) { if (!(((Explosive) box).getPlayer().equals(this))) {
if (!((Explosive) box).getPlayer().equals(this)) { action = move;
action = null; }
else{
if(actionList.stream().filter(a -> game.getGrid().getGridBox(a.getPoint()) instanceof Explosive).toList().size() ==
actionList.size()){
action = move;
} }
} }
}while(action == null); }
else{
action = move;
}
} while (action == null);
return action; return action;
} }
if(isInReunion(actions, Explosive.class)){ else{
List<Action> explosiveActions = extractReunionSameAction(actions, Move.class).getActions(); return choseExplosive(actions);
if(explosiveActions.size() > 1){
action = explosiveActions.get(random.nextInt(0, explosiveActions.size()));
return action;
}
System.out.println("explo " + action);
} }
} }
if(isInReunion(actions, Explosive.class)){ return null;
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){ public boolean isInReunion(List<ReunionSameAction> actions, Class clazz){