diff --git a/src/suicideChess/Board.java b/src/suicideChess/Board.java index bcedf42..f162993 100644 --- a/src/suicideChess/Board.java +++ b/src/suicideChess/Board.java @@ -35,7 +35,10 @@ public class Board { public class NoPieceOnSquare extends Exception { NoPieceOnSquare(String s) { super(s); }; } - + @SuppressWarnings("serial") + public class UnableToParseFENStringException extends Exception { + UnableToParseFENStringException(String s) { super(s); }; + } /** * Value returned by the evaluation function when White wins */ @@ -145,6 +148,73 @@ public class Board { this.currentPlayer = bitboard.currentPlayer; } + /** + * This constructor is used to create a board with a string in FEN notation + * @param command A string representing the command + * @return A {@link Board} corresponding to the FEN string + * @throws NotAValidSquare + * @throws UnableToParseFENStringException + * @see Board + */ + public Board(String command) throws NotAValidSquare, UnableToParseFENStringException { + String[] result = command.split("/|\\s"); + /* + * After splitting + * 0->rank 8 + * 1->rank 7 + * ... + * 7->rank 1 + * 8->color on move + * 9->castling (I don't care about this) + * 10->enpassant Square (if any), otherwise '-' + * 11->count of plies since the last pawn advance or capturing move + * 12->fullmove number + */ + + bitBoards = new long[NB_OF_BITBOARDS]; + + if (result.length!=13) { + throw new UnableToParseFENStringException(command); + } + + if (result[8].equals("b")) { + currentPlayer=Piece.BLACK; + } else { + currentPlayer=Piece.WHITE; + } + if (!result[10].equals("-")) { + enPassant = true; + enPassantSquare = new Square(result[10]); + } else { + enPassant = false; + enPassantSquare = new Square("a1"); //otherwise it is null + } + + numberOfBlackPieces = 0; + numberOfWhitePieces = 0; + + for(int split=0; split < 8; split++) { + int offset=0; + char[] rowToParse = result[split].toCharArray(); + for (int character=0; character(); + legalMovesCapture = new ArrayList(); + + Square square; + for(int squareNb = 0; squareNb < Board.NB_OF_SQUARES; squareNb++) { + square = new Square(squareNb); + if (board.isEmpty(square, new Piece(board.getCurrentPlayer()))) { + continue; + } + if ((legalMovesNonCapture.size()!=0)||(legalMovesCapture.size()!=0)) { + return true; //found a legal move + } + } + return false; + } + + /** * Computes the possible moves from a given {@link Square} * according to the current status of the {@link Board} * @param fromSquare The square from which the move must start * @param board The current board position. + * @param fast If this boolean is set to true then the program only searches if there is ONE legal move. + * @return boolean Stating if there is or not a legal move. * @throws NotAValidSquare If the program throws this exception then there is a bug. * @throws UnexpectedError This should never happen. * @see Square * @see Board */ - public static void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare { + private static void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare { Move validMove; Square toSquare; Piece movingPiece = board.getPiece(fromSquare); diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java index 574ef16..e5276e4 100644 --- a/src/suicideChess/SuicideChess.java +++ b/src/suicideChess/SuicideChess.java @@ -134,7 +134,7 @@ public class SuicideChess { try { String whatMove= moveInput.readLine(); boolean playedALegalMove = false; - + int xBoardCommand = XBoardProtocol.getCommand(whatMove); switch (xBoardCommand) { @@ -185,6 +185,11 @@ public class SuicideChess { case XBoardProtocol.NOPOST: post=false; break; + case XBoardProtocol.SETBOARD: + bitboard=new Board(whatMove.substring(9)); + if(ASCII_GAME) + bitboard.display(); + break; case XBoardProtocol.UNKNOWN: if (acceptedUsermove) { System.out.println("Error (unknown command): "+whatMove); @@ -315,6 +320,5 @@ public class SuicideChess { break; } } - } - + } } \ No newline at end of file diff --git a/src/suicideChess/XBoardProtocol.java b/src/suicideChess/XBoardProtocol.java index 9f9a1ae..aec8542 100644 --- a/src/suicideChess/XBoardProtocol.java +++ b/src/suicideChess/XBoardProtocol.java @@ -82,6 +82,10 @@ public class XBoardProtocol { * Xboard asks NOT to display thinking output */ public static final int NOPOST = 16; + /** + * XBoard changes the board position + */ + public static final int SETBOARD = 17; /** * Unknown command */ @@ -98,7 +102,7 @@ public class XBoardProtocol { System.out.println("feature myname=\"djib's Suicide Chess\""); System.out.println("feature sigint=0 sigterm=0"); //do not interrupt me System.out.println("feature variants=\"suicide\""); - System.out.println("feature ping=1"); + System.out.println("feature ping=1 setboard=1"); System.out.println("feature time=0 draw=0 reuse=0 analyze=0 colors=0"); System.out.println("feature done=1"); if (SuicideChess.ASCII_GAME) @@ -154,9 +158,10 @@ public class XBoardProtocol { return POST; } else if (command.equals("nopost")) { return NOPOST; + } else if (command.startsWith("setboard")) { + return SETBOARD; } return UNKNOWN; } - }