In this version it is possible to add and remove a piece (BLACK_PAWN) to the board.

The board is diplayed in a very basic way in the console.
This commit is contained in:
2006-01-05 16:37:20 +00:00
parent c3ced2c700
commit 08c973a452
4 changed files with 80 additions and 17 deletions

128
src/suicideChess/Board.java Normal file
View File

@ -0,0 +1,128 @@
package suicideChess;
/**
* @author djib
*
* This file 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
*
* $LastChangedDate$
* $LastChangedRevision$
* $LastChangedBy$
*/
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<NB_OF_SQUARES; i++) {
mapSquaresToBits[i] = (1L << i);
}
}
//The following table contains all the bit boards
protected long bitBoards[];
/***************
* CONSTRUCTOR *
***************/
public Board() {
bitBoards = new long[NB_OF_BITBOARDS];
}
/***********
* METHODS *
***********/
private long getBitBoard(int bitboard_number) {
return bitBoards[bitboard_number];
}
private void updateBitBoards(String move) {
//TODO
}
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 (checkIsNotEmpty(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 checkIsNotEmpty(Square square, Piece piece) {
long mask = mapSquaresToBits[squareToBitBoardSquare(square)];
if ((bitBoards[piece.getPieceNumber()] & mask) == 0) {
return false;
} else {
return true;
}
}
}