LOG320_Lab4/src/main/java/laboratoire4/strategies/Strategy.java
2023-04-11 23:18:34 -04:00

59 lines
1.8 KiB
Java

package laboratoire4.strategies;
import laboratoire4.Action;
import laboratoire4.IPawn;
import laboratoire4.game.Game;
import laboratoire4.pawns.PawnMovement;
import java.util.*;
import java.util.stream.Collectors;
public interface Strategy {
int WEIGHT_MAX = 10;
EvaluationResult getNextMove();
int getWeight();
static List<Action<IPawn>> getValidActions(Game game) {
List<IPawn> maxPawns = Arrays.stream(game.getBoard())
.flatMap(Arrays::stream)
.filter(Objects::nonNull)
.filter(p -> p.getPlayer() == game.getPlayer())
.collect(Collectors.toList());
return getValidActions(game.getBoard(), maxPawns);
}
static List<Action<IPawn>> getValidActions(IPawn[][] board, Collection<IPawn> pawns) {
return getValidActions(board, pawns, true);
}
static List<Action<IPawn>> getValidActions(IPawn[][] board, Collection<IPawn> pawns, boolean excludeDefense) {
List<Action<IPawn>> validActions = new ArrayList<>();
for (IPawn pawn : pawns) {
if (excludeDefense && pawn.getRow() == pawn.getPlayer().getHome()) {
int col = pawn.getCol();
// Si possible, on ne bouge pas ces pushers, comme ça on a une défense d'urgence totale
if (col == 1 || col == 2 || col == 5 || col == 6) {
continue;
}
}
for (PawnMovement movement : PawnMovement.values()) {
if (pawn.isMoveValid(board, movement)) {
validActions.add(new Action<>(pawn, movement));
}
}
}
if (excludeDefense && validActions.isEmpty()) {
return getValidActions(board, pawns, false);
}
return validActions;
}
}