diff --git a/src/suicideChess/Move.java b/src/suicideChess/Move.java index 8ecac73..5605dd0 100644 --- a/src/suicideChess/Move.java +++ b/src/suicideChess/Move.java @@ -65,6 +65,9 @@ public class Move { } movingPiece = board.getPiece(fromSquare()); + if(movingPiece.getPieceNumber()==Piece.NONE) { + throw new NotAValidMoveException("No piece to move on "+fromSquare.getFile()+fromSquare.getRank()); + } capturePiece = board.getPiece(toSquare()); if (capturePiece.getPieceNumber()!=Piece.NONE) { diff --git a/src/suicideChess/Rules.java b/src/suicideChess/Rules.java index b312426..c838d51 100644 --- a/src/suicideChess/Rules.java +++ b/src/suicideChess/Rules.java @@ -29,12 +29,136 @@ public class Rules { * @see Board */ public ArrayList legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare { + Move validMove; + Square toSquare; + Piece movingPiece = board.getPiece(fromSquare); switch (movingPiece.getPieceType()) { case Piece.NONE: break; case Piece.PAWN: - //@TODO + boolean normalMove=true; //is a normal (straight) move allowed + boolean promotion=false; + boolean bigJump=false; //if the pawn is allowed to move two squares + Square toBigJumpSquare=null; //the square for the "big jump" + + boolean captureRight=true; + boolean captureLeft=true; + Square toCaptureLeftSquare=null; + Square toCaptureRightSquare=null; + + switch (movingPiece.getColor()) { + case Piece.WHITE: + toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1); + if (fromSquare.getRank()==2) { + bigJump=true; + toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2); + } else if (toSquare.getRank()==8) { + promotion=true; + } + toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1); + toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()+1); + break; + case Piece.BLACK: + toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1); + if (fromSquare.getRank()==7) { + bigJump=true; + toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2); + } else if (toSquare.getRank()==1) { + promotion=true; + } + toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()-1); + toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()-1); + break; + default: //this should never happen + return legalMoves; + } + if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) { + normalMove= false; + } + if ((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR) + || (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor())) { + captureLeft = false; + } + if ((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR) + || (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor())) { + System.out.println("Here"); + captureRight = false; + } + if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) { + bigJump=false; + } + if (promotion) { + if (normalMove) { + //to Queen + validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to King + validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Knight + validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Bishop + validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Rook + validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + } + if (captureLeft) { + //to Queen + validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to King + validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Knight + validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Bishop + validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Rook + validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + } + if (captureRight) { + //to Queen + validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to King + validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Knight + validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Bishop + validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + //to Rook + validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); + legalMoves.add(validMove); + } + } else { + if (normalMove) { + validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE)); + legalMoves.add(validMove); + } + if (bigJump) { + validMove = new Move(fromSquare, toBigJumpSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE)); + legalMoves.add(validMove); + } + if (captureLeft) { + validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE)); + legalMoves.add(validMove); + } + if (captureRight) { + validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE)); + legalMoves.add(validMove); + } + } + break; default: //look for all valid moves @@ -45,7 +169,7 @@ public class Rules { moveNumber Board.NB_OF_FILES) || (rank <= 0) || (rank > Board.NB_OF_RANKS)) { + throw new NotAValidSquare("file: "+file+" - rank: "+rank); + } + this.fileNb = file; + this.file = (char)(this.fileNb + ((int)'a') - 1); + this.rank = rank; + } + /** * Returns the file of a Square. diff --git a/src/suicideChess/SuicideChess.java b/src/suicideChess/SuicideChess.java index 3356545..a42e806 100644 --- a/src/suicideChess/SuicideChess.java +++ b/src/suicideChess/SuicideChess.java @@ -57,13 +57,6 @@ public class SuicideChess { break; } - if (whatMove.startsWith("move")) { - Move move = new Move(new Square(0), new Square(1), new Piece(2), new Piece(3), new Piece(Piece.NONE)); - move.display(); - continue; - } - - if (whatMove.startsWith("hint")) { Rules rules = new Rules(); ArrayList allLegalMoves =