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
*
* @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); };
}
/*======*
* 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 = "| ";
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("+---+---+---+---+---+---+---+---+");
}
/*=================*
* PRIVATE METHODS *
*=================*/
/* private long getBitBoard(int bitboard_number) {
return bitBoards[bitboard_number];
}
*/
private int squareToBitBoardSquare(Square square) {
//converts a square ("e2") to a BitboardSquare (
return square.getFileNb() -1 + (square.getRank()-1)*NB_OF_FILES;
}
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)];;
}
//checks if a square is empty for a certain piece
private boolean isEmpty(Square square, Piece piece) {
long mask = mapSquaresToBits[squareToBitBoardSquare(square)];
if ((bitBoards[piece.getPieceNumber()] & mask) == 0) {
return true;
} else {
return false;
}
}
private boolean displayPiece(int whatToDisplay, long mask) {
if ((bitBoards[whatToDisplay] & mask)==0) {
return false;
} else {
return true;
}
}
}