50 lines
1.4 KiB
Java
50 lines
1.4 KiB
Java
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;
|
|
//HexBoard simBoard = (HexBoard) board.safeCopy();
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
for (int j = 0; j < size; j++) {
|
|
if (board.getCellPlayer(i, j) == Player.PLAYER1) {
|
|
score += Math.abs(i - center) + Math.abs(j - center); // Distance from center
|
|
}
|
|
}
|
|
}
|
|
return score;
|
|
}
|
|
}
|