package suicideChess;
import suicideChess.Square.NotAValidSquare;
/**
* This class contains the board representation.
* The board is represented using bitboards.
*
- a1 is square 0
* - h8 is square 7
* - a2 is square 8
* - ... and so on
*
* Note that the evaluation of the board balance is in this class since I considered that it is
* very closely related to the board position.
*
* @author Jean-Baptiste Hétier
* @version $LastChangedRevision$, $LastChangedDate$
*
*/
public class Board {
/*===========*
* CONSTANTS *
*===========*/
//Some constants to make code more readable
public static final int NB_OF_RANKS = 8;
public static final int NB_OF_FILES = 8;
public static final int NB_OF_SQUARES = NB_OF_RANKS*NB_OF_FILES;
private static final int NB_OF_BITBOARDS = 14;
public class NoPieceOnSquare extends Exception {
/*
* Added by Eclipse
*/
private static final long serialVersionUID = -2750943856086117656L;
NoPieceOnSquare(String s) { super(s); };
}
/**
* Value returned by the evaluation function when White wins
*/
public static final int WHITE_WINS = 99999;
/**
* Value returned by the evaluation function when Black wins
*/
public static final int BLACK_WINS = -99999;
/*======*
* DATA *
*======*/
//The following table is used to map squares to bits
protected static long mapSquaresToBits[];
//static function used to initialise data
static {
mapSquaresToBits = new long[NB_OF_SQUARES];
for(int i=0; i= 1; file--) {
System.out.println(" +---+---+---+---+---+---+---+---+");
String display = file+" |";
for (int rank=1; rank <= NB_OF_RANKS; rank++) {
boolean displayedSomething = false;
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
if (displayPiece(Piece.BLACK_PAWN,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.PAWN_CHAR)+"'|";
}
if (displayPiece(Piece.BLACK_QUEEN,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.QUEEN_CHAR)+"'|";
}
if (displayPiece(Piece.BLACK_KING,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.KING_CHAR)+"'|";
}
if (displayPiece(Piece.BLACK_KNIGHT,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.KNIGHT_CHAR)+"'|";
}
if (displayPiece(Piece.BLACK_ROOK,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.ROOK_CHAR)+"'|";
}
if (displayPiece(Piece.BLACK_BISHOP,mask)) {
displayedSomething=true;
display+="'"+Character.toUpperCase(Piece.BISHOP_CHAR)+"'|";
}
if (displayPiece(Piece.WHITE_PAWN,mask)) {
displayedSomething=true;
display+=" "+Piece.PAWN_CHAR+" |";
}
if (displayPiece(Piece.WHITE_QUEEN,mask)) {
displayedSomething=true;
display+=" "+Piece.QUEEN_CHAR+" |";
}
if (displayPiece(Piece.WHITE_KING,mask)) {
displayedSomething=true;
display+=" "+Piece.KING_CHAR+" |";
}
if (displayPiece(Piece.WHITE_KNIGHT,mask)) {
displayedSomething=true;
display+=" "+Piece.KNIGHT_CHAR+" |";
}
if (displayPiece(Piece.WHITE_ROOK,mask)) {
displayedSomething=true;
display+=" "+Piece.ROOK_CHAR+" |";
}
if (displayPiece(Piece.WHITE_BISHOP,mask)) {
displayedSomething=true;
display+=" "+Piece.BISHOP_CHAR+" |";
}
if (!displayedSomething)
display+=" |";
}
System.out.println(display);
}
System.out.println(" +---+---+---+---+---+---+---+---+");
System.out.println(" a b c d e f g h");
System.out.println();
}
/*=================*
* PRIVATE METHODS *
*=================*/
/* private long getBitBoard(int bitboard_number) {
return bitBoards[bitboard_number];
}
*/
protected void addPiece(Square square, Piece piece) {
//OR :
// 0 OR a = a
// 1 OR a = 1
//add Piece to corresponding bitboard.
bitBoards[piece.getPieceNumber()] |= mapSquaresToBits[squareToBitBoardSquare(square)];
//update the bitboard of all pieces of that color
bitBoards[piece.getColor()] |= mapSquaresToBits[squareToBitBoardSquare(square)];
}
protected void removePiece(Square square, Piece piece) throws NoPieceOnSquare {
//XOR :
// 0 XOR a = a
// 1 XOR 0 = 1 !!! Don't remove a piece that don't exist !!!
// 1 XOR 1 = 0
//remove Piece to corresponding bitboard.
if (SuicideChess.BITBOARD_REMOVEPIECE_CHECK_REMOVE) {
if (!isEmpty(square, piece)) {
bitBoards[piece.getPieceNumber()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];
} else {
throw new NoPieceOnSquare("Square: "+square + "; Piece: " + piece.getPieceNumber());
}
} else {
bitBoards[piece.getPieceNumber()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];
}
//update the bitboard of all pieces of that color
bitBoards[piece.getColor()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];;
}
//used by function display()
private boolean displayPiece(int whatToDisplay, long mask) {
if ((bitBoards[whatToDisplay] & mask)==0) {
return false;
} else {
return true;
}
}
private void evaluateNewBoardValue (Move move) {
if (move.isCaptureMove()) {
if (move.getCapturedPiece().getColor()==Piece.BLACK) {
numberOfBlackPieces--;
} else {
numberOfWhitePieces--;
}
if (numberOfBlackPieces == 0) {
boardValue = BLACK_WINS;
} else if (numberOfWhitePieces == 0){
boardValue = WHITE_WINS;
} else {
//this is a very very basic evaluation function that will be changed.
boardValue = numberOfBlackPieces - numberOfWhitePieces;
}
}
}
}