Version 0.1.99

==============
Everything seems to work fine for a two player game.
This commit is contained in:
2006-01-13 16:48:21 +00:00
parent c62bdacb9f
commit 651ed576bd
6 changed files with 242 additions and 100 deletions

View File

@ -175,7 +175,25 @@ public class Board {
return new Piece(Piece.NONE); return new Piece(Piece.NONE);
} }
/**
/**
* This function returns a boolean telling if a {@link Piece} is on a {@link Square}.
* @param square The Square
* @param piece A Piece constant
* @return boolean
* @see Piece
* @see Square
*/
public boolean isEmpty(Square square, Piece piece) {
long mask = mapSquaresToBits[squareToBitBoardSquare(square)];
if ((bitBoards[piece.getPieceNumber()] & mask) == 0) {
return true;
} else {
return false;
}
}
/**
* This function converts a {@link Square} to a number representing a bitboard square * This function converts a {@link Square} to a number representing a bitboard square
* @param square The Square to be converted * @param square The Square to be converted
* @return int * @return int
@ -227,66 +245,67 @@ 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 = file+" | "; 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];
if (displayPiece(Piece.BLACK_PAWN,mask)) { if (displayPiece(Piece.BLACK_PAWN,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Character.toUpperCase(Piece.PAWN_CHAR)+" | "; display+="'"+Character.toUpperCase(Piece.PAWN_CHAR)+"'|";
} }
if (displayPiece(Piece.BLACK_QUEEN,mask)) { if (displayPiece(Piece.BLACK_QUEEN,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Character.toUpperCase(Piece.QUEEN_CHAR)+" | "; display+="'"+Character.toUpperCase(Piece.QUEEN_CHAR)+"'|";
} }
if (displayPiece(Piece.BLACK_KING,mask)) { if (displayPiece(Piece.BLACK_KING,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Character.toUpperCase(Piece.KING_CHAR)+" | "; display+="'"+Character.toUpperCase(Piece.KING_CHAR)+"'|";
} }
if (displayPiece(Piece.BLACK_KNIGHT,mask)) { if (displayPiece(Piece.BLACK_KNIGHT,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Character.toUpperCase(Piece.KNIGHT_CHAR)+" | "; display+="'"+Character.toUpperCase(Piece.KNIGHT_CHAR)+"'|";
} }
if (displayPiece(Piece.BLACK_ROOK,mask)) { if (displayPiece(Piece.BLACK_ROOK,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Character.toUpperCase(Piece.ROOK_CHAR)+" | "; display+="'"+Character.toUpperCase(Piece.ROOK_CHAR)+"'|";
} }
if (displayPiece(Piece.BLACK_BISHOP,mask)) { if (displayPiece(Piece.BLACK_BISHOP,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Character.toUpperCase(Piece.BISHOP_CHAR)+" | "; display+="'"+Character.toUpperCase(Piece.BISHOP_CHAR)+"'|";
} }
if (displayPiece(Piece.WHITE_PAWN,mask)) { if (displayPiece(Piece.WHITE_PAWN,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Piece.PAWN_CHAR+" | "; display+=" "+Piece.PAWN_CHAR+" |";
} }
if (displayPiece(Piece.WHITE_QUEEN,mask)) { if (displayPiece(Piece.WHITE_QUEEN,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Piece.QUEEN_CHAR+" | "; display+=" "+Piece.QUEEN_CHAR+" |";
} }
if (displayPiece(Piece.WHITE_KING,mask)) { if (displayPiece(Piece.WHITE_KING,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Piece.KING_CHAR+" | "; display+=" "+Piece.KING_CHAR+" |";
} }
if (displayPiece(Piece.WHITE_KNIGHT,mask)) { if (displayPiece(Piece.WHITE_KNIGHT,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Piece.KNIGHT_CHAR+" | "; display+=" "+Piece.KNIGHT_CHAR+" |";
} }
if (displayPiece(Piece.WHITE_ROOK,mask)) { if (displayPiece(Piece.WHITE_ROOK,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Piece.ROOK_CHAR+" | "; display+=" "+Piece.ROOK_CHAR+" |";
} }
if (displayPiece(Piece.WHITE_BISHOP,mask)) { if (displayPiece(Piece.WHITE_BISHOP,mask)) {
displayedSomething=true; displayedSomething=true;
display+=Piece.BISHOP_CHAR+" | "; display+=" "+Piece.BISHOP_CHAR+" |";
} }
if (!displayedSomething) if (!displayedSomething)
display+=" | "; display+=" |";
} }
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"); System.out.println(" a b c d e f g h");
System.out.println();
} }
@ -331,16 +350,7 @@ public class Board {
bitBoards[piece.getColor()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];; bitBoards[piece.getColor()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];;
} }
//checks if a square is empty for a certain piece //used by function display()
private boolean isEmpty(Square square, Piece piece) {
long mask = mapSquaresToBits[squareToBitBoardSquare(square)];
if ((bitBoards[piece.getPieceNumber()] & mask) == 0) {
return true;
} else {
return false;
}
}
private boolean displayPiece(int whatToDisplay, long mask) { private boolean displayPiece(int whatToDisplay, long mask) {
if ((bitBoards[whatToDisplay] & mask)==0) { if ((bitBoards[whatToDisplay] & mask)==0) {
return false; return false;

View File

@ -33,7 +33,7 @@ public class Move {
private Square enPassantSquare; private Square enPassantSquare;
public class NotAValidMoveException extends Exception { public class NotAValidMoveException extends Exception {
/** /*
* Generated by Eclipse * Generated by Eclipse
*/ */
private static final long serialVersionUID = 2194133427162274651L; private static final long serialVersionUID = 2194133427162274651L;
@ -305,19 +305,30 @@ public class Move {
return fromSquare.toString()+toSquare+promotionPiece; return fromSquare.toString()+toSquare+promotionPiece;
} }
/**
/**
* Tests the equality of two moves by just looking at the fromSquare and the toSquare
* @param that A move to test
* @return boolean
*/
public boolean isSimpleEqual(Move that) {
return ((this.fromSquare.isEqual(that.fromSquare))&&(this.toSquare.isEqual(that.toSquare)));
}
/**
* Displays a move in great details. * Displays a move in great details.
*/ */
public void display() { public void display() {
System.out.println(" "); System.out.println(" ");
System.out.println("==== Move ===="); System.out.println("===== Move =====");
System.out.println("Move: "+getMovingPiece().getPieceNumber()); System.out.println("Move: "+getMovingPiece().toString());
System.out.println("From square:"+fromSquare()); System.out.println("From square: "+fromSquare());
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().toString() : "No Promotion"));
System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().getPieceNumber() : "No Capture")); System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().toString() : "No Capture"));
System.out.println((enablesEnPassant() ? "En-Passant: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank() : "No en-passant")); System.out.println((enablesEnPassant() ? "Set en-pass.: "+getEnPassantSquare().getFile()+getEnPassantSquare().getRank() : "No en-pass. set"));
System.out.println("=============="); System.out.println("================");
System.out.println(" "); System.out.println(" ");
} }

