From 4dc083acc1b95eaefbb6ddc45fd356241c1b5f8e Mon Sep 17 00:00:00 2001 From: djib Date: Wed, 28 Jun 2006 16:59:07 +0000 Subject: [PATCH] Version 5.1 =========== Corrected bug in alpha-beta. --- src/suicideChess/ComputerPlayer.java | 129 +++++++++++++++++---------- src/suicideChess/SuicideChess.java | 4 +- 2 files changed, 82 insertions(+), 51 deletions(-) diff --git a/src/suicideChess/ComputerPlayer.java b/src/suicideChess/ComputerPlayer.java index f605b38..e3209eb 100644 --- a/src/suicideChess/ComputerPlayer.java +++ b/src/suicideChess/ComputerPlayer.java @@ -139,22 +139,43 @@ public class ComputerPlayer { bestMove = null; nodesSearched = 0; - int bestScore = AlphaBeta(bitboard, 0, Board.BLACK_WINS-1, Board.WHITE_WINS+1); + ReturnWrapper bestScore = AlphaBeta(bitboard, 0, Board.BLACK_WINS-1, Board.WHITE_WINS+1); + + //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())); + + System.out.println("Found "+bestMoves.size()+" good moves."); + if (SuicideChess.postThinkingOutput()) { - System.out.println(SuicideChess.PLY_DEPTH+" "+bestScore*100+" 0 "+nodesSearched+" "+bestMove); + System.out.println(SuicideChess.PLY_DEPTH+" "+bestScore.getAlphaBeta()*100+" 0 "+nodesSearched+" "+bestMove); } return bestMove; } - private static int AlphaBeta(Board bitboard, int currentDepth, int alpha, int beta) throws NotAValidSquare, NoPieceOnSquare { - if (currentDepth >= SuicideChess.PLY_DEPTH) { - return bitboard.getBoardValue(); + private static ArrayList bestMoves=new ArrayList(); + //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) { + this.alphaBeta = alphaBeta; + this.branchValue = branchValue; + } + public int getAlphaBeta() {return this.alphaBeta;} + public int getBranchValue() {return this.branchValue;} + }; + private static ReturnWrapper AlphaBeta(Board bitboard, int currentDepth, int alpha, int beta) throws NotAValidSquare, NoPieceOnSquare { + if (currentDepth >= SuicideChess.PLY_DEPTH) { + System.out.println("Evaluate :" + bitboard.getBoardValue()); + return new ReturnWrapper(bitboard.getBoardValue(),bitboard.getBoardValue()); } - nodesSearched++; - //System.out.println(currentDepth + " : " + alpha + " " + beta); Rules.legalMovesForPlayer(bitboard); ArrayList allLegalMoves = Rules.getLegalMovesCapture(); @@ -163,74 +184,84 @@ public class ComputerPlayer { } if (allLegalMoves.size()==0) { if (bitboard.getCurrentPlayer()==Piece.BLACK) { - return Board.BLACK_WINS; + System.out.println("Evaluate :" + bitboard.getBoardValue()); + return new ReturnWrapper(Board.BLACK_WINS,Board.BLACK_WINS); } else { - return Board.WHITE_WINS; + System.out.println("Evaluate :" + bitboard.getBoardValue()); + return new ReturnWrapper(Board.WHITE_WINS,Board.BLACK_WINS); } - } else { - ArrayList bestMoveIndex=new ArrayList(); - int bestScoreSoFar; int currentScore; + int bestScoreSoFar; + int currentAlphaBeta; if (bitboard.getCurrentPlayer()==Piece.BLACK) { bestScoreSoFar=Board.WHITE_WINS+1; //any move even a WHITE_WINS will be better than that for (int i=0; i= bestScoreSoFar)) { //white tries to maximise his score - if (currentScore>bestScoreSoFar) { - bestScoreSoFar = currentScore; - bestMoveIndex.clear(); + System.out.println(currentDepth + ": " + allLegalMoves.get(i)); + ReturnWrapper returnValue = AlphaBeta(boardCopy,currentDepth+1,alpha,beta); + currentScore = returnValue.getBranchValue(); + currentAlphaBeta = returnValue.getAlphaBeta(); + System.out.println(currentScore + " " + currentAlphaBeta); + //calculating new value of beta + if (currentAlphaBeta>alpha) { + alpha = currentAlphaBeta; + if (currentDepth==0) { + bestMoves.clear(); + } + } + //calculating branch value + if (currentScore >= bestScoreSoFar) { + if (currentScore > bestScoreSoFar) { + bestScoreSoFar=currentScore; + } + if(currentDepth==0) { + System.out.println("Adding move"); + bestMoves.add(allLegalMoves.get(i)); } - bestMoveIndex.add(i); } - - //select one of the best moves randomly - Random generator = new Random(); - //if (currentDepth == 0) { System.out.println(bestMoveIndex.size()); } - bestMove = allLegalMoves.get(bestMoveIndex.get(generator.nextInt(bestMoveIndex.size()))); - - //System.out.println(currentDepth + " : " + alpha + " " + beta); - if(alpha>=beta) { - return beta; //pruning + System.out.println("pruning"); + return new ReturnWrapper(beta,bestScoreSoFar); //pruning } } - return alpha; + return new ReturnWrapper(alpha,bestScoreSoFar); } } } diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java index 5b7e5ee..0942937 100644 --- a/src/suicideChess/SuicideChess.java +++ b/src/suicideChess/SuicideChess.java @@ -38,12 +38,12 @@ public class SuicideChess { /** * Displays informations in the console. */ - public static final boolean ASCII_GAME = false; + public static final boolean ASCII_GAME = true; /** * Number of Plies the computes searches to */ - public static final int PLY_DEPTH = 6; + public static final int PLY_DEPTH = 2; /** * Test and display if the board is in a winning state.