Version 0.1.99
============== Everything seems to work fine for a two player game.
This commit is contained in:
@ -13,22 +13,58 @@ import suicideChess.Square.NotAValidSquare;
|
||||
|
||||
public class Rules {
|
||||
|
||||
private ArrayList<Move> legalMoves;
|
||||
private ArrayList<Move> legalMovesNonCapture;
|
||||
private ArrayList<Move> legalMovesCapture;
|
||||
|
||||
public class UnexpectedError extends Exception {
|
||||
/*
|
||||
* Generated by Eclipse
|
||||
*/
|
||||
private static final long serialVersionUID = 7448113740797323379L;
|
||||
|
||||
UnexpectedError(String s) { super(s); };
|
||||
}
|
||||
|
||||
|
||||
public Rules () {
|
||||
legalMoves = new ArrayList<Move>();
|
||||
legalMovesNonCapture = new ArrayList<Move>();
|
||||
legalMovesCapture = new ArrayList<Move>();
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Computes the possible moves according to the current status of the {@link Board} and the
|
||||
* color of the current player.
|
||||
* @param board The current board position.
|
||||
* @param color The color of the current player.
|
||||
* @throws NotAValidSquare If the program throws this exception then there is a bug.
|
||||
* @throws UnexpectedError This should never happen.
|
||||
* @see Square
|
||||
* @see Board
|
||||
*/
|
||||
public void legalMovesForPlayer(Board board, int color) throws NotAValidSquare, UnexpectedError {
|
||||
// Find it! There is only one king, so look for it and stop
|
||||
Square square;
|
||||
for(int squareNb = 0; squareNb < Board.NB_OF_SQUARES; squareNb++) {
|
||||
square = new Square(squareNb);
|
||||
if (board.isEmpty(square, new Piece(color))) {
|
||||
continue;
|
||||
}
|
||||
legalMovesFromSquare(square,board);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Computes the possible moves from a given {@link Square}
|
||||
* according to the current status of the {@link Board}
|
||||
* @param square The square from which the move must start
|
||||
* @param board The current board position.
|
||||
* @throws NotAValidSquare If the program throws this exception then there is a bug.
|
||||
* @throws UnexpectedError This should never happen.
|
||||
* @see Square
|
||||
* @see Board
|
||||
*/
|
||||
public ArrayList<Move> legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare {
|
||||
public void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare, UnexpectedError {
|
||||
Move validMove;
|
||||
Square toSquare;
|
||||
|
||||
@ -71,6 +107,7 @@ public class Rules {
|
||||
}
|
||||
} else {
|
||||
captureLeft = false;
|
||||
captureEnPassantLeft=false;
|
||||
}
|
||||
if(fromSquare.getFileNb()<Board.NB_OF_FILES) {
|
||||
toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()+1);
|
||||
@ -82,6 +119,7 @@ public class Rules {
|
||||
}
|
||||
} else {
|
||||
captureRight = false;
|
||||
captureEnPassantRight=false;
|
||||
}
|
||||
break;
|
||||
case Piece.BLACK:
|
||||
@ -102,6 +140,7 @@ public class Rules {
|
||||
}
|
||||
} else {
|
||||
captureLeft = false;
|
||||
captureEnPassantLeft=false;
|
||||
}
|
||||
if(fromSquare.getFileNb()<Board.NB_OF_FILES) {
|
||||
toCaptureRightSquare = new Square(fromSquare.getFileNb()+1, fromSquare.getRank()-1);
|
||||
@ -113,10 +152,11 @@ public class Rules {
|
||||
}
|
||||
} else {
|
||||
captureRight = false;
|
||||
captureEnPassantRight=false;
|
||||
}
|
||||
break;
|
||||
default: //this should never happen
|
||||
return legalMoves;
|
||||
default:
|
||||
throw new UnexpectedError("ERROR 01.");
|
||||
}
|
||||
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
|
||||
normalMove= false; //cannot move forward if there is a piece
|
||||
@ -138,82 +178,82 @@ public class Rules {
|
||||
if (normalMove) {
|
||||
//to Queen
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesNonCapture.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);
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMovesNonCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesNonCapture.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);
|
||||
legalMovesCapture.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);
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.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);
|
||||
legalMovesCapture.add(validMove);
|
||||
//to King
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Knight
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Bishop
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
//to Rook
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
} else {
|
||||
if (normalMove) {
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (bigJump) {
|
||||
//create an 'en-passant' status
|
||||
validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false);
|
||||
legalMoves.add(validMove);
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
if (captureEnPassantLeft) {
|
||||
//create an 'en-passant' move
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true);
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
if (captureEnPassantRight) {
|
||||
//create an 'en-passant' move
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, captureEnPassantRightSquare, movingPiece, true);
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
|
||||
if (captureLeft) {
|
||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
if (captureRight) {
|
||||
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE));
|
||||
legalMoves.add(validMove);
|
||||
legalMovesCapture.add(validMove);
|
||||
}
|
||||
}
|
||||
|
||||
@ -239,15 +279,44 @@ public class Rules {
|
||||
validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE));
|
||||
|
||||
//add move to list of legal moves
|
||||
legalMoves.add(validMove);
|
||||
if (validMove.isCaptureMove()) {
|
||||
legalMovesCapture.add(validMove);
|
||||
break; //don't go further in the ray
|
||||
} else {
|
||||
legalMovesNonCapture.add(validMove);
|
||||
}
|
||||
|
||||
}//end for moveNumber
|
||||
}//end for ray
|
||||
} //end case
|
||||
|
||||
|
||||
return legalMoves;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function return the current status of the ArrayList<Move> of all legal {@link Moves}
|
||||
* without a capture.
|
||||
* You need to call legalMovesFromSquare before calling this function.
|
||||
* @return ArrayList<Move>
|
||||
*/
|
||||
public ArrayList<Move> getLegalMovesNonCapture() {
|
||||
return legalMovesNonCapture;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function return the current status of the ArrayList<Move> of all legal {@link Moves}
|
||||
* with a capture.
|
||||
* You need to call legalMovesFromSquare before calling this function.
|
||||
* @return ArrayList<Move>
|
||||
*/
|
||||
public ArrayList<Move> getLegalMovesCapture() {
|
||||
return legalMovesCapture;
|
||||
}
|
||||
|
||||
|
||||
/*===================*
|
||||
* PRECOMPUTED MOVES *
|
||||
*===================*/
|
||||
|
||||
|
||||
private static int[][][][] movesAllowed;
|
||||
|
||||
|
Reference in New Issue
Block a user