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:
2006-01-13 12:29:54 +00:00
parent eb0e593ee1
commit c62bdacb9f
5 changed files with 233 additions and 29 deletions

View File

@ -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");
}

View File

@ -25,6 +25,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 {
/**
@ -114,9 +120,44 @@ 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;
}
/*===========================================*
* Those two constructors are no longer used *
@ -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(" ");
}

View File

@ -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);
}

View File

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

View File

@ -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();
@ -66,13 +68,35 @@ public class SuicideChess {
}
continue;
}
if (whatMove.startsWith("force")) {
Move theMove = new Move(whatMove.substring(6,10), bitboard);
theMove.display();
bitboard.doMove(theMove);
bitboard.display();
continue;
}
Move theMove = new Move(whatMove, bitboard);
theMove.display();
bitboard.doMove(theMove);
bitboard.display();
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;
System.out.println("Black: ");