152 lines
5.2 KiB
Java
152 lines
5.2 KiB
Java
package fr.iut_fbleau.Bot;
|
|
|
|
import fr.iut_fbleau.Avalam.*;
|
|
import fr.iut_fbleau.GameAPI.*;
|
|
import java.util.*;
|
|
|
|
public class DivineBot extends AbstractGamePlayer {
|
|
|
|
private final Player me;
|
|
private final int maxDepth;
|
|
private final Random rng = new Random();
|
|
|
|
public DivineBot(Player p, int maxDepth) {
|
|
super(p);
|
|
this.me = p;
|
|
this.maxDepth = maxDepth;
|
|
}
|
|
|
|
@Override
|
|
public AbstractPly giveYourMove(IBoard board) {
|
|
if (board == null || board.isGameOver()) return null;
|
|
|
|
List<AbstractPly> moves = listMoves(board);
|
|
int bestValue = Integer.MIN_VALUE;
|
|
List<AbstractPly> bestMoves = new ArrayList<>();
|
|
|
|
for (AbstractPly m : moves) {
|
|
IBoard next = board.safeCopy();
|
|
next.doPly(m);
|
|
// On regarde l'avenir
|
|
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);
|
|
}
|
|
}
|
|
return bestMoves.get(rng.nextInt(bestMoves.size()));
|
|
}
|
|
|
|
private int alphaBeta(IBoard board, int depth, int alpha, int beta) {
|
|
if (board.isGameOver()) {
|
|
Result r = board.getResult();
|
|
if (r == Result.DRAW) return 0;
|
|
// On vérifie strictement si MON joueur a gagné
|
|
boolean winP1 = (r == Result.WIN);
|
|
boolean amIP1 = (me == Player.PLAYER1);
|
|
return (winP1 == amIP1) ? 1000000 : -1000000;
|
|
}
|
|
|
|
if (depth == 0) return evaluate(board);
|
|
|
|
boolean isMax = (board.getCurrentPlayer() == me);
|
|
int best = isMax ? Integer.MIN_VALUE : Integer.MAX_VALUE;
|
|
|
|
for (AbstractPly m : listMoves(board)) {
|
|
IBoard next = board.safeCopy();
|
|
next.doPly(m);
|
|
int val = alphaBeta(next, depth - 1, alpha, beta);
|
|
|
|
if (isMax) {
|
|
best = Math.max(best, val);
|
|
alpha = Math.max(alpha, best);
|
|
} else {
|
|
best = Math.min(best, val);
|
|
beta = Math.min(beta, best);
|
|
}
|
|
if (alpha >= beta) break;
|
|
}
|
|
return best;
|
|
}
|
|
|
|
private int evaluate(IBoard board) {
|
|
if (!(board instanceof AvalamBoard)) return 0;
|
|
AvalamBoard b = (AvalamBoard) board;
|
|
|
|
// On définit clairement les couleurs
|
|
Color myColor = (me == Player.PLAYER1) ? Color.YELLOW : Color.RED;
|
|
Color oppColor = (myColor == Color.YELLOW) ? Color.RED : Color.YELLOW;
|
|
|
|
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);
|
|
if (t == null || t.getHeight() == 0) continue;
|
|
|
|
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é
|
|
} else {
|
|
// É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;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (t.getColor() == myColor) score += weight;
|
|
else score -= weight;
|
|
}
|
|
}
|
|
return score;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private List<AbstractPly> listMoves(IBoard board) {
|
|
List<AbstractPly> moves = new ArrayList<>();
|
|
Iterator<AbstractPly> it = board.iterator();
|
|
while (it.hasNext()) moves.add(it.next());
|
|
return moves;
|
|
}
|
|
} |