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:
@ -57,6 +57,9 @@ public class Board {
|
||||
protected long bitBoards[];
|
||||
|
||||
|
||||
private boolean enPassant=false; //is there an 'en passant pawn' on the board
|
||||
private Square enPassantSquare;
|
||||
|
||||
|
||||
|
||||
/*=============*
|
||||
@ -122,7 +125,11 @@ public class Board {
|
||||
|
||||
public void doMove(Move move) throws NoPieceOnSquare {
|
||||
if (move.isCaptureMove()) {
|
||||
removePiece(move.toSquare(), move.getCapturedPiece());
|
||||
if (move.isEnPassant()) {
|
||||
removePiece(move.getEnPassantSquare(), move.getCapturedPiece());
|
||||
} else {
|
||||
removePiece(move.toSquare(), move.getCapturedPiece());
|
||||
}
|
||||
}
|
||||
removePiece(move.fromSquare(), move.getMovingPiece());
|
||||
if (move.isPromotionMove()) {
|
||||
@ -130,6 +137,14 @@ public class Board {
|
||||
} else {
|
||||
addPiece(move.toSquare(), move.getMovingPiece());
|
||||
}
|
||||
|
||||
this.enPassant=false;
|
||||
if (move.enablesEnPassant()) {
|
||||
enPassant=true;
|
||||
enPassantSquare=move.getEnPassantSquare();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -171,6 +186,39 @@ public class Board {
|
||||
return square.getFileNb() -1 + (square.getRank()-1)*NB_OF_FILES;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * This function is used to set the table to an state in which a {@link Square} is an 'en passant' Square
|
||||
// * @param Square The 'en passant' Square
|
||||
// * @see Square
|
||||
// */
|
||||
// public void setEnPassant(Square square) {
|
||||
// enPassant=true;
|
||||
// enPassantSquare=square;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * This function is used to set the table back to a state with no 'en passant' Squares.
|
||||
// */
|
||||
// public void clearEnPassant () {
|
||||
// enPassant=false;
|
||||
// }
|
||||
|
||||
/**
|
||||
* This function returns a boolean saying if the board is in an 'en passant' state.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isEnPassant() {
|
||||
return enPassant;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is used to return the 'en passant' Square. Don't use it unless table is in an 'en passant' state.
|
||||
* @return Square
|
||||
* @see Square
|
||||
*/
|
||||
public Square getEnPassantSquare() {
|
||||
return enPassantSquare;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function can be used to display the board
|
||||
@ -178,8 +226,8 @@ public class Board {
|
||||
*/
|
||||
public void display(){
|
||||
for (int file = NB_OF_FILES; file >= 1; file--) {
|
||||
System.out.println("+---+---+---+---+---+---+---+---+");
|
||||
String display = "| ";
|
||||
System.out.println(" +---+---+---+---+---+---+---+---+");
|
||||
String display = file+" | ";
|
||||
for (int rank=1; rank <= NB_OF_RANKS; rank++) {
|
||||
boolean displayedSomething = false;
|
||||
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
|
||||
@ -237,7 +285,8 @@ public class Board {
|
||||
}
|
||||
System.out.println(display);
|
||||
}
|
||||
System.out.println("+---+---+---+---+---+---+---+---+");
|
||||
System.out.println(" +---+---+---+---+---+---+---+---+");
|
||||
System.out.println(" a b c d e f g h");
|
||||
}
|
||||
|
||||
|
||||
|
@ -26,6 +26,12 @@ public class Move {
|
||||
private boolean isCapture;
|
||||
private Piece capturePiece;
|
||||
|
||||
//the following are used for two things :
|
||||
//moves that enable the 'en Passant' status of the board
|
||||
//and 'en passant' captures.
|
||||
private boolean isEnPassant=false;
|
||||
private Square enPassantSquare;
|
||||
|
||||
public class NotAValidMoveException extends Exception {
|
||||
/**
|
||||
* Generated by Eclipse
|
||||
@ -114,7 +120,42 @@ public class Move {
|
||||
} else {
|
||||
isPromotion = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This is the constructor of the class.
|
||||
* It should be used only for moves that enable or are 'en passant' capture.
|
||||
*
|
||||
* @param fromSquare The {@link Square} to move from.
|
||||
* @param toSquare The {@link Square} to move to.
|
||||
* @param enPassantSquare The 'en passant' {@link Square}.
|
||||
* @param movingPiece The {@link Piece} to be moved.
|
||||
* @param isCapture This boolean is used to differenciate 'en passant' captures from moves that simply enable 'en passant' capture.
|
||||
* @throws NotAValidSquare If Squares are not valid.
|
||||
* @see Square
|
||||
* @see Piece
|
||||
*/
|
||||
public Move(Square fromSquare, Square toSquare, Square enPassantSquare, Piece movingPiece, boolean isCapture)
|
||||
throws NotAValidSquare {
|
||||
|
||||
this.fromSquare = fromSquare;
|
||||
this.toSquare = toSquare;
|
||||
|
||||
this.movingPiece = movingPiece;
|
||||
|
||||
this.isCapture = isCapture;
|
||||
if (isCapture) {
|
||||
if (movingPiece.getColor()==Piece.WHITE) {
|
||||
this.capturePiece=new Piece(Piece.BLACK_PAWN);
|
||||
} else {
|
||||
this.capturePiece=new Piece(Piece.WHITE_PAWN);
|
||||
}
|
||||
}
|
||||
|
||||
isPromotion = false;
|
||||
isEnPassant = true;
|
||||
this.enPassantSquare = enPassantSquare;
|
||||
}
|
||||
|
||||
|
||||
@ -235,6 +276,28 @@ public class Move {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean saying is a Move makes 'en passant' move possible.
|
||||
*/
|
||||
public boolean enablesEnPassant() {
|
||||
return (!isCapture)&&isEnPassant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean saying is a Move is an 'en passant' move.
|
||||
*/
|
||||
public boolean isEnPassant() {
|
||||
return (isCapture)&&isEnPassant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the enPassant Square for a move.
|
||||
*/
|
||||
public Square getEnPassantSquare() {
|
||||
return enPassantSquare;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a Move into a String.
|
||||
* @return String
|
||||
*/
|
||||
@ -253,9 +316,9 @@ public class Move {
|
||||
System.out.println("To square: "+toSquare());
|
||||
System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece().getPieceNumber() : "No Promotion"));
|
||||
System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().getPieceNumber() : "No Capture"));
|
||||
System.out.println((enablesEnPassant() ? "En-Passant: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank() : "No en-passant"));
|
||||
System.out.println("==============");
|
||||
System.out.println(" ");
|
||||
System.out.println(" ");
|
||||
}
|
||||
|
||||
|
||||
|
@ -47,42 +47,88 @@ public class Rules {
|
||||
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:
|
||||
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) {
|
||||
} 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);
|
||||
}
|
||||
|
@ -101,6 +101,16 @@ public class Square {
|
||||
return fileNb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean telling if two squares are the same
|
||||
* @param Square to compare to.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isEqual(Square that) {
|
||||
return ((this.fileNb==that.fileNb)&&(this.rank==that.rank));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a Square back to a String
|
||||
* @return String
|
||||
|
@ -30,15 +30,17 @@ public class SuicideChess {
|
||||
public static final boolean SQUARE_CHECK_INVALID = true;
|
||||
|
||||
private static final int MAIN_VERSION_NUMBER = 0;
|
||||
private static final int REVISION_NUMBER = 15;
|
||||
private static final int REVISION_NUMBER = 17;
|
||||
|
||||
|
||||
/**
|
||||
* The main function
|
||||
* @param args No parameters should be transmitted to this function.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Welcome to SuicideChess v"+MAIN_VERSION_NUMBER+"."+REVISION_NUMBER+"!");
|
||||
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println(" Welcome to SuicideChess v"+MAIN_VERSION_NUMBER+"."+REVISION_NUMBER+"!");
|
||||
System.out.println();
|
||||
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
try {
|
||||
Board bitboard = new Board();
|
||||
@ -67,11 +69,33 @@ public class SuicideChess {
|
||||
continue;
|
||||
}
|
||||
|
||||
Move theMove = new Move(whatMove, bitboard);
|
||||
if (whatMove.startsWith("force")) {
|
||||
Move theMove = new Move(whatMove.substring(6,10), bitboard);
|
||||
theMove.display();
|
||||
bitboard.doMove(theMove);
|
||||
bitboard.display();
|
||||
continue;
|
||||
}
|
||||
|
||||
theMove.display();
|
||||
bitboard.doMove(theMove);
|
||||
bitboard.display();
|
||||
Move theMove = new Move(whatMove, bitboard);
|
||||
Rules rules = new Rules();
|
||||
ArrayList<Move> allLegalMoves =
|
||||
rules.legalMovesFromSquare(new Square(whatMove.substring(0,2)),bitboard);
|
||||
int foundMoveIndex = -1;
|
||||
for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) {
|
||||
if (allLegalMoves.get(moveIndex).toSquare().isEqual(theMove.toSquare())) {
|
||||
foundMoveIndex=moveIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundMoveIndex == -1) {
|
||||
System.out.println("This move is not valid. Please type 'hint "+whatMove.substring(0,2)
|
||||
+"' to see available moves.");
|
||||
} else {
|
||||
allLegalMoves.get(foundMoveIndex).display();
|
||||
bitboard.doMove(allLegalMoves.get(foundMoveIndex));
|
||||
bitboard.display();
|
||||
}
|
||||
|
||||
/*if (playerColor == Piece.WHITE) {
|
||||
playerColor = Piece.BLACK;
|
||||
|
Reference in New Issue
Block a user