Hi there, Im currently designing a Dots and Boxes Game and ive run into a snag,where my Minimax Algorithm will go 4-1 against the AI but not make the next move to win.Any help with this would be great
package players;
import interfaces.GameState;
import java.util.List;
import data.Line;
public class MiniMax extends AbstractPlayer {
public Line makeMove(GameState gs){
if (gs.getRemainingLines().size() == 2) {
}
if(gs.getPlayer() ==1){
int minscore = -1;
Line lnew1 = null;
List<Line> l =gs.getRemainingLines();
for(Line l4: l){
GameState g = gs.clone();
g.addLine(l4);
if (evaluate(g)> minscore){
minscore = (evaluate(g));
lnew1 = l4;
}
}
return lnew1;
}else{
int maxscore = 0;
Line lnew = null;
List<Line> L = gs.getRemainingLines();
for (Line l2 : L) {
GameState g1 = gs.clone();
g1.addLine(l2);
if (evaluate(g1) < maxscore) {
maxscore = (evaluate(g1));
lnew = l2;
}
}
return lnew;
}
}
@Override
public String getName() {
// TODO Auto-generated method stub
return "MiniMax";
}
/*
* This method calculates the value of the game after all
* available boxes have been taken. It is not a great
* evaluation function, but if it is called at a depth
* of three in the Mini-Max tree, it will be able to
* spot a double-cross move and thus beat EasyAI pretty
* much every time.
*/
private int evaluate(GameState game) {
int chain = chainLength(game);
return game.getValue() + (game.getPlayer()==1 ? chain : -chain);
}
/*
* This method calculates the number of available boxes
*/
private int chainLength(GameState game) {
GameState safe = game.clone();
List<Line> lines = safe.getRemainingLines();
for (Line line : lines) {
int moveScore = safe.moveScore(line);
if (moveScore > 0) {
safe.addLine(line);
return moveScore + chainLength(safe);
}
}
return 0;
}
}