Version 0.4

Now plays with a grandpa minmax.
It is much better, though not excellent ;)
This commit is contained in:
2006-01-26 18:05:16 +00:00
parent 6cf9f27892
commit 5266b582bb
5 changed files with 337 additions and 160 deletions

View File

@ -3,6 +3,7 @@ package suicideChess;
import java.util.ArrayList;
import java.util.Random;
import suicideChess.Board.NoPieceOnSquare;
import suicideChess.Square.NotAValidSquare;
/**
@ -12,7 +13,9 @@ import suicideChess.Square.NotAValidSquare;
*/
public class ComputerPlayer {
/**
* This constructor creates a computer.
*/
@ -23,12 +26,13 @@ public class ComputerPlayer {
/**
* This asks the computer to compute a move
* @param bitboard The current status of the {@link Board}
* @param color The color to play
* @return move A {@link Move}
* @throws NotAValidSquare
* @see Board
* @see Move
*/
public Move doMove(Board bitboard, int color) throws NotAValidSquare {
public Move doRandomMove(Board bitboard, int color) throws NotAValidSquare {
Random generator = new Random();
Rules.legalMovesForPlayer(bitboard,color);
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
@ -39,8 +43,73 @@ public class ComputerPlayer {
return allLegalMoves.get(generator.nextInt(allLegalMoves.size()));
}
throw new RuntimeException();
throw new RuntimeException("**Error** in doRandomMove");
}
/**
* Basic MinMax
* @param bitboard The bitboard
* @param color The color to play
* @return The best Move found
* @throws NotAValidSquare
* @throws NoPieceOnSquare
* @see Board
* @see Move
*/
public Move doMinMaxMove(Board bitboard, int color) throws NotAValidSquare, NoPieceOnSquare {
bestMove = null;
MinMax(bitboard, color, 0);
return bestMove;
}
private int MinMax(Board bitboard, int color, int currentDepth) throws NotAValidSquare, NoPieceOnSquare {
if (currentDepth >= SuicideChess.PLY_DEPTH) {
return bitboard.getBoardValue();
}
Rules.legalMovesForPlayer(bitboard,color);
ArrayList<Move> allLegalMoves = Rules.getLegalMovesCapture();
if (allLegalMoves.size()==0) {
allLegalMoves = Rules.getLegalMovesNonCapture();
}
if (allLegalMoves.size()==0) {
if (color==Piece.BLACK) {
return Board.BLACK_WINS;
} else {
return Board.WHITE_WINS;
}
} else {
int bestMoveIndex=0;
int currentScore;
int bestScoreSoFar;
if (color==Piece.BLACK) {
bestScoreSoFar=Board.WHITE_WINS+1; //any move even a WHITE_WINS will be better than that
for (int i=0; i<allLegalMoves.size(); i++) {
Board boardCopy = new Board(bitboard);
boardCopy.doMove(allLegalMoves.get(i));
currentScore=MinMax(boardCopy,Piece.WHITE,currentDepth+1);
if (currentScore < bestScoreSoFar) { //black tries to minimise his score
bestScoreSoFar = currentScore;
bestMoveIndex = i;
}
}
} else { //white piece
bestScoreSoFar=Board.BLACK_WINS-1; //any move even a BLACK_WINS will be better than that
for (int i=0; i<allLegalMoves.size(); i++) {
Board boardCopy = new Board(bitboard);
boardCopy.doMove(allLegalMoves.get(i));
currentScore=MinMax(boardCopy,Piece.BLACK,currentDepth+1);
if (currentScore > bestScoreSoFar) { //white tries to maximise his score
bestScoreSoFar = currentScore;
bestMoveIndex = i;
}
}
}
bestMove = allLegalMoves.get(bestMoveIndex);
return bestScoreSoFar;
}
}
private static Move bestMove = null;
}