Rules for pawn. Every rule is implemented except 'prise en passant' and obligation to capture.

This commit is contained in:
2006-01-12 17:32:52 +00:00
parent d1a82db0cf
commit eb0e593ee1
4 changed files with 144 additions and 10 deletions

View File

@ -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) {

View File

@ -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

View File

@ -61,6 +61,21 @@ public class Square {
rank = bitboardSquare/Board.NB_OF_FILES + 1;
}
/**
* Construcs a Square given a rank and a file number
* @param file A file number (1 to Board.NB_OF_FILES)
* @param rank A rank number (1 to Board.NB_OF_RANKS)
*/
public Square(int file, int rank) throws NotAValidSquare {
if ((file <= 0) || (file > 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.
* @return char representing the file

View File

@ -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<Move> allLegalMoves =