Version 5.2

===========
Corrected another bug in alpha-beta.
This commit is contained in:
2006-06-29 18:45:21 +00:00
parent 4dc083acc1
commit 95deacff45
3 changed files with 44 additions and 19 deletions

View File

@ -44,10 +44,21 @@ public class Board {
*/ */
public static final int WHITE_WINS = 99999; public static final int WHITE_WINS = 99999;
/**
* Value representing the maximum value possible for the evaluation function
*/
public static final int MAX_VALUE = WHITE_WINS+1;
/** /**
* Value returned by the evaluation function when Black wins * Value returned by the evaluation function when Black wins
*/ */
public static final int BLACK_WINS = -99999; public static final int BLACK_WINS = -99999;
/**
* Value representing the minimum value possible for the evaluation function
*/
public static final int MIN_VALUE = BLACK_WINS-1;
/*======* /*======*

View File

@ -172,7 +172,7 @@ public class ComputerPlayer {
}; };
private static ReturnWrapper AlphaBeta(Board bitboard, int currentDepth, int alpha, int beta) throws NotAValidSquare, NoPieceOnSquare { private static ReturnWrapper AlphaBeta(Board bitboard, int currentDepth, int alpha, int beta) throws NotAValidSquare, NoPieceOnSquare {
if (currentDepth >= SuicideChess.PLY_DEPTH) { if (currentDepth >= SuicideChess.PLY_DEPTH) {
System.out.println("Evaluate :" + bitboard.getBoardValue()); //System.out.println("'-> Evaluate: "+bitboard.getBoardValue());
return new ReturnWrapper(bitboard.getBoardValue(),bitboard.getBoardValue()); return new ReturnWrapper(bitboard.getBoardValue(),bitboard.getBoardValue());
} }
nodesSearched++; nodesSearched++;
@ -184,31 +184,37 @@ public class ComputerPlayer {
} }
if (allLegalMoves.size()==0) { if (allLegalMoves.size()==0) {
if (bitboard.getCurrentPlayer()==Piece.BLACK) { if (bitboard.getCurrentPlayer()==Piece.BLACK) {
System.out.println("Evaluate :" + bitboard.getBoardValue()); //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 { } else {
System.out.println("Evaluate :" + bitboard.getBoardValue()); //System.out.println("'-> Evaluate *WHITE WINS* : "+Board.WHITE_WINS);
return new ReturnWrapper(Board.WHITE_WINS,Board.BLACK_WINS); return new ReturnWrapper(Board.WHITE_WINS,Board.WHITE_WINS);
} }
} else { } else {
int currentScore; int currentScore;
int bestScoreSoFar; int bestScoreSoFar;
int currentAlphaBeta; int currentAlphaBeta;
if (bitboard.getCurrentPlayer()==Piece.BLACK) { if (bitboard.getCurrentPlayer()==Piece.BLACK) {
bestScoreSoFar=Board.WHITE_WINS+1; //any move even a WHITE_WINS will be better than that bestScoreSoFar=Board.MAX_VALUE; //black tries to minimise
for (int i=0; i<allLegalMoves.size(); i++) { for (int i=0; i<allLegalMoves.size(); i++) {
Board boardCopy = new Board(bitboard); Board boardCopy = new Board(bitboard);
System.out.println(currentDepth + ": " + allLegalMoves.get(i));
boardCopy.doMove(allLegalMoves.get(i)); boardCopy.doMove(allLegalMoves.get(i));
//System.out.println("Analysing "+currentDepth+":"+allLegalMoves.get(i));
ReturnWrapper returnValue = AlphaBeta(boardCopy,currentDepth+1,alpha,beta); ReturnWrapper returnValue = AlphaBeta(boardCopy,currentDepth+1,alpha,beta);
currentScore = returnValue.getBranchValue(); currentScore = returnValue.getBranchValue();
currentAlphaBeta = returnValue.getAlphaBeta(); currentAlphaBeta = returnValue.getAlphaBeta();
System.out.println(currentScore + " " + currentAlphaBeta);
//System.out.println("| CurrentScore, BestScore:" + currentScore + ", " + bestScoreSoFar);
//System.out.println("| CurrentBeta, beta:" + currentAlphaBeta + ", " + beta);
//calculating new value of beta //calculating new value of beta
if (currentAlphaBeta<beta) { if (currentAlphaBeta<beta) {
beta = currentAlphaBeta; beta = currentAlphaBeta;
if (currentDepth==0) { if (currentDepth==0) {
bestMoves.clear(); bestMoves.clear();
//System.out.println("*** Clear ");
} }
} }
//calculating branch value //calculating branch value
@ -217,32 +223,39 @@ public class ComputerPlayer {
bestScoreSoFar=currentScore; bestScoreSoFar=currentScore;
} }
if(currentDepth==0) { if(currentDepth==0) {
System.out.println("Adding move");
bestMoves.add(allLegalMoves.get(i)); bestMoves.add(allLegalMoves.get(i));
//System.out.println("*** Adding "+allLegalMoves.get(i));
} }
} }
if(beta<=alpha) { if(beta<alpha) {
System.out.println("pruning"); if(currentDepth != 3)
System.out.println("Pruning "+Integer.toString(allLegalMoves.size()-i)+" alternatives at depth "+ currentDepth);
return new ReturnWrapper(alpha,bestScoreSoFar); //pruning return new ReturnWrapper(alpha,bestScoreSoFar); //pruning
} }
} }
return new ReturnWrapper(beta,bestScoreSoFar); return new ReturnWrapper(beta,bestScoreSoFar);
} else { //white piece } else { //white piece
bestScoreSoFar=Board.BLACK_WINS-1; //any move even a BLACK_WINS will be better than that bestScoreSoFar=Board.MIN_VALUE; //white tries to maximise
for (int i=0; i<allLegalMoves.size(); i++) { for (int i=0; i<allLegalMoves.size(); i++) {
Board boardCopy = new Board(bitboard); Board boardCopy = new Board(bitboard);
boardCopy.doMove(allLegalMoves.get(i)); boardCopy.doMove(allLegalMoves.get(i));
System.out.println(currentDepth + ": " + allLegalMoves.get(i));
//System.out.println("Analysing "+currentDepth+":"+allLegalMoves.get(i));
ReturnWrapper returnValue = AlphaBeta(boardCopy,currentDepth+1,alpha,beta); ReturnWrapper returnValue = AlphaBeta(boardCopy,currentDepth+1,alpha,beta);
currentScore = returnValue.getBranchValue(); currentScore = returnValue.getBranchValue();
currentAlphaBeta = returnValue.getAlphaBeta(); currentAlphaBeta = returnValue.getAlphaBeta();
System.out.println(currentScore + " " + currentAlphaBeta);
//System.out.println("| CurrentScore, BestScore:" + currentScore + ", " + bestScoreSoFar);
//System.out.println("| CurrentAlpha, alpha:" + currentAlphaBeta + ", " + alpha);
//calculating new value of beta //calculating new value of beta
if (currentAlphaBeta>alpha) { if (currentAlphaBeta>alpha) {
alpha = currentAlphaBeta; alpha = currentAlphaBeta;
if (currentDepth==0) { if (currentDepth==0) {
bestMoves.clear(); bestMoves.clear();
//System.out.println("*** Clear ");
} }
} }
//calculating branch value //calculating branch value
@ -251,13 +264,14 @@ public class ComputerPlayer {
bestScoreSoFar=currentScore; bestScoreSoFar=currentScore;
} }
if(currentDepth==0) { if(currentDepth==0) {
System.out.println("Adding move");
bestMoves.add(allLegalMoves.get(i)); bestMoves.add(allLegalMoves.get(i));
//System.out.println("*** Adding "+allLegalMoves.get(i));
} }
} }
if(alpha>=beta) { if(alpha>beta) {
System.out.println("pruning"); if(currentDepth != 3)
System.out.println("Pruning "+Integer.toString(allLegalMoves.size()-i)+" alternatives at depth "+ currentDepth);
return new ReturnWrapper(beta,bestScoreSoFar); //pruning return new ReturnWrapper(beta,bestScoreSoFar); //pruning
} }
} }

View File

@ -33,17 +33,17 @@ public class SuicideChess {
/** /**
* The name to be displayed * The name to be displayed
*/ */
public static final String NAME = "djib's SuShi (Suicide Chess) v0.5"; public static final String NAME = "djib's SuShi v0.5.2";
/** /**
* Displays informations in the console. * Displays informations in the console.
*/ */
public static final boolean ASCII_GAME = true; public static final boolean ASCII_GAME = false;
/** /**
* Number of Plies the computes searches to * Number of Plies the computes searches to
*/ */
public static final int PLY_DEPTH = 2; public static final int PLY_DEPTH = 4;
/** /**
* Test and display if the board is in a winning state. * Test and display if the board is in a winning state.