I'm trying to implement an AI that uses Minimax for the dots and boxes game (http://en.wikipedia.org/wiki/Dots_and_Boxes)
Here is what I have so far:
public Line makeMove(GameState gs) {
if (gs.getRemainingLines().size() == 1) {
return gs.getRemainingLines().get(0);
}
if (gs.getPlayer() == 1) {
int minscore = -1;
GameState g = gs.clone();
Line lnew = null;
List<Line> l = gs.getRemainingLines();
for (Line l2 : l) {
g.addLine(l2);
if (evaluate(g) > minscore) {
minscore = (evaluate(g));
lnew = l2;
}
}
return lnew;
} else {
int maxscore = 999;
GameState g = gs.clone();
Line lnew = null;
List<Line> l = gs.getRemainingLines();
for (Line l2 : l) {
g.addLine(l2);
if (evaluate(g) < maxscore) {
maxscore = (evaluate(g));
lnew = l2;
}
}
return lnew;
}
}
However, it keeps returning null and I don't think I'm implementing minimax correctly. Can anyone give me some pointers?
getRemainingLines() returns a list of moves that are still possible.
evaluate() returns an int for the score.