View File

@ -138,4 +138,26 @@ public class Piece {
public int getPieceType() { public int getPieceType() {
return ((pieceNumber/2)-1); return ((pieceNumber/2)-1);
} }
/**
* Displays the agebraic name of the piece
* @return String
*/
public String toString() {
switch (pieceNumber/2-1) {
case BISHOP:
return Character.toString(BISHOP_CHAR);
case QUEEN:
return Character.toString(QUEEN_CHAR);
case KING:
return Character.toString(KING_CHAR);
case ROOK:
return Character.toString(ROOK_CHAR);
case PAWN:
return Character.toString(PAWN_CHAR);
case KNIGHT:
return Character.toString(KNIGHT_CHAR);
}
return "**Error**";
}
} }

View File

@ -13,22 +13,58 @@ import suicideChess.Square.NotAValidSquare;
public class Rules { 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 () { 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} * Computes the possible moves from a given {@link Square}
* according to the current status of the {@link Board} * according to the current status of the {@link Board}
* @param square The square from which the move must start * @param square The square from which the move must start
* @param board The current board position. * @param board The current board position.
* @throws NotAValidSquare If the program throws this exception then there is a bug. * @throws NotAValidSquare If the program throws this exception then there is a bug.
* @throws UnexpectedError This should never happen.
* @see Square * @see Square
* @see Board * @see Board
*/ */
public ArrayList<Move> legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare { public void legalMovesFromSquare(Square fromSquare, Board board) throws NotAValidSquare, UnexpectedError {
Move validMove; Move validMove;
Square toSquare; Square toSquare;
@ -71,6 +107,7 @@ public class Rules {
} }
} else { } else {
captureLeft = false; captureLeft = false;
captureEnPassantLeft=false;
} }
if(fromSquare.getFileNb()<Board.NB_OF_FILES) { 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);
@ -82,6 +119,7 @@ public class Rules {
} }
} else { } else {
captureRight = false; captureRight = false;
captureEnPassantRight=false;
} }
break; break;
case Piece.BLACK: case Piece.BLACK:
@ -102,6 +140,7 @@ public class Rules {
} }
} else { } else {
captureLeft = false; captureLeft = false;
captureEnPassantLeft=false;
} }
if(fromSquare.getFileNb()<Board.NB_OF_FILES) { 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);
@ -113,10 +152,11 @@ public class Rules {
} }
} else { } else {
captureRight = false; captureRight = false;
captureEnPassantRight=false;
} }
break; break;
default: //this should never happen default:
return legalMoves; throw new UnexpectedError("ERROR 01.");
} }
if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) { if (board.getPiece(toSquare).getPieceNumber()!=Piece.NONE) {
normalMove= false; //cannot move forward if there is a piece normalMove= false; //cannot move forward if there is a piece
@ -138,82 +178,82 @@ public class Rules {
if (normalMove) { if (normalMove) {
//to Queen //to Queen
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); 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 //to King
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.KING_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()));
legalMoves.add(validMove);
//to Bishop //to Bishop
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); 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 //to Rook
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); 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) { if (captureLeft) {
//to Queen //to Queen
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
//to King //to King
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.KING_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()));
legalMoves.add(validMove);
//to Bishop //to Bishop
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); 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 //to Rook
validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureLeftSquare, movingPiece, board.getPiece(toCaptureLeftSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
} }
if (captureRight) { if (captureRight) {
//to Queen //to Queen
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.QUEEN_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
//to King //to King
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KING_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
//to Knight //to Knight
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.KNIGHT_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
//to Bishop //to Bishop
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.BISHOP_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
//to Rook //to Rook
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor())); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.ROOK_CHAR, movingPiece.getColor()));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
} }
} else { } else {
if (normalMove) { if (normalMove) {
validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toSquare, movingPiece, new Piece(Piece.NONE), new Piece(Piece.NONE));
legalMoves.add(validMove); legalMovesNonCapture.add(validMove);
} }
if (bigJump) { if (bigJump) {
//create an 'en-passant' status //create an 'en-passant' status
validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false); validMove = new Move(fromSquare, toBigJumpSquare, toSquare, movingPiece, false);
legalMoves.add(validMove); legalMovesNonCapture.add(validMove);
} }
if (captureEnPassantLeft) { if (captureEnPassantLeft) {
//create an 'en-passant' move //create an 'en-passant' move
validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true); validMove = new Move(fromSquare, toCaptureLeftSquare, captureEnPassantLeftSquare, movingPiece, true);
legalMoves.add(validMove); legalMovesCapture.add(validMove);
} }
if (captureEnPassantRight) { if (captureEnPassantRight) {
//create an 'en-passant' move //create an 'en-passant' move
validMove = new Move(fromSquare, toCaptureRightSquare, captureEnPassantRightSquare, movingPiece, true); validMove = new Move(fromSquare, toCaptureRightSquare, captureEnPassantRightSquare, movingPiece, true);
legalMoves.add(validMove); legalMovesCapture.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); legalMovesCapture.add(validMove);
} }
if (captureRight) { if (captureRight) {
validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toCaptureRightSquare, movingPiece, board.getPiece(toCaptureRightSquare), new Piece(Piece.NONE));
legalMoves.add(validMove); legalMovesCapture.add(validMove);
} }
} }
@ -239,16 +279,45 @@ public class Rules {
validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE)); validMove = new Move(fromSquare, toSquare, movingPiece, board.getPiece(toSquare), new Piece(Piece.NONE));
//add move to list of legal moves //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 moveNumber
}//end for ray }//end for ray
} //end case } //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; private static int[][][][] movesAllowed;
static { static {

View File

@ -14,7 +14,7 @@ public class Square {
private int rank; private int rank;
public class NotAValidSquare extends Exception { public class NotAValidSquare extends Exception {
/** /*
* Generated by Eclipse * Generated by Eclipse
*/ */
private static final long serialVersionUID = 7586171991212094565L; private static final long serialVersionUID = 7586171991212094565L;

View File

@ -46,13 +46,14 @@ public class SuicideChess {
Board bitboard = new Board(); Board bitboard = new Board();
bitboard.display(); bitboard.display();
/*int playerColor = Piece.WHITE; int playerColor = Piece.WHITE;
System.out.println("White: ");*/ System.out.println("White: ");
while (true) { while (true) {
try { try {
String whatMove= moveInput.readLine(); String whatMove= moveInput.readLine();
boolean playedALegalMove = false;
if (whatMove.startsWith("quit")) { if (whatMove.startsWith("quit")) {
System.out.println("Goodbye!"); System.out.println("Goodbye!");
@ -61,10 +62,21 @@ public class SuicideChess {
if (whatMove.startsWith("hint")) { if (whatMove.startsWith("hint")) {
Rules rules = new Rules(); Rules rules = new Rules();
ArrayList<Move> allLegalMoves = rules.legalMovesForPlayer(bitboard,playerColor);
rules.legalMovesFromSquare(new Square(whatMove.substring(5,7)),bitboard); ArrayList<Move> allLegalMoves = rules.getLegalMovesCapture();
if (allLegalMoves.size()==0) {
allLegalMoves = rules.getLegalMovesNonCapture();
}
for(int i = 0; i<allLegalMoves.size(); i++) { for(int i = 0; i<allLegalMoves.size(); i++) {
allLegalMoves.get(i).display(); if(allLegalMoves.get(i).isPromotionMove()) {
System.out.println(allLegalMoves.get(i).fromSquare().toString() +
allLegalMoves.get(i).toSquare().toString() +
allLegalMoves.get(i).getPromotionPiece().toString());
} else {
System.out.println(allLegalMoves.get(i).fromSquare().toString() +
allLegalMoves.get(i).toSquare().toString());
}
} }
continue; continue;
} }
@ -79,32 +91,50 @@ public class SuicideChess {
Move theMove = new Move(whatMove, bitboard); Move theMove = new Move(whatMove, bitboard);
Rules rules = new Rules(); Rules rules = new Rules();
ArrayList<Move> allLegalMoves = boolean needToCapture = false;
rules.legalMovesFromSquare(new Square(whatMove.substring(0,2)),bitboard); int foundMoveIndex = -1;
int foundMoveIndex = -1; if(theMove.getMovingPiece().getColor() == playerColor) {
for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) { rules.legalMovesForPlayer(bitboard,playerColor);
if (allLegalMoves.get(moveIndex).toSquare().isEqual(theMove.toSquare())) { ArrayList<Move> allLegalMoves = rules.getLegalMovesCapture();
foundMoveIndex=moveIndex; if (allLegalMoves.size()==0) {
break; allLegalMoves = rules.getLegalMovesNonCapture();
} } else {
} needToCapture = true;
if (foundMoveIndex == -1) { }
System.out.println("This move is not valid. Please type 'hint "+whatMove.substring(0,2) for (int moveIndex = 0; moveIndex < allLegalMoves.size(); moveIndex++) {
+"' to see available moves."); if (allLegalMoves.get(moveIndex).isSimpleEqual(theMove)) {
} else { if(theMove.isPromotionMove()&&
allLegalMoves.get(foundMoveIndex).display(); theMove.getPromotionPiece().getPieceNumber()!=allLegalMoves.get(moveIndex).getPromotionPiece().getPieceNumber()) {
bitboard.doMove(allLegalMoves.get(foundMoveIndex)); continue;
bitboard.display(); }
} foundMoveIndex=moveIndex;
break;
/*if (playerColor == Piece.WHITE) { }
playerColor = Piece.BLACK; }
System.out.println("Black: "); if (foundMoveIndex == -1) {
} else { if (needToCapture) {
playerColor = Piece.WHITE; System.out.println("Capturing is mandatory.");
System.out.println("White: "); }
}*/ System.out.println("This move is not valid. Please type 'hint' to see list of legal moves.");
} else {
allLegalMoves.get(foundMoveIndex).display();
bitboard.doMove(allLegalMoves.get(foundMoveIndex));
bitboard.display();
playedALegalMove=true;
}
} else {
System.out.println("Please play a piece of the right color.");
}
if (playedALegalMove) {
if (playerColor == Piece.WHITE) {
playerColor = Piece.BLACK;
System.out.println("Black: ");
} else {
playerColor = Piece.WHITE;
System.out.println("White: ");
}
}
} catch (NotAValidMoveException err) { } catch (NotAValidMoveException err) {
System.out.println(err); System.out.println(err);