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

3
TODO
View File

@ -1 +1,2 @@
Make sure we don't remove a piece that don't exist. Create a variable called SECURITY.
If SECURITY = false then many test that are 'a priori' useless are not done.

View File

@ -15,7 +15,7 @@ package suicideChess;
* $LastChangedBy$ * $LastChangedBy$
*/ */
public class BitBoard { public class Board {
/************* /*************
* CONSTANTS * * CONSTANTS *
@ -26,7 +26,16 @@ public class BitBoard {
public static final int NB_OF_FILES = 8; public static final int NB_OF_FILES = 8;
public static final int NB_OF_SQUARES = NB_OF_RANKS*NB_OF_FILES; public static final int NB_OF_SQUARES = NB_OF_RANKS*NB_OF_FILES;
public static final int NB_OF_BITBOARDS = 14; 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); };
}
/******** /********
@ -51,7 +60,7 @@ public class BitBoard {
* CONSTRUCTOR * * CONSTRUCTOR *
***************/ ***************/
public BitBoard() { public Board() {
bitBoards = new long[NB_OF_BITBOARDS]; bitBoards = new long[NB_OF_BITBOARDS];
} }
@ -61,13 +70,13 @@ public class BitBoard {
* METHODS * * METHODS *
***********/ ***********/
public long getBitBoard(int bitboard_number) { private long getBitBoard(int bitboard_number) {
return bitBoards[bitboard_number]; return bitBoards[bitboard_number];
} }
public void updateBitBoards(String move) { private void updateBitBoards(String move) {
//TODO
} }
@ -87,14 +96,33 @@ public class BitBoard {
bitBoards[piece.getColor()] |= mapSquaresToBits[squareToBitBoardSquare(square)]; bitBoards[piece.getColor()] |= mapSquaresToBits[squareToBitBoardSquare(square)];
} }
protected void removePiece(Square square, Piece piece) { protected void removePiece(Square square, Piece piece) throws NoPieceOnSquare {
//XOR : //XOR :
// 0 XOR a = a // 0 XOR a = a
// 1 XOR 0 = 1 !!! Don't remove a piece that don't exist !!! // 1 XOR 0 = 1 !!! Don't remove a piece that don't exist !!!
// 1 XOR 1 = 0 // 1 XOR 1 = 0
//remove Piece to corresponding bitboard. //remove Piece to corresponding bitboard.
if (SuicideChess.BITBOARD_REMOVEPIECE_CHECK_REMOVE) {
if (checkIsNotEmpty(square, piece)) {
bitBoards[piece.getPieceNumber()] ^= mapSquaresToBits[squareToBitBoardSquare(square)]; 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 //update the bitboard of all pieces of that color
bitBoards[piece.getColor()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];; 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;
}
}
} }

View File

@ -0,0 +1,34 @@
package suicideChess;
import tests.TestMoves;
/**
* @author djib
*
* Main File
*
* $LastChangedDate$
* $LastChangedRevision$
* $LastChangedBy$
*/
public class SuicideChess {
/**
* Those flags are used to perform extra checks during the debugging of the
* program. They may be safely all set to false once the program is stable.
* It should improve performance a lot.
*/
//does BitBoard.class removePiece function checks if removing piece is legal ?
public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = true;
/*****************
* MAIN FUNCTION *
*****************/
public static void main(String[] args) {
TestMoves.main(args);
}
}

View File

@ -4,6 +4,7 @@ import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import suicideChess.*; import suicideChess.*;
import suicideChess.Board.NoPieceOnSquare;
import suicideChess.Square.NotAValidSquare; import suicideChess.Square.NotAValidSquare;
/** /**
@ -86,11 +87,10 @@ class SquareTest extends Square {
} }
} }
class BitBoardTest extends BitBoard { class BitBoardTest extends Board {
public void display(int whatToDisplay) { public void display(int whatToDisplay) {
System.out.println(bitBoards[whatToDisplay]);
for (int file = NB_OF_FILES; file >= 1; file--) { for (int file = NB_OF_FILES; file >= 1; file--) {
System.out.println("+-+-+-+-+-+-+-+-+"); System.out.println("+---+---+---+---+---+---+---+---+");
String display = "| "; String display = "| ";
for (int rank=1; rank <= NB_OF_RANKS; rank++) { for (int rank=1; rank <= NB_OF_RANKS; rank++) {
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS]; long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
@ -102,12 +102,12 @@ class BitBoardTest extends BitBoard {
} }
System.out.println(display); System.out.println(display);
} }
System.out.println("+-+-+-+-+-+-+-+-+"); System.out.println("+---+---+---+---+---+---+---+---+");
} }
public void add (Square square, Piece piece) { public void add (Square square, Piece piece) {
addPiece(square, piece); addPiece(square, piece);
} }
public void rm (Square square, Piece piece) { public void rm (Square square, Piece piece) throws NoPieceOnSquare {
removePiece(square, piece); removePiece(square, piece);
} }
} }