Pieces can be added and removed to bitboards.
I need to implement move function and it will be ok for a very basic suicide chess program without any move checking.
This commit is contained in:
4
TODO
4
TODO
@ -1,3 +1 @@
|
||||
Créer une classe Piece pour mettre dans PromotionPiece dans Move.java
|
||||
|
||||
Further testing of the moves : check if squares are valid (function is square ?)
|
||||
Make sure we don't remove a piece that don't exist.
|
||||
|
@ -25,24 +25,7 @@ public class BitBoard {
|
||||
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;
|
||||
|
||||
//Some constants used to access the bitboards
|
||||
//Note that in suicide chess there may be more than one king
|
||||
public static final int WHITE_PAWNS = 0;
|
||||
public static final int BLACK_PAWNS = 1;
|
||||
public static final int WHITE_KINGS = 2;
|
||||
public static final int BLACK_KINGS = 3;
|
||||
public static final int WHITE_QUEENS = 4;
|
||||
public static final int BLACK_QUEENS = 5;
|
||||
public static final int WHITE_BISHOPS = 6;
|
||||
public static final int BLACK_BISHOPS = 7;
|
||||
public static final int WHITE_KNIGHTS = 8;
|
||||
public static final int BLACK_KNIGHTS = 9;
|
||||
public static final int WHITE_ROOKS = 10;
|
||||
public static final int BLACK_ROOKS = 11;
|
||||
public static final int WHITE_PIECES = 12;
|
||||
public static final int BLACK_PIECES = 13;
|
||||
|
||||
|
||||
public static final int NB_OF_BITBOARDS = 14;
|
||||
|
||||
|
||||
@ -51,7 +34,7 @@ public class BitBoard {
|
||||
********/
|
||||
|
||||
//The following table is used to map squares to bits
|
||||
private static long mapSquaresToBits[];
|
||||
protected static long mapSquaresToBits[];
|
||||
|
||||
//static function used to initialise data
|
||||
static {
|
||||
@ -62,7 +45,7 @@ public class BitBoard {
|
||||
}
|
||||
|
||||
//The following table contains all the bit boards
|
||||
private long bitBoards[];
|
||||
protected long bitBoards[];
|
||||
|
||||
/***************
|
||||
* CONSTRUCTOR *
|
||||
@ -70,6 +53,7 @@ public class BitBoard {
|
||||
|
||||
public BitBoard() {
|
||||
bitBoards = new long[NB_OF_BITBOARDS];
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -77,18 +61,40 @@ public class BitBoard {
|
||||
* METHODS *
|
||||
***********/
|
||||
|
||||
public long getBitboard(int bitboard_number) {
|
||||
public long getBitBoard(int bitboard_number) {
|
||||
return bitBoards[bitboard_number];
|
||||
}
|
||||
|
||||
|
||||
public void updateBitboards(String move) {
|
||||
public void updateBitBoards(String move) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int squareToBitboardSquare(Square square) {
|
||||
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) {
|
||||
//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.
|
||||
bitBoards[piece.getPieceNumber()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];
|
||||
//update the bitboard of all pieces of that color
|
||||
bitBoards[piece.getColor()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];;
|
||||
}
|
||||
}
|
78
src/suicideChess/Piece.java
Normal file
78
src/suicideChess/Piece.java
Normal file
@ -0,0 +1,78 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package suicideChess;
|
||||
|
||||
/**
|
||||
* @author djib
|
||||
*
|
||||
* This file contains the moves representation.
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
* $LastChangedBy$
|
||||
*/
|
||||
|
||||
public class Piece {
|
||||
/*************
|
||||
* CONSTANTS *
|
||||
*************/
|
||||
|
||||
|
||||
/**
|
||||
* Take really good care if you want to change those values
|
||||
* Class BitBoard makes intensive use of those
|
||||
*/
|
||||
//Contants used to detect color
|
||||
public static final int WHITE=0;
|
||||
public static final int BLACK=1;
|
||||
//Constants used in the board representation
|
||||
public static final int WHITE_PIECE = WHITE;
|
||||
public static final int BLACK_PIECE = BLACK;
|
||||
public static final int WHITE_PAWN = 2;
|
||||
public static final int BLACK_PAWN = 3;
|
||||
public static final int WHITE_KING = 4;
|
||||
public static final int BLACK_KING = 5;
|
||||
public static final int WHITE_QUEEN = 6;
|
||||
public static final int BLACK_QUEEN = 7;
|
||||
public static final int WHITE_BISHOP = 8;
|
||||
public static final int BLACK_BISHOP = 9;
|
||||
public static final int WHITE_KNIGHT = 10;
|
||||
public static final int BLACK_KNIGHT = 11;
|
||||
public static final int WHITE_ROOK = 12;
|
||||
public static final int BLACK_ROOK = 13;
|
||||
|
||||
|
||||
//Constants used for promotion (as used in the xboard protocol)
|
||||
public static final char KING_CHAR='k';
|
||||
public static final char QUEEN_CHAR='q';
|
||||
public static final char BISHOP_CHAR='b';
|
||||
public static final char KNIGHT_CHAR='n';
|
||||
public static final char ROOK_CHAR='r';
|
||||
|
||||
/****************
|
||||
* PRIVATE DATA *
|
||||
****************/
|
||||
|
||||
private static int pieceNumber;
|
||||
|
||||
/***************
|
||||
* CONSTRUCTOR *
|
||||
***************/
|
||||
|
||||
public Piece(int piece) {
|
||||
pieceNumber = piece;
|
||||
}
|
||||
|
||||
/***********
|
||||
* METHODS *
|
||||
***********/
|
||||
|
||||
public int getColor() {
|
||||
return pieceNumber%2; //cf declaration of BLACK and WHITE above and the pieces above.
|
||||
}
|
||||
|
||||
public int getPieceNumber() {
|
||||
return pieceNumber;
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import suicideChess.Square.NotAValidSquare;
|
||||
/**
|
||||
* @author djib
|
||||
*
|
||||
* The purpose of this file is to test the Moves.java class
|
||||
* The purpose of this file is to test the Moves.java class and the BitBoard.java class
|
||||
*
|
||||
* $LastChangedDate$
|
||||
* $LastChangedRevision$
|
||||
@ -21,13 +21,31 @@ public class TestMoves {
|
||||
public static void main(String[] args) {
|
||||
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
BitBoardTest bitboard = new BitBoardTest();
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
String move = moveInput.readLine();
|
||||
/**
|
||||
* Test of Move and Square class
|
||||
*/
|
||||
//String move = moveInput.readLine();
|
||||
//(new MoveTest(move)).display();
|
||||
System.out.println((new BitBoard().squareToBitboardSquare(new Square(move))));
|
||||
//System.out.println((new BitBoard().squareToBitboardSquare(new Square(move))));
|
||||
//new SquareTest(move).display();
|
||||
//System.out.println(new Square(move));
|
||||
|
||||
/**
|
||||
* Test of the add function in class BitBoard
|
||||
*/
|
||||
|
||||
String whereToAdd = moveInput.readLine();
|
||||
if (whereToAdd.startsWith("rm ")) {
|
||||
bitboard.rm(new Square(whereToAdd.substring(3)), new Piece(Piece.BLACK_PAWN));
|
||||
} else {
|
||||
bitboard.add(new Square(whereToAdd), new Piece(Piece.BLACK_PAWN));
|
||||
}
|
||||
bitboard.display(Piece.BLACK_PAWN);
|
||||
|
||||
} catch (Exception err) {
|
||||
System.out.println(err);
|
||||
break;
|
||||
@ -66,4 +84,30 @@ class SquareTest extends Square {
|
||||
System.out.println(" ");
|
||||
System.out.println(" ");
|
||||
}
|
||||
}
|
||||
|
||||
class BitBoardTest extends BitBoard {
|
||||
public void display(int whatToDisplay) {
|
||||
System.out.println(bitBoards[whatToDisplay]);
|
||||
for (int file = NB_OF_FILES; file >= 1; file--) {
|
||||
System.out.println("+-+-+-+-+-+-+-+-+");
|
||||
String display = "|";
|
||||
for (int rank=1; rank <= NB_OF_RANKS; rank++) {
|
||||
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
|
||||
if ((bitBoards[whatToDisplay] & mask)==0) {
|
||||
display+=" |";
|
||||
} else {
|
||||
display+="1|";
|
||||
}
|
||||
}
|
||||
System.out.println(display);
|
||||
}
|
||||
System.out.println("+-+-+-+-+-+-+-+-+");
|
||||
}
|
||||
public void add (Square square, Piece piece) {
|
||||
addPiece(square, piece);
|
||||
}
|
||||
public void rm (Square square, Piece piece) {
|
||||
removePiece(square, piece);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user