diff --git a/src/suicideChess/Board.java b/src/suicideChess/Board.java index a90a70d..787d2a6 100644 --- a/src/suicideChess/Board.java +++ b/src/suicideChess/Board.java @@ -57,6 +57,9 @@ public class Board { protected long bitBoards[]; + private boolean enPassant=false; //is there an 'en passant pawn' on the board + private Square enPassantSquare; + /*=============* @@ -122,7 +125,11 @@ public class Board { public void doMove(Move move) throws NoPieceOnSquare { if (move.isCaptureMove()) { - removePiece(move.toSquare(), move.getCapturedPiece()); + if (move.isEnPassant()) { + removePiece(move.getEnPassantSquare(), move.getCapturedPiece()); + } else { + removePiece(move.toSquare(), move.getCapturedPiece()); + } } removePiece(move.fromSquare(), move.getMovingPiece()); if (move.isPromotionMove()) { @@ -130,6 +137,14 @@ public class Board { } else { addPiece(move.toSquare(), move.getMovingPiece()); } + + this.enPassant=false; + if (move.enablesEnPassant()) { + enPassant=true; + enPassantSquare=move.getEnPassantSquare(); + } + + } /** @@ -171,6 +186,39 @@ public class Board { return square.getFileNb() -1 + (square.getRank()-1)*NB_OF_FILES; } +// /** +// * This function is used to set the table to an state in which a {@link Square} is an 'en passant' Square +// * @param Square The 'en passant' Square +// * @see Square +// */ +// public void setEnPassant(Square square) { +// enPassant=true; +// enPassantSquare=square; +// } +// +// /** +// * This function is used to set the table back to a state with no 'en passant' Squares. +// */ +// public void clearEnPassant () { +// enPassant=false; +// } + + /** + * This function returns a boolean saying if the board is in an 'en passant' state. + * @return boolean + */ + public boolean isEnPassant() { + return enPassant; + } + + /** + * This function is used to return the 'en passant' Square. Don't use it unless table is in an 'en passant' state. + * @return Square + * @see Square + */ + public Square getEnPassantSquare() { + return enPassantSquare; + } /** * This function can be used to display the board @@ -178,8 +226,8 @@ public class Board { */ public void display(){ for (int file = NB_OF_FILES; file >= 1; file--) { - System.out.println("+---+---+---+---+---+---+---+---+"); - String display = "| "; + System.out.println(" +---+---+---+---+---+---+---+---+"); + String display = file+" | "; for (int rank=1; rank <= NB_OF_RANKS; rank++) { boolean displayedSomething = false; long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS]; @@ -237,7 +285,8 @@ public class Board { } System.out.println(display); } - System.out.println("+---+---+---+---+---+---+---+---+"); + System.out.println(" +---+---+---+---+---+---+---+---+"); + System.out.println(" a b c d e f g h"); } diff --git a/src/suicideChess/Move.java b/src/suicideChess/Move.java index 5605dd0..9694ed2 100644 --- a/src/suicideChess/Move.java +++ b/src/suicideChess/Move.java @@ -25,6 +25,12 @@ public class Move { private boolean isCapture; private Piece capturePiece; + + //the following are used for two things : + //moves that enable the 'en Passant' status of the board + //and 'en passant' captures. + private boolean isEnPassant=false; + private Square enPassantSquare; public class NotAValidMoveException extends Exception { /** @@ -114,9 +120,44 @@ public class Move { } else { isPromotion = false; } - } + + /** + * This is the constructor of the class. + * It should be used only for moves that enable or are 'en passant' capture. + * + * @param fromSquare The {@link Square} to move from. + * @param toSquare The {@link Square} to move to. + * @param enPassantSquare The 'en passant' {@link Square}. + * @param movingPiece The {@link Piece} to be moved. + * @param isCapture This boolean is used to differenciate 'en passant' captures from moves that simply enable 'en passant' capture. + * @throws NotAValidSquare If Squares are not valid. + * @see Square + * @see Piece + */ + public Move(Square fromSquare, Square toSquare, Square enPassantSquare, Piece movingPiece, boolean isCapture) + throws NotAValidSquare { + + this.fromSquare = fromSquare; + this.toSquare = toSquare; + + this.movingPiece = movingPiece; + + this.isCapture = isCapture; + if (isCapture) { + if (movingPiece.getColor()==Piece.WHITE) { + this.capturePiece=new Piece(Piece.BLACK_PAWN); + } else { + this.capturePiece=new Piece(Piece.WHITE_PAWN); + } + } + + isPromotion = false; + isEnPassant = true; + this.enPassantSquare = enPassantSquare; + } + /*===========================================* * Those two constructors are no longer used * @@ -235,6 +276,28 @@ public class Move { } /** + * Returns a boolean saying is a Move makes 'en passant' move possible. + */ + public boolean enablesEnPassant() { + return (!isCapture)&&isEnPassant; + } + + /** + * Returns a boolean saying is a Move is an 'en passant' move. + */ + public boolean isEnPassant() { + return (isCapture)&&isEnPassant; + } + + /** + * Returns the enPassant Square for a move. + */ + public Square getEnPassantSquare() { + return enPassantSquare; + } + + + /** * Converts a Move into a String. * @return String */ @@ -253,9 +316,9 @@ public class Move { System.out.println("To square: "+toSquare()); System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece().getPieceNumber() : "No Promotion")); System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().getPieceNumber() : "No Capture")); + System.out.println((enablesEnPassant() ? "En-Passant: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank() : "No en-passant")); System.out.println("=============="); System.out.println(" "); - System.out.println(" "); } diff --git a/src/suicideChess/Rules.java b/src/suicideChess/Rules.java index c838d51..90fb673 100644 --- a/src/suicideChess/Rules.java +++ b/src/suicideChess/Rules.java @@ -46,6 +46,11 @@ public class Rules { boolean captureLeft=true; Square toCaptureLeftSquare=null; Square toCaptureRightSquare=null; + + boolean captureEnPassantRight=board.isEnPassant(); + boolean captureEnPassantLeft=board.isEnPassant(); + Square captureEnPassantLeftSquare=null; + Square captureEnPassantRightSquare=null; switch (movingPiece.getColor()) { case Piece.WHITE: @@ -53,36 +58,77 @@ public class Rules { if (fromSquare.getRank()==2) { bigJump=true; toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2); - } else if (toSquare.getRank()==8) { + } else if (toSquare.getRank()==Board.NB_OF_RANKS) { promotion=true; } - toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1); - toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()+1); + if(fromSquare.getFileNb()>1) { //make sure not to go out of the board + toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1); + if(captureEnPassantLeft) { + captureEnPassantLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()); + if (!toCaptureLeftSquare.isEqual(board.getEnPassantSquare())) { + captureEnPassantLeft=false; + } + } + } else { + captureLeft = false; + } + if(fromSquare.getFileNb()1) { //make sure not to go out of the board + toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()-1); + if(captureEnPassantLeft) { + captureEnPassantLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()); + if (!toCaptureLeftSquare.isEqual(board.getEnPassantSquare())) { + captureEnPassantLeft=false; + } + } + } else { + captureLeft = false; + } + if(fromSquare.getFileNb() allLegalMoves = + rules.legalMovesFromSquare(new Square(whatMove.substring(0,2)),bitboard); + int foundMoveIndex = -1; + for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) { + if (allLegalMoves.get(moveIndex).toSquare().isEqual(theMove.toSquare())) { + foundMoveIndex=moveIndex; + break; + } + } + if (foundMoveIndex == -1) { + System.out.println("This move is not valid. Please type 'hint "+whatMove.substring(0,2) + +"' to see available moves."); + } else { + allLegalMoves.get(foundMoveIndex).display(); + bitboard.doMove(allLegalMoves.get(foundMoveIndex)); + bitboard.display(); + } + /*if (playerColor == Piece.WHITE) { playerColor = Piece.BLACK; System.out.println("Black: ");