heuristic + arena + alphabeta
This commit is contained in:
48
javaAPI/fr/iut_fbleau/HexGame/HeuristicBot.java
Normal file
48
javaAPI/fr/iut_fbleau/HexGame/HeuristicBot.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package fr.iut_fbleau.HexGame;
|
||||
|
||||
import fr.iut_fbleau.GameAPI.*;
|
||||
|
||||
public class HeuristicBot extends AbstractGamePlayer {
|
||||
|
||||
public HeuristicBot(Player me) {
|
||||
super(me); // Correct constructor usage
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractPly giveYourMove(IBoard board) {
|
||||
HexBoard hb = (HexBoard) board;
|
||||
float bestScore = -Float.MAX_VALUE;
|
||||
HexPly bestMove = null;
|
||||
|
||||
for (int i = 0; i < hb.getSize(); i++) {
|
||||
for (int j = 0; j < hb.getSize(); j++) {
|
||||
HexPly move = new HexPly(hb.getCurrentPlayer(), i, j);
|
||||
if (hb.isLegal(move)) {
|
||||
hb.doPly(move);
|
||||
float score = evaluateBoard(hb);
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestMove = move;
|
||||
}
|
||||
hb.undoPly();
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private float evaluateBoard(HexBoard board) {
|
||||
int size = board.getSize();
|
||||
int center = size / 2;
|
||||
float score = 0;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = 0; j < size; j++) {
|
||||
if (board.getPlayerAt(i, j) == Player.PLAYER1) {
|
||||
score += Math.abs(i - center) + Math.abs(j - center); // Distance from center
|
||||
}
|
||||
}
|
||||
}
|
||||
return score;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user