Files
BUT3ProjetJeuGroupe/fr/iut_fbleau/Bot/DivineBot.java

152 lines
5.2 KiB
Java
Raw Normal View History

package fr.iut_fbleau.Bot;
2026-01-30 14:11:13 +01:00
import fr.iut_fbleau.Avalam.*;
import fr.iut_fbleau.GameAPI.*;
import java.util.*;
2026-01-30 14:11:13 +01:00
public class DivineBot extends AbstractGamePlayer {
2026-01-30 13:40:04 +01:00
private final Player me;
private final int maxDepth;
private final Random rng = new Random();
2026-01-30 13:40:04 +01:00
public DivineBot(Player p, int maxDepth) {
super(p);
this.me = p;
this.maxDepth = maxDepth;
2026-01-30 13:40:04 +01:00
}
@Override
public AbstractPly giveYourMove(IBoard board) {
if (board == null || board.isGameOver()) return null;
List<AbstractPly> moves = listMoves(board);
2026-02-05 22:23:07 +01:00
int bestValue = Integer.MIN_VALUE;
2026-01-30 13:40:04 +01:00
List<AbstractPly> bestMoves = new ArrayList<>();
for (AbstractPly m : moves) {
IBoard next = board.safeCopy();
next.doPly(m);
2026-02-05 23:12:03 +01:00
// On regarde l'avenir
2026-02-05 22:23:07 +01:00
int value = alphaBeta(next, maxDepth - 1, Integer.MIN_VALUE, Integer.MAX_VALUE);
if (value > bestValue) {
bestValue = value;
bestMoves.clear();
bestMoves.add(m);
} else if (value == bestValue) {
bestMoves.add(m);
2026-01-30 13:40:04 +01:00
}
}
return bestMoves.get(rng.nextInt(bestMoves.size()));
}
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
2026-02-05 22:23:07 +01:00
if (board.isGameOver()) {
Result r = board.getResult();
if (r == Result.DRAW) return 0;
2026-02-05 23:12:03 +01:00
// On vérifie strictement si MON joueur a gagné
boolean winP1 = (r == Result.WIN);
2026-02-05 22:23:07 +01:00
boolean amIP1 = (me == Player.PLAYER1);
2026-02-05 23:12:03 +01:00
return (winP1 == amIP1) ? 1000000 : -1000000;
2026-02-05 22:23:07 +01:00
}
2026-01-30 14:11:13 +01:00
if (depth == 0) return evaluate(board);
2026-01-30 13:40:04 +01:00
2026-02-05 22:23:07 +01:00
boolean isMax = (board.getCurrentPlayer() == me);
int best = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
2026-01-30 13:40:04 +01:00
2026-02-05 22:23:07 +01:00
for (AbstractPly m : listMoves(board)) {
IBoard next = board.safeCopy();
next.doPly(m);
int val = alphaBeta(next, depth - 1, alpha, beta);
2026-02-05 22:23:07 +01:00
if (isMax) {
best = Math.max(best, val);
alpha = Math.max(alpha, best);
2026-02-05 22:23:07 +01:00
} else {
best = Math.min(best, val);
beta = Math.min(beta, best);
2026-01-30 13:40:04 +01:00
}
2026-02-05 22:23:07 +01:00
if (alpha >= beta) break;
2026-01-30 13:40:04 +01:00
}
2026-02-05 22:23:07 +01:00
return best;
2026-01-30 14:11:13 +01:00
}
2026-01-30 13:40:04 +01:00
private int evaluate(IBoard board) {
if (!(board instanceof AvalamBoard)) return 0;
AvalamBoard b = (AvalamBoard) board;
2026-02-05 23:12:03 +01:00
// On définit clairement les couleurs
2026-01-30 13:40:04 +01:00
Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
2026-02-05 23:12:03 +01:00
Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW;
2026-02-05 22:23:07 +01:00
2026-01-30 13:40:04 +01:00
int score = 0;
for (int r = 0; r < AvalamBoard.SIZE; r++) {
for (int c = 0; c < AvalamBoard.SIZE; c++) {
Tower t = b.getTowerAt(r, c);
2026-02-05 22:23:07 +01:00
if (t == null || t.getHeight() == 0) continue;
2026-01-30 13:40:04 +01:00
2026-02-05 23:12:03 +01:00
int h = t.getHeight();
int weight = 0;
// Étape 1 : Est-ce que cette tour est "finie" ou isolée ?
if (h == 5 || isIsolated(b, r, c)) {
weight = 1000; // Point sécurisé
2026-02-05 22:23:07 +01:00
} else {
2026-02-05 23:12:03 +01:00
// Étape 2 : Si la tour est prenable par l'ennemi
if (isVulnerable(b, r, c, oppColor)) {
// Si c'est ma tour, c'est un danger (-200)
// Si c'est la sienne, c'est une opportunité (+50)
weight = -200;
} else {
// Étape 3 : Barème de progression sécurisée
switch (h) {
case 4: weight = 400; break;
case 3: weight = 150; break;
case 2: weight = 60; break;
default: weight = 10; break;
}
}
2026-02-05 22:23:07 +01:00
}
2026-02-05 23:12:03 +01:00
if (t.getColor() == myColor) score += weight;
else score -= weight;
2026-01-30 13:40:04 +01:00
}
}
return score;
}
2026-02-05 23:12:03 +01:00
private boolean isIsolated(AvalamBoard b, int r, int c) {
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if (dr == 0 && dc == 0) continue;
Tower n = b.getTowerAt(r + dr, c + dc);
if (n != null && n.getHeight() > 0) return false;
}
}
return true;
}
private boolean isVulnerable(AvalamBoard b, int r, int c, Color enemyColor) {
int myHeight = b.getTowerAt(r, c).getHeight();
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if (dr == 0 && dc == 0) continue;
Tower n = b.getTowerAt(r + dr, c + dc);
// Si un ennemi peut sauter dessus pour faire une tour <= 5
if (n != null && n.getColor() == enemyColor && (n.getHeight() + myHeight <= 5)) {
return true;
}
}
}
return false;
}
2026-01-30 13:40:04 +01:00
private List<AbstractPly> listMoves(IBoard board) {
List<AbstractPly> moves = new ArrayList<>();
2026-02-05 22:23:07 +01:00
Iterator<AbstractPly> it = board.iterator();
2026-02-05 23:12:03 +01:00
while (it.hasNext()) moves.add(it.next());
2026-01-30 13:40:04 +01:00
return moves;
}
2026-01-30 14:11:13 +01:00
}