From 08c973a4529cd93d321aaa0786e9243446ab3722 Mon Sep 17 00:00:00 2001 From: djib Date: Thu, 5 Jan 2006 16:37:20 +0000 Subject: [PATCH] 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. --- TODO | 3 +- .../{BitBoard.java => Board.java} | 44 +++++++++++++++---- src/suicideChess/SuicideChess.java | 34 ++++++++++++++ src/tests/TestMoves.java | 16 +++---- 4 files changed, 80 insertions(+), 17 deletions(-) rename src/suicideChess/{BitBoard.java => Board.java} (63%) create mode 100644 src/suicideChess/SuicideChess.java diff --git a/TODO b/TODO index 1f463af..b884925 100644 --- a/TODO +++ b/TODO @@ -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. \ No newline at end of file diff --git a/src/suicideChess/BitBoard.java b/src/suicideChess/Board.java similarity index 63% rename from src/suicideChess/BitBoard.java rename to src/suicideChess/Board.java index 8c6f47d..d58db8c 100644 --- a/src/suicideChess/BitBoard.java +++ b/src/suicideChess/Board.java @@ -15,7 +15,7 @@ package suicideChess; * $LastChangedBy$ */ -public class BitBoard { +public class Board { /************* * CONSTANTS * @@ -26,7 +26,16 @@ public class BitBoard { 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_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 * ***************/ - public BitBoard() { + public Board() { bitBoards = new long[NB_OF_BITBOARDS]; } @@ -61,13 +70,13 @@ public class BitBoard { * METHODS * ***********/ - public long getBitBoard(int bitboard_number) { + private long getBitBoard(int 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)]; } - protected void removePiece(Square square, Piece piece) { + 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. - bitBoards[piece.getPieceNumber()] ^= mapSquaresToBits[squareToBitBoardSquare(square)]; + + 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; + } + } } \ No newline at end of file diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java new file mode 100644 index 0000000..10be4fb --- /dev/null +++ b/src/suicideChess/SuicideChess.java @@ -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); + } + +} diff --git a/src/tests/TestMoves.java b/src/tests/TestMoves.java index 556c8b4..390da70 100644 --- a/src/tests/TestMoves.java +++ b/src/tests/TestMoves.java @@ -4,6 +4,7 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import suicideChess.*; +import suicideChess.Board.NoPieceOnSquare; import suicideChess.Square.NotAValidSquare; /** @@ -86,28 +87,27 @@ class SquareTest extends Square { } } -class BitBoardTest extends BitBoard { +class BitBoardTest extends Board { public void display(int whatToDisplay) { - System.out.println(bitBoards[whatToDisplay]); for (int file = NB_OF_FILES; file >= 1; file--) { - System.out.println("+-+-+-+-+-+-+-+-+"); - String display = "|"; + 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+=" |"; + display+=" | "; } else { - display+="1|"; + display+="1 | "; } } System.out.println(display); } - System.out.println("+-+-+-+-+-+-+-+-+"); + System.out.println("+---+---+---+---+---+---+---+---+"); } public void add (Square square, Piece piece) { addPiece(square, piece); } - public void rm (Square square, Piece piece) { + public void rm (Square square, Piece piece) throws NoPieceOnSquare { removePiece(square, piece); } } \ No newline at end of file