diff --git a/SuicideChess/TODO b/SuicideChess/TODO new file mode 100644 index 0000000..50879bc --- /dev/null +++ b/SuicideChess/TODO @@ -0,0 +1 @@ +Capture is not implemented in Move.class \ No newline at end of file diff --git a/SuicideChess/src/suicideChess/Board.java b/SuicideChess/src/suicideChess/Board.java new file mode 100644 index 0000000..f29016a --- /dev/null +++ b/SuicideChess/src/suicideChess/Board.java @@ -0,0 +1,175 @@ +package suicideChess; + +import suicideChess.Square.NotAValidSquare; + +/** + * @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; iBoard.NB_OF_FILES) { + throw new NotAValidSquare(square); + } + if (rank<1 || rank>Board.NB_OF_RANKS) { + throw new NotAValidSquare(square); + } + } + } + + public char getFile() { + return file; + } + public int getRank() { + return rank; + } + public int getFileNb() { + return fileNb; + } + + public String toString() { + return String.valueOf(file)+String.valueOf(rank); + } +} diff --git a/SuicideChess/src/suicideChess/SuicideChess.java b/SuicideChess/src/suicideChess/SuicideChess.java new file mode 100644 index 0000000..1045862 --- /dev/null +++ b/SuicideChess/src/suicideChess/SuicideChess.java @@ -0,0 +1,36 @@ +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; + //does Square.class checks if the strings are valid (is "z9" a valid square ? + public static final boolean SQUARE_CHECK_INVALID = true; + + + + /***************** + * MAIN FUNCTION * + *****************/ + public static void main(String[] args) { + TestMoves.main(args); + } + +} diff --git a/SuicideChess/src/tests/TestMoves.java b/SuicideChess/src/tests/TestMoves.java new file mode 100644 index 0000000..ef2fd4e --- /dev/null +++ b/SuicideChess/src/tests/TestMoves.java @@ -0,0 +1,217 @@ +package tests; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +import suicideChess.*; +import suicideChess.Board.NoPieceOnSquare; +import suicideChess.Move.NotAValidMoveException; +import suicideChess.Square.NotAValidSquare; + +/** + * @author djib + * + * The purpose of this file is to test the Moves.java class and the BitBoard.java class + * + * $LastChangedDate$ + * $LastChangedRevision$ + * $LastChangedBy$ + */ + +public class TestMoves { + + public static void main(String[] args) { + BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in)); + + try { + BitBoardTest bitboard = new BitBoardTest(); + bitboard.display(); + + int playerColor = Piece.WHITE; + + while (true) { + try { + /** + * Test of Move and Square class + */ + //String move = moveInput.readLine(); + //(new MoveTest(move)).display(); + //System.out.println((new BitBoard().squareToBitboardSquare(new Square(move)))); + //new SquareTest(move).display(); + //System.out.println(new Square(move)); + + + /** + * Test of the moves + * Moves must be displayed in the following form : + * PieceLetter move(4lettes) PieceCaptureCode(2digits) + * example p e4d5 p + * + * Not very convenient but it is for test purposes :-P + */ + + String whatMove= moveInput.readLine(); + MoveTest theMove; + Piece movePiece = new Piece(whatMove.toCharArray()[0],playerColor); + if (whatMove.length()==6) { + theMove = new MoveTest(whatMove.substring(2,6),movePiece); + } else { + Piece capturePiece; + if (playerColor==Piece.WHITE) { + capturePiece = new Piece(whatMove.toCharArray()[7],Piece.BLACK); + theMove = new MoveTest(whatMove.substring(2,6),movePiece,capturePiece); + } else { + capturePiece = new Piece(whatMove.toCharArray()[7],Piece.WHITE); + theMove = new MoveTest(whatMove.substring(2,6),movePiece,capturePiece); + } + } + theMove.display(); + bitboard.doMove(theMove); + bitboard.display(); + + if (playerColor == Piece.WHITE) { + playerColor = Piece.BLACK; + } else { + playerColor = Piece.WHITE; + } + + + } catch (Exception err) { + System.out.println(err); + break; + } + } + } catch (NotAValidSquare e) { + e.printStackTrace(); + } + } +} + +class MoveTest extends Move { + public MoveTest(String move, Piece pieceToMove) throws NotAValidMoveException, NotAValidSquare { + super(move, pieceToMove); + } + public MoveTest(String move, Piece pieceToMove, Piece pieceToCapture) throws NotAValidMoveException, NotAValidSquare { + super(move, pieceToMove, pieceToCapture); + } + + public void display() { + System.out.println(" "); + System.out.println("==== Move ===="); + System.out.println("Move: "+getMovingPiece().getPieceNumber()); + System.out.println("From square:"+fromSquare()); + System.out.println("To square: "+toSquare()); + System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece() : "No Promotion")); + System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().getPieceNumber() : "No Capture")); + System.out.println("=============="); + System.out.println(" "); + System.out.println(" "); + } +} + + +/* +class SquareTest extends Square { + public SquareTest(String square) throws NotAValidSquare { + super(square); + } + public void display() { + System.out.println(" "); + System.out.println("==== Square ===="); + System.out.println("Rank: "+getRank()); + System.out.println("File: "+getFile()); + System.out.println("FileNb: "+getFileNb()); + System.out.println("==============="); + System.out.println(" "); + System.out.println(" "); + } +} + +*/ + +class BitBoardTest extends Board { + public BitBoardTest() throws NotAValidSquare { + super(); + } + + public void display(){ + for (int file = NB_OF_FILES; file >= 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 boolean displayPiece(int whatToDisplay, long mask) { + if ((bitBoards[whatToDisplay] & mask)==0) { + return false; + } else { + return true; + } + } + /* + public void add (Square square, Piece piece) { + addPiece(square, piece); + } + public void rm (Square square, Piece piece) throws NoPieceOnSquare { + removePiece(square, piece); + } + */ + +} \ No newline at end of file diff --git a/SuicideChess/src/tests/TestXBoardProtocol.java b/SuicideChess/src/tests/TestXBoardProtocol.java new file mode 100644 index 0000000..7211d69 --- /dev/null +++ b/SuicideChess/src/tests/TestXBoardProtocol.java @@ -0,0 +1,65 @@ +package tests; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.logging.*; + +/** + * @author djib + * + * This class is used to test the XBoard protocol. + * + * $LastChangedDate$ + * $LastChangedRevision$ + * $LastChangedBy$ + * + */ +public class TestXBoardProtocol { + public static void main(String[] args) { + Logger logger = Logger.getLogger(""); + try { + FileHandler logFile = new FileHandler("%t/TestXBoardProtocol.txt"); + logFile.setFormatter(new SimpleFormatter()); + logger.addHandler(logFile); + } catch (Exception e) { + } + + BufferedReader xBoardInput = new BufferedReader(new InputStreamReader(System.in)); + + + //done=1 is here to tell that the program has finish requesting features + System.out.println("feature myname=\"djib's Suicide Chess\" sigint=0 sigterm=0 variant=\"suicide\" done=1"); + + while (true) { + try { + String xBoardCommand = xBoardInput.readLine(); + logger.info(xBoardCommand); + System.err.println(xBoardCommand); + if (xBoardCommand.startsWith("quit")) { + logger.info(">quit;"); + break; + } + if (xBoardCommand.startsWith("otim")) { + logger.info(">Error"); + System.out.println("Error (unknown command): time"); + } + if (xBoardCommand.startsWith("e2e4")) { + logger.info(">move b8b8"); + System.out.println("move b8c6"); + } + if (xBoardCommand.startsWith("d2d4")) { + logger.info(">move d7d5"); + System.out.println("move d7d5"); + } + if (xBoardCommand.startsWith("d2d3")) { + logger.info(">invalid move d2d3"); + System.out.println("Illegal move (Don't like it.): d2d3"); + } + + + } catch (Exception err) { + logger.info(err.toString()); + break; + } + } + } +}