Version 6.9

===========
Now detects draw (50 moves only)
Displays time to find move
Can read a library of problems from a file
Each problem can be selected independently or all can be
  played in succession and then the result is displayed.
This commit is contained in:
2006-07-01 15:06:24 +00:00
parent 74206376a7
commit 1b500b4421
5 changed files with 369 additions and 161 deletions

View File

@ -1,5 +1,6 @@
package suicideChess;
import suicideChess.Square.NotAValidSquare;
/**
@ -32,8 +33,11 @@ public class Board {
private static final int NB_OF_BITBOARDS = 14;
//with less than that many pawns on one side, the computer will enter endgame mode
public static final int ENDGAME = 3;
public static final int ENDGAME_PAWNS = 3;
//with less than that many pieces on one side, the computer will enter endgame mode
public static final int ENDGAME_PIECES = 6;
@SuppressWarnings("serial")
public class NoPieceOnSquare extends Exception {
NoPieceOnSquare(String s) { super(s); };
@ -61,9 +65,12 @@ public class Board {
* Value representing the minimum value possible for the evaluation function
*/
public static final int MIN_VALUE = BLACK_WINS-1;
/**
* Value representing the value of a draw position
*/
public static final int DRAW_BOARD = 0;
/*======*
* DATA *
*======*/
@ -94,6 +101,11 @@ public class Board {
private int boardValue = 0; //evaluation of the board value
private int currentPlayer; //color of the current player.
//starts at 1 and increment after each of black's move
private int fullmoveNumber;
//this variable holds the number of Moves since last capture or last pawn move
private int halfmoveClock;
/*=============*
* CONSTRUCTOR *
@ -108,6 +120,9 @@ public class Board {
//the following line makes sure that enPassantSquare is defined at some point.
enPassantSquare = new Square("a1");
fullmoveNumber = 1;
halfmoveClock = 0;
numberOfPieces = new int[Piece.MAX_PIECE_NUMBER+1];
for (int i=0; i<=Piece.MAX_PIECE_NUMBER; i++) {
numberOfPieces[i]=0;
@ -163,6 +178,9 @@ public class Board {
this.numberOfPieces[i] = bitboard.numberOfPieces[i];
}
this.fullmoveNumber = bitboard.fullmoveNumber;
this.halfmoveClock = bitboard.halfmoveClock;
this.boardValue = bitboard.boardValue;
this.bitBoards = new long[NB_OF_BITBOARDS];
for (int i=0; i<NB_OF_BITBOARDS; i++) {
@ -214,9 +232,16 @@ public class Board {
enPassant = false;
enPassantSquare = new Square("a1"); //otherwise it is null
}
try {
fullmoveNumber = Integer.parseInt(result[12]);
halfmoveClock = Integer.parseInt(result[11]);
} catch (NumberFormatException e) {
System.out.println("Error (impossible to parse clock): "+command);
fullmoveNumber = 1;
halfmoveClock = 0;
}
numberOfPieces = new int[Piece.MAX_PIECE_NUMBER];
numberOfPieces = new int[Piece.MAX_PIECE_NUMBER+1];
for(int split=0; split < 8; split++) {
int offset=0;
@ -262,12 +287,14 @@ public class Board {
*/
public void doMove(Move move) throws NoPieceOnSquare, NotAValidSquare {
if (move.isCaptureMove()) {
halfmoveClock++;
if (move.isCaptureMove()) {
if (move.isEnPassant()) {
removePiece(move.getEnPassantSquare(), move.getCapturedPiece());
} else {
removePiece(move.toSquare(), move.getCapturedPiece());
}
halfmoveClock=0; //reinitialise halfmoveClock since there has been a capture
}
removePiece(move.fromSquare(), move.getMovingPiece());
if (move.isPromotionMove()) {
@ -275,6 +302,9 @@ public class Board {
} else {
addPiece(move.toSquare(), move.getMovingPiece());
}
if(move.getMovingPiece().getPieceNumber()==Piece.PAWN) {
halfmoveClock = 0; //a pawn has been moved
}
this.enPassant=false;
if (move.enablesEnPassant()) {
@ -287,11 +317,12 @@ public class Board {
//if (SuicideChess.ASCII_GAME)
// System.out.println("Black: ");
} else {
fullmoveNumber++; //black has just played
currentPlayer=Piece.WHITE;
//if (SuicideChess.ASCII_GAME)
// System.out.println("White: ");
}
evaluateNewBoardValue(move);
}
@ -396,6 +427,24 @@ public class Board {
return boardValue;
}
/**
* This function tells if the board is in a draw status
*/
public boolean isADraw() {
if(halfmoveClock>=Rules.NUMBER_OF_MOVES_BEFORE_DRAW) {
return true;
}
return false;
}
/**
* Used to get the halfmoveClock.
* @return the number of halfmoves since the last capture or pawn movement
*/
public int getHalfmoveClock() {
return halfmoveClock;
}
/**
* This function returns the current player color
* @return Integer
@ -543,13 +592,14 @@ public class Board {
//this is a very very basic evaluation function that will be changed.
//boardValue = numberOfBlackPieces - numberOfWhitePieces;
boardValue = 0;
if((numberOfPieces[Piece.BLACK_PAWN] <= ENDGAME) || (numberOfPieces[Piece.WHITE_PAWN] <= ENDGAME)) {
System.out.println("Playing endgame");
if((numberOfPieces[Piece.BLACK_PAWN] <= ENDGAME_PAWNS) || (numberOfPieces[Piece.WHITE_PAWN] <= ENDGAME_PAWNS)
|| (numberOfPieces[Piece.BLACK_PIECES] <= ENDGAME_PIECES) || (numberOfPieces[Piece.WHITE_PIECES] <= ENDGAME_PIECES) ) {
//System.out.println("Playing endgame");
for (int i = Piece.OFFSET; i<=Piece.MAX_PIECE_NUMBER; i++) {
boardValue += numberOfPieces[i]*Piece.PIECE_VALUE_ENDGAME[i];
}
} else {
System.out.println("Playing midgame");
//System.out.println("Playing midgame");
for (int i = Piece.OFFSET; i<=Piece.MAX_PIECE_NUMBER; i++) {
boardValue += numberOfPieces[i]*Piece.PIECE_VALUE_MIDDLEGAME[i];
}