Rules for pawn. Every rule is implemented except 'prise en passant' and obligation to capture.
This commit is contained in:
@ -29,12 +29,136 @@ public class Rules {
|
||||
* @see Board
|
||||
*/
|
||||
public ArrayList<Move> 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<movesAllowed[movingPiece.getPieceType()][Board.squareToBitBoardSquare(fromSquare)][ray].length;
|
||||
moveNumber++) {
|
||||
|
||||
Square toSquare =
|
||||
toSquare =
|
||||
new Square(movesAllowed[movingPiece.getPieceType()]
|
||||
[Board.squareToBitBoardSquare(fromSquare)][ray][moveNumber]);
|
||||
|
||||
@ -54,7 +178,6 @@ public class Rules {
|
||||
break;
|
||||
}
|
||||
|
||||
Move validMove;
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE));
|
||||
|
||||
//add move to list of legal moves
|
||||
|
Reference in New Issue
Block a user