Version 0.1.9
Rules are checked and en passant works. Does not check if black or white is playing. Does not check multiple possibilities for promotion.
This commit is contained in:
@ -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()<Board.NB_OF_FILES) {
|
||||
toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()+1);
|
||||
if(captureEnPassantRight) {
|
||||
captureEnPassantRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank());
|
||||
if (!toCaptureRightSquare.isEqual(board.getEnPassantSquare())) {
|
||||
captureEnPassantRight=false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
captureRight = false;
|
||||
}
|
||||
break;
|
||||
case Piece.BLACK:
|
||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1);
|
||||
if (fromSquare.getRank()==7) {
|
||||
if (fromSquare.getRank()==(Board.NB_OF_RANKS-1)) {
|
||||
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);
|
||||
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()<Board.NB_OF_FILES) {
|
||||
toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()-1);
|
||||
if(captureEnPassantRight) {
|
||||
captureEnPassantRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank());
|
||||
if (!toCaptureRightSquare.isEqual(board.getEnPassantSquare())) {
|
||||
captureEnPassantRight=false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
captureRight = false;
|
||||
}
|
||||
break;
|
||||
default: //this should never happen
|
||||
return legalMoves;
|
||||
}
|
||||
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
|
||||
normalMove= false;
|
||||
normalMove= false; //cannot move forward if there is a piece
|
||||
}
|
||||
if ((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR)
|
||||
|| (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor())) {
|
||||
if (captureLeft&&
|
||||
((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");
|
||||
if (captureRight&&
|
||||
((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR)
|
||||
|| (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor()))) {
|
||||
captureRight = false;
|
||||
}
|
||||
if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) {
|
||||
@ -146,10 +192,22 @@ public class Rules {
|
||||
legalMoves.add(validMove);
|
||||
}
|
||||
if (bigJump) {
|
||||
validMove = new Move(fromSquare, toBigJumpSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE));
|
||||
//create an 'en-passant' status
|
||||
validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false);
|
||||
legalMoves.add(validMove);
|
||||
}
|
||||
if (captureLeft) {
|
||||
if (captureEnPassantLeft) {
|
||||
//create an 'en-passant' move
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true);
|
||||
legalMoves.add(validMove);
|
||||
}
|
||||
if (captureEnPassantRight) {
|
||||
//create an 'en-passant' move
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, captureEnPassantRightSquare, movingPiece, true);
|
||||
legalMoves.add(validMove);
|
||||
}
|
||||
|
||||
if (captureLeft) {
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
|
||||
legalMoves.add(validMove);
|
||||
}
|
||||
|
Reference in New Issue
Block a user