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

72 lines
1.7 KiB
Java

package laboratoire4.strategies;
import laboratoire4.game.Game;
public class MasterStrategy implements Strategy {
private static Strategy instance;
public static Strategy getInstance(Game game) {
if (instance == null) {
instance = new MasterStrategy(game);
}
return instance;
}
private final Game game;
private MasterStrategy(Game game) {
this.game = game;
}
@Override
public EvaluationResult getNextMove() {
long startMs = System.currentTimeMillis();
Strategy strategy = getBestStrategy();
System.out.println(strategy.getClass().getSimpleName());
EvaluationResult result = strategy.getNextMove();
long endMs = System.currentTimeMillis();
System.out.printf("Temps: %d ms\n", endMs - startMs);
return result;
}
private Strategy getBestStrategy() {
Strategy[] strategies = new Strategy[]{
new ImmediateDefenseStrategy(game),
new WinningStrategy(game),
new DefenseStrategy(game),
new AttackStrategy(game)
};
int maxWeight = 0;
Strategy bestStrategy = null;
for (Strategy strategy : strategies) {
int weight = strategy.getWeight();
if (weight == WEIGHT_MAX) {
return strategy;
}
if (weight > maxWeight) {
maxWeight = weight;
bestStrategy = strategy;
}
}
if (maxWeight > 0) {
return bestStrategy;
}
return new RandomStrategy(game);
}
@Override
public int getWeight() {
return WEIGHT_MAX;
}
}