After 3 attempts for creating a chess program not doing brute force calculations and still play nice, I gave up. It's not going to work.
Sooo, let's see if I can implement minimax!
I think I implemented it correctly but since I get strange results (really weird moves) if the search depth gets > 1, I want to ask you if you can point me at my mistake.
Code: Select all
int iterate(Move baseMoveIn, SelectedMove selectedMove, List<QueuedMove> queuedMoves, Scene scene, PlayerColor baseColor, PlayerColor currentColor, int depth, IO io) {
// get list of moves in the current setup
List<Move> moves = scene.getMoveList(currentColor);
// just a difficult way of finding the color of the opponent :_)
PlayerColor afterMoveColor = Statics.opponentColor(currentColor);
// go through list of moves
for(Move currentMove : moves) {
// calculate the situation after the move
Scene afterMove = scene.Move(currentMove);
// for the situation after the move, calculate the shannon value
// seen from the point of view of the opponent
double value = getShannonValue(afterMove, afterMoveColor);
// value bigger than previous calculated value and we're looking at
// the shannon value for the color that started the whole search?
// then remember the move that started it all (one of the moves in
// the first step of the tree) and the value
Move baseMoveUse = baseMoveIn != null ? baseMoveIn : currentMove;
if (value > selectedMove.getValue() && afterMoveColor == baseColor) {
selectedMove.setMove(baseMoveUse, value, depth);
}
// queue the node after the move; will check its children
if (afterMove.isCheckMate(afterMoveColor) == false)
queuedMoves.add(new QueuedMove(baseMoveUse, value, afterMove, afterMoveColor, depth + 1));
}
return moves.size();
}
public SelectedMove calculateMove(int it, List<History> history, double finishBefore, Scene scene, PlayerColor myColor, IO io)
{
SelectedMove selectedMove = new SelectedMove(null, -25000000.0, -1);
List<QueuedMove> queuedMoves = new ArrayList<QueuedMove>();
int processed = 0;
// depth = 0
processed += iterate(null, selectedMove, queuedMoves, scene, myColor, myColor, 1, io);
for(;;) {
int size = queuedMoves.size();
if (size == 0 || Statics.getTimestamp() >= finishBefore)
break;
QueuedMove currentQueuedMove = queuedMoves.remove(size - 1);
// other depths
processed += iterate(currentQueuedMove.getBaseMove(), selectedMove, queuedMoves, currentQueuedMove.getScene(), myColor, currentQueuedMove.getColor(), currentQueuedMove.getDepth(), io);
}
return selectedMove;
}