LOG320_Lab4/src/main/java/laboratoire4/strategies/MasterStrategy.java
2023-04-11 14:11:19 -04:00

69 lines
1.6 KiB
Java

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