Version 7.0
=========== Corrected a few bugs from 6.9, like the principal variation. About to add opening book.
This commit is contained in:
@ -60,7 +60,7 @@ public class ComputerPlayer {
|
||||
|
||||
int bestScore = MinMax(bitboard, 0);
|
||||
if (SuicideChess.postThinkingOutput()) {
|
||||
System.out.println(SuicideChess.PLY_DEPTH+" "+bestScore*100+" 0 "+nodesSearched+" "+bestMove);
|
||||
System.out.println(SuicideChess.getPlyDepth()+" "+bestScore*100+" 0 "+nodesSearched+" "+bestMove);
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
@ -69,7 +69,7 @@ public class ComputerPlayer {
|
||||
private static int MinMax(Board bitboard, int currentDepth) throws NotAValidSquare, NoPieceOnSquare {
|
||||
nodesSearched++;
|
||||
|
||||
if (currentDepth >= SuicideChess.PLY_DEPTH) {
|
||||
if (currentDepth >= SuicideChess.getPlyDepth()) {
|
||||
return bitboard.getBoardValue();
|
||||
}
|
||||
|
||||
@ -140,50 +140,63 @@ public class ComputerPlayer {
|
||||
bestMove = null;
|
||||
nodesSearched = 0;
|
||||
|
||||
Date thinkingBeginingTime = new Date();
|
||||
ReturnWrapper bestScore = AlphaBeta(bitboard, 0, Board.BLACK_WINS-1, Board.WHITE_WINS+1);
|
||||
Date thinkingEndTime = new Date();
|
||||
thinkingBeginingTime = new Date();
|
||||
|
||||
//iterative deepening
|
||||
for(maxDepth=2; maxDepth<=SuicideChess.getPlyDepth(); maxDepth++) {
|
||||
ReturnWrapper bestScore = AlphaBeta(bitboard, 0, Board.MIN_VALUE, Board.MAX_VALUE);
|
||||
Date thinkingEndTime = new Date();
|
||||
|
||||
//select one of the best moves randomly
|
||||
Random generator = new Random();
|
||||
//if (currentDepth == 0) { System.out.println(bestMoveIndex.size()); }
|
||||
bestMove = bestMoves.get(generator.nextInt(bestMoves.size()));
|
||||
|
||||
if (SuicideChess.postThinkingOutput()) {
|
||||
System.out.println(maxDepth+"\t"+bestScore.getBranchValue()*100+
|
||||
"\t"+((int)(thinkingEndTime.getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
|
||||
"\t"+nodesSearched+"\t"+bestScore.getPrincipalVariation());
|
||||
}
|
||||
|
||||
//select one of the best moves randomly
|
||||
Random generator = new Random();
|
||||
//if (currentDepth == 0) { System.out.println(bestMoveIndex.size()); }
|
||||
bestMove = bestMoves.get(generator.nextInt(bestMoves.size()));
|
||||
}
|
||||
if(SuicideChess.playInACSII()) {
|
||||
System.out.println("Found "+bestMoves.size()+" good moves.");
|
||||
}
|
||||
|
||||
System.out.println("Found "+bestMoves.size()+" good moves.");
|
||||
|
||||
if (SuicideChess.postThinkingOutput()) {
|
||||
System.out.println(SuicideChess.PLY_DEPTH+" "+bestScore.getAlphaBeta()*100+
|
||||
" "+((int)(thinkingEndTime.getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
|
||||
" "+nodesSearched+" "+bestMove);
|
||||
}
|
||||
System.out.println(((bitboard.mobility(Piece.WHITE))));
|
||||
System.out.println(((-bitboard.mobility(Piece.BLACK))));
|
||||
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
|
||||
private static ArrayList<Move> bestMoves=new ArrayList<Move>();
|
||||
private static Date thinkingBeginingTime;
|
||||
private static int maxDepth;
|
||||
private static ArrayList<Move> bestMoves=new ArrayList<Move>();
|
||||
//this class is used to return two arguments in the next function, the two arguments being
|
||||
//an integer representing the value of alpha or beta
|
||||
//an integer representing the real value of the branch (alpha-beta only give boundaries)
|
||||
private static class ReturnWrapper {
|
||||
private int alphaBeta;
|
||||
private int branchValue;
|
||||
public ReturnWrapper(int alphaBeta, int branchValue) {
|
||||
private String principalVariation;
|
||||
public ReturnWrapper(int alphaBeta, int branchValue, String principalVariation) {
|
||||
this.alphaBeta = alphaBeta;
|
||||
this.branchValue = branchValue;
|
||||
this.principalVariation = principalVariation;
|
||||
}
|
||||
public int getAlphaBeta() {return this.alphaBeta;}
|
||||
public int getBranchValue() {return this.branchValue;}
|
||||
public String getPrincipalVariation() {return this.principalVariation;}
|
||||
};
|
||||
private static ReturnWrapper AlphaBeta(Board bitboard, int currentDepth, int alpha, int beta) throws NotAValidSquare, NoPieceOnSquare {
|
||||
nodesSearched++;
|
||||
|
||||
if(bitboard.isADraw()) {
|
||||
return new ReturnWrapper(Board.DRAW_BOARD,Board.DRAW_BOARD);
|
||||
return new ReturnWrapper(Board.DRAW_BOARD,Board.DRAW_BOARD,"");
|
||||
}
|
||||
if (currentDepth >= SuicideChess.PLY_DEPTH) {
|
||||
if (currentDepth >= maxDepth) {
|
||||
//System.out.println("'-> Evaluate: "+bitboard.getBoardValue());
|
||||
return new ReturnWrapper(bitboard.getBoardValue(),bitboard.getBoardValue());
|
||||
return new ReturnWrapper(bitboard.getBoardValue(),bitboard.getBoardValue(),"");
|
||||
}
|
||||
|
||||
Rules.legalMovesForPlayer(bitboard);
|
||||
@ -194,15 +207,16 @@ public class ComputerPlayer {
|
||||
if (allLegalMoves.size()==0) {
|
||||
if (bitboard.getCurrentPlayer()==Piece.BLACK) {
|
||||
//System.out.println("'-> Evaluate *BLACK WINS*: "+Board.BLACK_WINS);
|
||||
return new ReturnWrapper(Board.BLACK_WINS,Board.BLACK_WINS);
|
||||
return new ReturnWrapper(Board.BLACK_WINS,Board.BLACK_WINS,"");
|
||||
} else {
|
||||
//System.out.println("'-> Evaluate *WHITE WINS* : "+Board.WHITE_WINS);
|
||||
return new ReturnWrapper(Board.WHITE_WINS,Board.WHITE_WINS);
|
||||
return new ReturnWrapper(Board.WHITE_WINS,Board.WHITE_WINS,"");
|
||||
}
|
||||
} else {
|
||||
int currentScore;
|
||||
int bestScoreSoFar;
|
||||
int currentAlphaBeta;
|
||||
String bestVariationSoFar="";
|
||||
if (bitboard.getCurrentPlayer()==Piece.BLACK) {
|
||||
bestScoreSoFar=Board.MAX_VALUE; //black tries to minimise
|
||||
for (int i=0; i<allLegalMoves.size(); i++) {
|
||||
@ -226,8 +240,14 @@ public class ComputerPlayer {
|
||||
if (currentScore <= bestScoreSoFar) {
|
||||
if (currentScore < bestScoreSoFar) {
|
||||
bestScoreSoFar=currentScore;
|
||||
bestVariationSoFar = allLegalMoves.get(i).toString()+" "+returnValue.getPrincipalVariation();
|
||||
if (currentDepth==0) {
|
||||
bestMoves.clear();
|
||||
if (SuicideChess.postThinkingOutput()) {
|
||||
System.out.println(maxDepth+"\t"+returnValue.getBranchValue()*100+
|
||||
"\t"+((int)((new Date()).getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
|
||||
"\t"+nodesSearched+"\t"+bestVariationSoFar);
|
||||
}
|
||||
//System.out.println("*** Clear ");
|
||||
}
|
||||
}
|
||||
@ -236,19 +256,19 @@ public class ComputerPlayer {
|
||||
//System.out.println("*** Adding "+allLegalMoves.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(beta<alpha) {
|
||||
//if(currentDepth!=SuicideChess.PLY_DEPTH-1) System.out.println("Pruning "+Integer.toString(allLegalMoves.size()-i)+" alternatives at depth "+ currentDepth);
|
||||
return new ReturnWrapper(alpha,bestScoreSoFar); //pruning
|
||||
//if(currentDepth!=SuicideChess.getPlyDepth()-1) System.out.println("Pruning "+Integer.toString(allLegalMoves.size()-i)+" alternatives at depth "+ currentDepth);
|
||||
return new ReturnWrapper(alpha,bestScoreSoFar,bestVariationSoFar); //pruning
|
||||
}
|
||||
}
|
||||
return new ReturnWrapper(beta,bestScoreSoFar);
|
||||
return new ReturnWrapper(beta,bestScoreSoFar,bestVariationSoFar);
|
||||
} else { //white piece
|
||||
bestScoreSoFar=Board.MIN_VALUE; //white tries to maximise
|
||||
for (int i=0; i<allLegalMoves.size(); i++) {
|
||||
Board boardCopy = new Board(bitboard);
|
||||
boardCopy.doMove(allLegalMoves.get(i));
|
||||
|
||||
|
||||
//System.out.println("Analysing "+currentDepth+":"+allLegalMoves.get(i));
|
||||
|
||||
ReturnWrapper returnValue = AlphaBeta(boardCopy,currentDepth+1,alpha,Board.MAX_VALUE);
|
||||
@ -265,9 +285,15 @@ public class ComputerPlayer {
|
||||
//calculating branch value
|
||||
if (currentScore >= bestScoreSoFar) {
|
||||
if (currentScore > bestScoreSoFar) {
|
||||
bestVariationSoFar = allLegalMoves.get(i).toString()+" "+returnValue.getPrincipalVariation();
|
||||
bestScoreSoFar=currentScore;
|
||||
if (currentDepth==0) {
|
||||
bestMoves.clear();
|
||||
if (SuicideChess.postThinkingOutput()) {
|
||||
System.out.println(maxDepth+"\t"+returnValue.getBranchValue()*100+
|
||||
"\t"+((int)((new Date()).getTime()-thinkingBeginingTime.getTime())/10)+ //search time in centiseconds
|
||||
"\t"+nodesSearched+"\t"+bestVariationSoFar);
|
||||
}
|
||||
//System.out.println("*** Clear ");
|
||||
}
|
||||
}
|
||||
@ -278,11 +304,11 @@ public class ComputerPlayer {
|
||||
}
|
||||
|
||||
if(alpha>beta) {
|
||||
//if(currentDepth!=SuicideChess.PLY_DEPTH-1) System.out.println("Pruning "+Integer.toString(allLegalMoves.size()-i)+" alternatives at depth "+ currentDepth);
|
||||
return new ReturnWrapper(beta,bestScoreSoFar); //pruning
|
||||
//if(currentDepth!=SuicideChess.getPlyDepth()-1) System.out.println("Pruning "+Integer.toString(allLegalMoves.size()-i)+" alternatives at depth "+ currentDepth);
|
||||
return new ReturnWrapper(beta,bestScoreSoFar,bestVariationSoFar); //pruning
|
||||
}
|
||||
}
|
||||
return new ReturnWrapper(alpha,bestScoreSoFar);
|
||||
return new ReturnWrapper(alpha,bestScoreSoFar,bestVariationSoFar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user