Version 1.4.2

Now possible to undo, save games, restore positions, and view statistics when the computer plays.
This commit is contained in:
2006-01-28 16:35:21 +00:00
parent a3916eb445
commit ed438e593a
5 changed files with 194 additions and 31 deletions

View File

@ -35,7 +35,10 @@ public class Board {
public class NoPieceOnSquare extends Exception {
NoPieceOnSquare(String s) { super(s); };
}
@SuppressWarnings("serial")
public class UnableToParseFENStringException extends Exception {
UnableToParseFENStringException(String s) { super(s); };
}
/**
* Value returned by the evaluation function when White wins
*/
@ -145,6 +148,73 @@ public class Board {
this.currentPlayer = bitboard.currentPlayer;
}
/**
* This constructor is used to create a board with a string in FEN notation
* @param command A string representing the command
* @return A {@link Board} corresponding to the FEN string
* @throws NotAValidSquare
* @throws UnableToParseFENStringException
* @see Board
*/
public Board(String command) throws NotAValidSquare, UnableToParseFENStringException {
String[] result = command.split("/|\\s");
/*
* After splitting
* 0->rank 8
* 1->rank 7
* ...
* 7->rank 1
* 8->color on move
* 9->castling (I don't care about this)
* 10->enpassant Square (if any), otherwise '-'
* 11->count of plies since the last pawn advance or capturing move
* 12->fullmove number
*/
bitBoards = new long[NB_OF_BITBOARDS];
if (result.length!=13) {
throw new UnableToParseFENStringException(command);
}
if (result[8].equals("b")) {
currentPlayer=Piece.BLACK;
} else {
currentPlayer=Piece.WHITE;
}
if (!result[10].equals("-")) {
enPassant = true;
enPassantSquare = new Square(result[10]);
} else {
enPassant = false;
enPassantSquare = new Square("a1"); //otherwise it is null
}
numberOfBlackPieces = 0;
numberOfWhitePieces = 0;
for(int split=0; split < 8; split++) {
int offset=0;
char[] rowToParse = result[split].toCharArray();
for (int character=0; character<rowToParse.length; character++) {
if (Character.isDigit(rowToParse[character])) {
offset+=((int)rowToParse[character]-(int)'1');
} else {
Piece pieceToAdd = new Piece(rowToParse[character]);
addPiece(new Square(character+offset+1,NB_OF_RANKS-split),pieceToAdd);
if (pieceToAdd.getColor()==Piece.BLACK) {
numberOfBlackPieces++;
} else {
numberOfWhitePieces++;
}
}
}
}
boardValue = getBoardValue();
}
/*================*
* PUBLIC METHODS *
*================*/
@ -315,51 +385,51 @@ public class Board {
if (displayPiece(Piece.BLACK_PAWN,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.PAWN_CHAR)+"'|";
display+="'"+Piece.BLACK_PAWN_CHAR+"'|";
}
if (displayPiece(Piece.BLACK_QUEEN,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.QUEEN_CHAR)+"'|";
display+="'"+Piece.BLACK_QUEEN_CHAR+"'|";
}
if (displayPiece(Piece.BLACK_KING,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.KING_CHAR)+"'|";
display+="'"+Piece.BLACK_KING_CHAR+"'|";
}
if (displayPiece(Piece.BLACK_KNIGHT,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.KNIGHT_CHAR)+"'|";
display+="'"+Piece.BLACK_KNIGHT_CHAR+"'|";
}
if (displayPiece(Piece.BLACK_ROOK,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.ROOK_CHAR)+"'|";
display+="'"+Piece.BLACK_ROOK_CHAR+"'|";
}
if (displayPiece(Piece.BLACK_BISHOP,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.BISHOP_CHAR)+"'|";
display+="'"+Piece.BLACK_BISHOP_CHAR+"'|";
}
if (displayPiece(Piece.WHITE_PAWN,mask)) {
displayedSomething=true;
display+=" "+Piece.PAWN_CHAR+" |";
display+=" "+Piece.WHITE_PAWN_CHAR+" |";
}
if (displayPiece(Piece.WHITE_QUEEN,mask)) {
displayedSomething=true;
display+=" "+Piece.QUEEN_CHAR+" |";
display+=" "+Piece.WHITE_QUEEN_CHAR+" |";
}
if (displayPiece(Piece.WHITE_KING,mask)) {
displayedSomething=true;
display+=" "+Piece.KING_CHAR+" |";
display+=" "+Piece.WHITE_KING_CHAR+" |";
}
if (displayPiece(Piece.WHITE_KNIGHT,mask)) {
displayedSomething=true;
display+=" "+Piece.KNIGHT_CHAR+" |";
display+=" "+Piece.WHITE_KNIGHT_CHAR+" |";
}
if (displayPiece(Piece.WHITE_ROOK,mask)) {
displayedSomething=true;
display+=" "+Piece.ROOK_CHAR+" |";
display+=" "+Piece.WHITE_ROOK_CHAR+" |";
}
if (displayPiece(Piece.WHITE_BISHOP,mask)) {
displayedSomething=true;
display+=" "+Piece.BISHOP_CHAR+" |";
display+=" "+Piece.WHITE_BISHOP_CHAR+" |";
}
if (!displayedSomething)
display+=" |";
@ -438,14 +508,11 @@ public class Board {
boardValue = numberOfBlackPieces - numberOfWhitePieces;
}
}
Rules.legalMovesForPlayer(this);
if ((Rules.getLegalMovesCapture().size()==0)&&(Rules.getLegalMovesNonCapture().size()==0)) {
// The following line is NOT an error !!!
// After move from WHITE, if there is no moves for BLACK, BLACK won.
if (move.getMovingPiece().getColor()==Piece.WHITE) {
boardValue = BLACK_WINS;
} else {
if (Rules.isThereALegalMovesForPlayer(this)) {
if (currentPlayer==Piece.WHITE) {
boardValue = WHITE_WINS;
} else {
boardValue = BLACK_WINS;
}
}
}