Moves are now intuitive.
Don't have to tell what piece to move and what piece to capture. Big code cleanup as well.
This commit is contained in:
@ -104,9 +104,9 @@ public class Board {
|
||||
}
|
||||
|
||||
|
||||
/***********
|
||||
* METHODS *
|
||||
***********/
|
||||
/******************
|
||||
* PUBLIC METHODS *
|
||||
******************/
|
||||
|
||||
public void doMove(Move move) throws NoPieceOnSquare {
|
||||
if (move.isCaptureMove()) {
|
||||
@ -119,7 +119,100 @@ public class Board {
|
||||
addPiece(move.toSquare(), move.getMovingPiece());
|
||||
}
|
||||
}
|
||||
|
||||
//this function searchs for what piece is on a square
|
||||
public Piece getPiece(Square onSquare) {
|
||||
//if there is a corresponding white piece.
|
||||
if (!isEmpty(onSquare, new Piece(Piece.WHITE_PIECES))) {
|
||||
//for every white piece bitboard, look for the piece
|
||||
for (int piece = Piece.WHITE_PAWN; piece <= Piece.WHITE_ROOK; piece += 2) {
|
||||
if(!isEmpty(onSquare, new Piece(piece))) {
|
||||
return new Piece(piece);
|
||||
}
|
||||
}
|
||||
} else if (!isEmpty(onSquare, new Piece(Piece.BLACK_PIECES))) {
|
||||
//for every white piece bitboard, look for the piece
|
||||
for (int piece = Piece.BLACK_PAWN; piece <= Piece.BLACK_ROOK; piece += 2) {
|
||||
if(!isEmpty(onSquare, new Piece(piece))) {
|
||||
return new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
//if no piece found
|
||||
return new Piece(Piece.NONE);
|
||||
}
|
||||
|
||||
|
||||
//this function can be used to display the board
|
||||
public void display(){
|
||||
for (int file = NB_OF_FILES; file >= 1; file--) {
|
||||
System.out.println("+---+---+---+---+---+---+---+---+");
|
||||
String display = "| ";
|
||||
for (int rank=1; rank <= NB_OF_RANKS; rank++) {
|
||||
boolean displayedSomething = false;
|
||||
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
|
||||
|
||||
if (displayPiece(Piece.BLACK_PAWN,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Character.toUpperCase(Piece.PAWN_CHAR)+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.BLACK_QUEEN,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Character.toUpperCase(Piece.QUEEN_CHAR)+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.BLACK_KING,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Character.toUpperCase(Piece.KING_CHAR)+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.BLACK_KNIGHT,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Character.toUpperCase(Piece.KNIGHT_CHAR)+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.BLACK_ROOK,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Character.toUpperCase(Piece.ROOK_CHAR)+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.BLACK_BISHOP,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Character.toUpperCase(Piece.BISHOP_CHAR)+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.WHITE_PAWN,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Piece.PAWN_CHAR+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.WHITE_QUEEN,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Piece.QUEEN_CHAR+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.WHITE_KING,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Piece.KING_CHAR+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.WHITE_KNIGHT,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Piece.KNIGHT_CHAR+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.WHITE_ROOK,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Piece.ROOK_CHAR+" | ";
|
||||
}
|
||||
if (displayPiece(Piece.WHITE_BISHOP,mask)) {
|
||||
displayedSomething=true;
|
||||
display+=Piece.BISHOP_CHAR+" | ";
|
||||
}
|
||||
if (!displayedSomething)
|
||||
display+=" | ";
|
||||
}
|
||||
System.out.println(display);
|
||||
}
|
||||
System.out.println("+---+---+---+---+---+---+---+---+");
|
||||
}
|
||||
|
||||
|
||||
/*******************
|
||||
* PRIVATE METHODS *
|
||||
*******************/
|
||||
|
||||
|
||||
/* private long getBitBoard(int bitboard_number) {
|
||||
return bitBoards[bitboard_number];
|
||||
@ -151,7 +244,7 @@ public class Board {
|
||||
//remove Piece to corresponding bitboard.
|
||||
|
||||
if (SuicideChess.BITBOARD_REMOVEPIECE_CHECK_REMOVE) {
|
||||
if (checkIsNotEmpty(square, piece)) {
|
||||
if (!isEmpty(square, piece)) {
|
||||
bitBoards[piece.getPieceNumber()] ^= mapSquaresToBits[squareToBitBoardSquare(square)];
|
||||
} else {
|
||||
throw new NoPieceOnSquare("Square: "+square + "; Piece: " + piece.getPieceNumber());
|
||||
@ -164,12 +257,21 @@ public class Board {
|
||||
}
|
||||
|
||||
//checks if a square is empty for a certain piece
|
||||
private boolean checkIsNotEmpty(Square square, Piece piece) {
|
||||
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) {
|
||||
if ((bitBoards[whatToDisplay] & mask)==0) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user