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[];
|
protected long bitBoards[];
|
||||||
|
|
||||||
|
|
||||||
|
private boolean enPassant=false; //is there an 'en passant pawn' on the board
|
||||||
|
private Square enPassantSquare;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*=============*
|
/*=============*
|
||||||
@ -122,14 +125,26 @@ public class Board {
|
|||||||
|
|
||||||
public void doMove(Move move) throws NoPieceOnSquare {
|
public void doMove(Move move) throws NoPieceOnSquare {
|
||||||
if (move.isCaptureMove()) {
|
if (move.isCaptureMove()) {
|
||||||
|
if (move.isEnPassant()) {
|
||||||
|
removePiece(move.getEnPassantSquare(), move.getCapturedPiece());
|
||||||
|
} else {
|
||||||
removePiece(move.toSquare(), move.getCapturedPiece());
|
removePiece(move.toSquare(), move.getCapturedPiece());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
removePiece(move.fromSquare(), move.getMovingPiece());
|
removePiece(move.fromSquare(), move.getMovingPiece());
|
||||||
if (move.isPromotionMove()) {
|
if (move.isPromotionMove()) {
|
||||||
addPiece(move.toSquare(), move.getPromotionPiece());
|
addPiece(move.toSquare(), move.getPromotionPiece());
|
||||||
} else {
|
} else {
|
||||||
addPiece(move.toSquare(), move.getMovingPiece());
|
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;
|
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
|
* This function can be used to display the board
|
||||||
@ -179,7 +227,7 @@ public class Board {
|
|||||||
public void display(){
|
public void display(){
|
||||||
for (int file = NB_OF_FILES; file >= 1; file--) {
|
for (int file = NB_OF_FILES; file >= 1; file--) {
|
||||||
System.out.println(" +---+---+---+---+---+---+---+---+");
|
System.out.println(" +---+---+---+---+---+---+---+---+");
|
||||||
String display = "| ";
|
String display = file+" | ";
|
||||||
for (int rank=1; rank <= NB_OF_RANKS; rank++) {
|
for (int rank=1; rank <= NB_OF_RANKS; rank++) {
|
||||||
boolean displayedSomething = false;
|
boolean displayedSomething = false;
|
||||||
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
|
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
|
||||||
@ -238,6 +286,7 @@ public class Board {
|
|||||||
System.out.println(display);
|
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 boolean isCapture;
|
||||||
private Piece capturePiece;
|
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 {
|
public class NotAValidMoveException extends Exception {
|
||||||
/**
|
/**
|
||||||
* Generated by Eclipse
|
* Generated by Eclipse
|
||||||
@ -114,7 +120,42 @@ public class Move {
|
|||||||
} else {
|
} else {
|
||||||
isPromotion = false;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -234,6 +275,28 @@ public class Move {
|
|||||||
return capturePiece;
|
return capturePiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Converts a Move into a String.
|
||||||
* @return String
|
* @return String
|
||||||
@ -253,9 +316,9 @@ public class Move {
|
|||||||
System.out.println("To square: "+toSquare());
|
System.out.println("To square: "+toSquare());
|
||||||
System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece().getPieceNumber() : "No Promotion"));
|
System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece().getPieceNumber() : "No Promotion"));
|
||||||
System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().getPieceNumber() : "No Capture"));
|
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(" ");
|
System.out.println(" ");
|
||||||
System.out.println(" ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,42 +47,88 @@ public class Rules {
|
|||||||
Square toCaptureLeftSquare=null;
|
Square toCaptureLeftSquare=null;
|
||||||
Square toCaptureRightSquare=null;
|
Square toCaptureRightSquare=null;
|
||||||
|
|
||||||
|
boolean captureEnPassantRight=board.isEnPassant();
|
||||||
|
boolean captureEnPassantLeft=board.isEnPassant();
|
||||||
|
Square captureEnPassantLeftSquare=null;
|
||||||
|
Square captureEnPassantRightSquare=null;
|
||||||
|
|
||||||
switch (movingPiece.getColor()) {
|
switch (movingPiece.getColor()) {
|
||||||
case Piece.WHITE:
|
case Piece.WHITE:
|
||||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1);
|
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+1);
|
||||||
if (fromSquare.getRank()==2) {
|
if (fromSquare.getRank()==2) {
|
||||||
bigJump=true;
|
bigJump=true;
|
||||||
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2);
|
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()+2);
|
||||||
} else if (toSquare.getRank()==8) {
|
} else if (toSquare.getRank()==Board.NB_OF_RANKS) {
|
||||||
promotion=true;
|
promotion=true;
|
||||||
}
|
}
|
||||||
|
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
|
||||||
toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()+1);
|
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);
|
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;
|
break;
|
||||||
case Piece.BLACK:
|
case Piece.BLACK:
|
||||||
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1);
|
toSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-1);
|
||||||
if (fromSquare.getRank()==7) {
|
if (fromSquare.getRank()==(Board.NB_OF_RANKS-1)) {
|
||||||
bigJump=true;
|
bigJump=true;
|
||||||
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2);
|
toBigJumpSquare = new Square(fromSquare.getFileNb(), fromSquare.getRank()-2);
|
||||||
} else if (toSquare.getRank()==1) {
|
} else if (toSquare.getRank()==1) {
|
||||||
promotion=true;
|
promotion=true;
|
||||||
}
|
}
|
||||||
|
if(fromSquare.getFileNb()>1) { //make sure not to go out of the board
|
||||||
toCaptureLeftSquare = new Square(fromSquare.getFileNb()-1, fromSquare.getRank()-1);
|
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);
|
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;
|
break;
|
||||||
default: //this should never happen
|
default: //this should never happen
|
||||||
return legalMoves;
|
return legalMoves;
|
||||||
}
|
}
|
||||||
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
|
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)
|
if (captureLeft&&
|
||||||
|| (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor())) {
|
((board.getPiece(toCaptureLeftSquare).getColor()==Piece.NO_COLOR)
|
||||||
|
|| (board.getPiece(toCaptureLeftSquare).getColor()==movingPiece.getColor()))) {
|
||||||
captureLeft = false;
|
captureLeft = false;
|
||||||
}
|
}
|
||||||
if ((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR)
|
if (captureRight&&
|
||||||
|| (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor())) {
|
((board.getPiece(toCaptureRightSquare).getColor()==Piece.NO_COLOR)
|
||||||
System.out.println("Here");
|
|| (board.getPiece(toCaptureRightSquare).getColor()==movingPiece.getColor()))) {
|
||||||
captureRight = false;
|
captureRight = false;
|
||||||
}
|
}
|
||||||
if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) {
|
if (bigJump && (board.getPiece(toBigJumpSquare).getPieceNumber()!=Piece.NONE)) {
|
||||||
@ -146,9 +192,21 @@ public class Rules {
|
|||||||
legalMoves.add(validMove);
|
legalMoves.add(validMove);
|
||||||
}
|
}
|
||||||
if (bigJump) {
|
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);
|
legalMoves.add(validMove);
|
||||||
}
|
}
|
||||||
|
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) {
|
if (captureLeft) {
|
||||||
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
|
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.NONE));
|
||||||
legalMoves.add(validMove);
|
legalMoves.add(validMove);
|
||||||
|
@ -101,6 +101,16 @@ public class Square {
|
|||||||
return fileNb;
|
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
|
* Converts a Square back to a String
|
||||||
* @return String
|
* @return String
|
||||||
|
@ -30,7 +30,8 @@ public class SuicideChess {
|
|||||||
public static final boolean SQUARE_CHECK_INVALID = true;
|
public static final boolean SQUARE_CHECK_INVALID = true;
|
||||||
|
|
||||||
private static final int MAIN_VERSION_NUMBER = 0;
|
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
|
* The main function
|
||||||
@ -38,6 +39,7 @@ public class SuicideChess {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(" Welcome to SuicideChess v"+MAIN_VERSION_NUMBER+"."+REVISION_NUMBER+"!");
|
System.out.println(" Welcome to SuicideChess v"+MAIN_VERSION_NUMBER+"."+REVISION_NUMBER+"!");
|
||||||
|
System.out.println();
|
||||||
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -67,11 +69,33 @@ public class SuicideChess {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Move theMove = new Move(whatMove, bitboard);
|
if (whatMove.startsWith("force")) {
|
||||||
|
Move theMove = new Move(whatMove.substring(6,10), bitboard);
|
||||||
theMove.display();
|
theMove.display();
|
||||||
bitboard.doMove(theMove);
|
bitboard.doMove(theMove);
|
||||||
bitboard.display();
|
bitboard.display();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
/*if (playerColor == Piece.WHITE) {
|
||||||
playerColor = Piece.BLACK;
|
playerColor = Piece.BLACK;
|
||||||
|
Reference in New Issue
Block a user