From f9ff33b2b381c226491dbb87438b67e6ee8a2ebc Mon Sep 17 00:00:00 2001 From: djib Date: Tue, 18 Jul 2006 16:43:27 +0000 Subject: [PATCH] Version 1.0.3 ============= Corrected a bug in the principal variation first search --- src/suicideChess/Board.java | 53 ++++++++++++++++++++++------ src/suicideChess/ComputerPlayer.java | 12 ++++--- src/suicideChess/Move.java | 34 ++++++++++++++---- src/suicideChess/Rules.java | 2 +- src/suicideChess/SuicideChess.java | 23 +++++++----- 5 files changed, 95 insertions(+), 29 deletions(-) diff --git a/src/suicideChess/Board.java b/src/suicideChess/Board.java index 6442a5f..727a605 100644 --- a/src/suicideChess/Board.java +++ b/src/suicideChess/Board.java @@ -81,8 +81,7 @@ public class Board { } //The following table contains all the bit boards - protected long bitBoards[]; - + protected long bitBoards[]; private boolean enPassant=false; //is there an 'en passant pawn' on the board private Square enPassantSquare; @@ -632,10 +631,16 @@ public class Board { //update the board value : remove the value of the square the piece comes from and add the value //of the square the piece goes to - pieceValue += ( - ConfigFile.getSquareWeightMidgame()[squareToBitBoardSquare(move.toSquare())] - - ConfigFile.getSquareWeightMidgame()[squareToBitBoardSquare(move.fromSquare())] - )*ConfigFile.getPieceValuesMidgame()[move.getMovingPiece().getPieceNumber()]; + + if (move.isPromotionMove()){ + pieceValue += ConfigFile.getSquareWeightMidgame()[squareToBitBoardSquare(move.toSquare())] + *ConfigFile.getPieceValuesMidgame()[move.getPromotionPiece().getPieceNumber()]; + } else { + pieceValue += ConfigFile.getSquareWeightMidgame()[squareToBitBoardSquare(move.toSquare())] + *ConfigFile.getPieceValuesMidgame()[move.getMovingPiece().getPieceNumber()]; + } + pieceValue -= ConfigFile.getSquareWeightMidgame()[squareToBitBoardSquare(move.fromSquare())] + *ConfigFile.getPieceValuesMidgame()[move.getMovingPiece().getPieceNumber()]; //if there is a capture also remove the value of the sqare the piece was captured if (move.isCaptureMove()) { if (move.isEnPassant()) { @@ -667,10 +672,15 @@ public class Board { } }*/ - pieceValue += ( - ConfigFile.getSquareWeightEndgame()[squareToBitBoardSquare(move.toSquare())] - - ConfigFile.getSquareWeightEndgame()[squareToBitBoardSquare(move.fromSquare())] - )*ConfigFile.getPieceValuesEndgame()[move.getMovingPiece().getPieceNumber()]; + if (move.isPromotionMove()){ + pieceValue += ConfigFile.getSquareWeightEndgame()[squareToBitBoardSquare(move.toSquare())] + *ConfigFile.getPieceValuesEndgame()[move.getPromotionPiece().getPieceNumber()]; + } else { + pieceValue += ConfigFile.getSquareWeightEndgame()[squareToBitBoardSquare(move.toSquare())] + *ConfigFile.getPieceValuesEndgame()[move.getMovingPiece().getPieceNumber()]; + } + pieceValue -= ConfigFile.getSquareWeightEndgame()[squareToBitBoardSquare(move.fromSquare())] + *ConfigFile.getPieceValuesEndgame()[move.getMovingPiece().getPieceNumber()]; if (move.isCaptureMove()) { if (move.isEnPassant()) { pieceValue -= ConfigFile.getSquareWeightEndgame()[squareToBitBoardSquare(move.getEnPassantSquare())] @@ -694,4 +704,27 @@ public class Board { } } } + + + /** + * displays debug information + */ + public void debug(){ + for(int i = 0; i=alpha) { + if (currentAlphaBeta>alpha) { alpha = currentAlphaBeta; } //calculating branch value diff --git a/src/suicideChess/Move.java b/src/suicideChess/Move.java index 8298760..a7bb7e0 100644 --- a/src/suicideChess/Move.java +++ b/src/suicideChess/Move.java @@ -57,6 +57,7 @@ public class Move { switch (move.length()) { case 5: isPromotion = true; + promotionPiece = new Piece(move.substring(4,5).toCharArray()[0],board.getCurrentPlayer()); //no break statement here on purpose case 4: fromSquare = new Square(move.substring(0,2)); @@ -77,10 +78,30 @@ public class Move { } else { isCapture = false; } - - if (isPromotion) { - promotionPiece = new Piece(move.toCharArray()[4], movingPiece.getColor()); - } + + if(movingPiece.getPieceType()==Piece.PAWN) { + //check for enPassant capture + if(board.isEnPassant() && (toSquare.isEqual(board.getEnPassantSquare()))) { + isCapture = true; + isEnPassant = true; + if(movingPiece.getColor()==Piece.BLACK) { + enPassantSquare = new Square(board.getEnPassantSquare().getFileNb(),board.getEnPassantSquare().getRank()+1); + } else { + enPassantSquare = new Square(board.getEnPassantSquare().getFileNb(),board.getEnPassantSquare().getRank()-1); + } + capturePiece = board.getPiece(enPassantSquare); + } + + //check for setting enPassant Square + if(movingPiece.getColor()==Piece.BLACK && toSquare.getRank()==fromSquare.getRank()-2) { + isEnPassant = true; + enPassantSquare = new Square(fromSquare.getFileNb(),fromSquare.getRank()-1); + } + if(movingPiece.getColor()==Piece.WHITE && toSquare.getRank()==fromSquare.getRank()+2) { + isEnPassant = true; + enPassantSquare = new Square(fromSquare.getFileNb(),fromSquare.getRank()+1); + } + } } /** @@ -121,7 +142,7 @@ public class Move { /** * This is the constructor of the class. - * It should be used only for moves that enable or are 'en passant' capture. + * It should be used only for moves that enable 'en passant' or are 'en passant' capture. * * @param fromSquare The {@link Square} to move from. * @param toSquare The {@link Square} to move to. @@ -326,7 +347,8 @@ public class Move { System.out.println("To square: "+toSquare()); System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece().toString() : "No Promotion")); System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().toString() : "No Capture")); - System.out.println((enablesEnPassant() ? "Set en-pass.: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank() : "No en-pass. set")); + if (isEnPassant()) System.out.println("En passant: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank()); + System.out.println((enablesEnPassant() ? "Set en-pass.: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank() : "No en-pass. set")); System.out.println("================"); System.out.println(" "); } diff --git a/src/suicideChess/Rules.java b/src/suicideChess/Rules.java index f8784ca..0e10f68 100644 --- a/src/suicideChess/Rules.java +++ b/src/suicideChess/Rules.java @@ -190,7 +190,7 @@ public class Rules { } break; default: - throw new RuntimeException("ERROR 01."); + throw new RuntimeException("Unexpected error: e01"); } if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) { normalMove= false; //cannot move forward if there is a piece diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java index 2538872..a5d5fc4 100644 --- a/src/suicideChess/SuicideChess.java +++ b/src/suicideChess/SuicideChess.java @@ -28,16 +28,16 @@ public class SuicideChess { /** * does BitBoard.class removePiece function checks if removing piece is legal ? */ - public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = true; + public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = false; /** * does Square.class checks if the strings are valid (is "z9" a valid square ?) */ - public static final boolean SQUARE_CHECK_INVALID = true; + public static final boolean SQUARE_CHECK_INVALID = false; /** * Use mobility in evaluation function (slows the program down a lot) */ - public static final boolean USE_MOBILITY = true; + public static final boolean USE_MOBILITY = false; /** * do move ordering in Alpha-Beta pruning ? @@ -94,12 +94,12 @@ public class SuicideChess { /** * Try the primary variation from the earliest iteration first */ - public static final boolean PRINCIPAL_VARIATION_FIRST = false; + public static final boolean PRINCIPAL_VARIATION_FIRST = true; /** * The name to be displayed */ - public static final String NAME = "djib's SuShi v1.0.2"; + public static final String NAME = "djib's SuShi v1.0.3"; /** * Displays informations in the console. @@ -178,11 +178,11 @@ public class SuicideChess { private static void displayPlayer(Board bitboard) { if(bitboard.getCurrentPlayer()==Piece.BLACK) { - System.out.println("Black: "); + System.out.println("-> Black: "); } else { - System.out.println("White: "); + System.out.println("-> White: "); } - + //bitboard.debug(); } //this variable is used to decide whether or not the computer should use it's opening book @@ -398,6 +398,8 @@ public class SuicideChess { theMove = new Move(whatMove, bitboard); } + //theMove.display(); + if (testAndDisplayIfWinningOrDrawPosition(bitboard)) { //if board was set in an illegal position System.out.println("Illegal move: "+theMove.toString()); @@ -430,6 +432,9 @@ public class SuicideChess { if (asciiGame) System.out.println("Capturing is mandatory."); } + //bitboard.debug(); + for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) + System.out.println(allLegalMoves.get(moveIndex)); System.out.println("Illegal move: "+theMove.toString()); } else { bitboard.doMove(allLegalMoves.get(foundMoveIndex)); @@ -493,6 +498,8 @@ public class SuicideChess { bitboard.display(); displayPlayer(bitboard); } + computerMove.display(); + bitboard.debug(); } if (testAndDisplayIfWinningOrDrawPosition(bitboard)) {