This is a first version where it is possible to play in console.

Rules are not implemented and program crashes when moving a piece that does not
exit or when trying to capture our own pieces...
This commit is contained in:
2006-01-07 16:34:40 +00:00
parent 08c973a452
commit 5bd6db4b06
7 changed files with 327 additions and 75 deletions

View File

@ -1,5 +1,7 @@
package suicideChess;
import suicideChess.Square.NotAValidSquare;
/**
* @author djib
*
@ -58,11 +60,47 @@ public class Board {
/***************
* CONSTRUCTOR *
* @throws NotAValidSquare
***************/
public Board() {
public Board() throws NotAValidSquare {
bitBoards = new long[NB_OF_BITBOARDS];
addPiece(new Square("a1"),new Piece(Piece.WHITE_ROOK));
addPiece(new Square("b1"),new Piece(Piece.WHITE_KNIGHT));
addPiece(new Square("c1"),new Piece(Piece.WHITE_BISHOP));
addPiece(new Square("d1"),new Piece(Piece.WHITE_QUEEN));
addPiece(new Square("e1"),new Piece(Piece.WHITE_KING));
addPiece(new Square("f1"),new Piece(Piece.WHITE_BISHOP));
addPiece(new Square("g1"),new Piece(Piece.WHITE_KNIGHT));
addPiece(new Square("h1"),new Piece(Piece.WHITE_ROOK));
addPiece(new Square("a2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("b2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("c2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("d2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("e2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("f2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("g2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("h2"),new Piece(Piece.WHITE_PAWN));
addPiece(new Square("a8"),new Piece(Piece.BLACK_ROOK));
addPiece(new Square("b8"),new Piece(Piece.BLACK_KNIGHT));
addPiece(new Square("c8"),new Piece(Piece.BLACK_BISHOP));
addPiece(new Square("d8"),new Piece(Piece.BLACK_QUEEN));
addPiece(new Square("e8"),new Piece(Piece.BLACK_KING));
addPiece(new Square("f8"),new Piece(Piece.BLACK_BISHOP));
addPiece(new Square("g8"),new Piece(Piece.BLACK_KNIGHT));
addPiece(new Square("h8"),new Piece(Piece.BLACK_ROOK));
addPiece(new Square("a7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("b7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("c7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("d7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("e7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("f7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("g7"),new Piece(Piece.BLACK_PAWN));
addPiece(new Square("h7"),new Piece(Piece.BLACK_PAWN));
}
@ -70,15 +108,24 @@ public class Board {
* METHODS *
***********/
private long getBitBoard(int bitboard_number) {
public void doMove(Move move) throws NoPieceOnSquare {
if (move.isCaptureMove()) {
removePiece(move.toSquare(), move.getCapturedPiece());
}
removePiece(move.fromSquare(), move.getMovingPiece());
if (move.isPromotionMove()) {
addPiece(move.toSquare(), move.getPromotionPiece());
} else {
addPiece(move.toSquare(), move.getMovingPiece());
}
}
/* private long getBitBoard(int bitboard_number) {
return bitBoards[bitboard_number];
}
*/
private void updateBitBoards(String move) {
//TODO
}
private int squareToBitBoardSquare(Square square) {
//converts a square ("e2") to a BitboardSquare (

View File

@ -19,10 +19,15 @@ public class Move {
********/
//integers
private Piece movingPiece;
private Square fromSquare;
private Square toSquare;
private String promotionPiece;
private Piece promotionPiece;
private boolean isPromotion;
private boolean isCapture;
private Piece capturePiece;
public class NotAValidMoveException extends Exception {
/**
@ -38,23 +43,49 @@ public class Move {
***************/
//The string is of type e2e4 (4 chars), b7b8q (5 chars) for promotions
public Move(String move) throws NotAValidMoveException, NotAValidSquare {
//non capture move
public Move(String move, Piece pieceToMove) throws NotAValidMoveException, NotAValidSquare {
movingPiece = pieceToMove;
isPromotion = false;
promotionPiece = "";
isCapture = false;
switch (move.length()) {
case 5:
isPromotion = true;
promotionPiece = move.substring(4,5);
promotionPiece = new Piece(move.toCharArray()[4], movingPiece.getColor());
//no break statement here on purpose
case 4:
fromSquare = new Square(move.substring(0,2));
toSquare = new Square(move.substring(2,4));
toSquare = new Square(move.substring(2,4));
break;
default:
throw new NotAValidMoveException("Invalid Move: "+move);
}
}
//capture move
public Move(String move, Piece pieceToMove, Piece pieceToCapture) throws NotAValidMoveException, NotAValidSquare {
movingPiece = pieceToMove;
isPromotion = false;
isCapture = true;
capturePiece = pieceToCapture;
switch (move.length()) {
case 5:
isPromotion = true;
promotionPiece = new Piece(move.toCharArray()[4], movingPiece.getColor());
//no break statement here on purpose
case 4:
fromSquare = new Square(move.substring(0,2));
toSquare = new Square(move.substring(2,4));
break;
default:
throw new NotAValidMoveException("Invalid Move: "+move);
}
}
/***********
@ -70,26 +101,52 @@ public class Move {
public boolean isPromotionMove() {
return isPromotion;
}
public String getPromotionPiece() {
return promotionPiece;
public Piece getPromotionPiece() {
if (isPromotion) {
return promotionPiece;
} else {
return new Piece(Piece.NONE);
}
}
public Piece getMovingPiece() {
return movingPiece;
}
public boolean isCaptureMove() {
return isCapture;
}
public Piece getCapturedPiece() {
return capturePiece;
}
public String toString() {
return fromSquare.toString()+toSquare+promotionPiece;
}
/****************************************
* I do not use those functions anymore *
****************************************
public void setFromSquare(Square square) {
fromSquare = square;
}
public void setToSquare(Square square) {
toSquare = square;
}
public void setIsPromotionMove(String piece) {
if (promotionPiece.length()==0) {
public void setPromotionMove(String piece) {
if (piece.length()==0) {
isPromotion=false;
} else {
isPromotion=true;
promotionPiece=piece;
promotionPiece=new Piece(piece);
}
}
public String toString() {
return fromSquare.toString()+toSquare+promotionPiece;
public void setMovingPiece(Piece piece) {
movingPiece = piece;
}
*/
}

View File

@ -23,24 +23,31 @@ public class Piece {
* Take really good care if you want to change those values
* Class BitBoard makes intensive use of those
*/
public static final int NONE=-1;
//Contants used to detect color
public static final int WHITE=0;
public static final int BLACK=1;
//Constants used in the board representation
public static final int WHITE_PIECE = WHITE;
public static final int BLACK_PIECE = BLACK;
public static final int WHITE_PAWN = 2;
public static final int BLACK_PAWN = 3;
public static final int WHITE_KING = 4;
public static final int BLACK_KING = 5;
public static final int WHITE_QUEEN = 6;
public static final int BLACK_QUEEN = 7;
public static final int WHITE_BISHOP = 8;
public static final int BLACK_BISHOP = 9;
public static final int WHITE_KNIGHT = 10;
public static final int BLACK_KNIGHT = 11;
public static final int WHITE_ROOK = 12;
public static final int BLACK_ROOK = 13;
public static final int PAWN = 2;
public static final int WHITE_PAWN = PAWN + WHITE;
public static final int BLACK_PAWN = PAWN + BLACK;
public static final int KING = 4;
public static final int WHITE_KING = KING + WHITE;
public static final int BLACK_KING = KING + BLACK;
public static final int QUEEN = 6;
public static final int WHITE_QUEEN = QUEEN + WHITE;
public static final int BLACK_QUEEN = QUEEN + BLACK;
public static final int BISHOP = 8;
public static final int WHITE_BISHOP = BISHOP + WHITE;
public static final int BLACK_BISHOP = BISHOP + BLACK;
public static final int KNIGHT = 10;
public static final int WHITE_KNIGHT = KNIGHT + WHITE;
public static final int BLACK_KNIGHT = KNIGHT + BLACK;
public static final int ROOK = 12;
public static final int WHITE_ROOK = ROOK + WHITE;
public static final int BLACK_ROOK = ROOK + BLACK;
//Constants used for promotion (as used in the xboard protocol)
@ -49,12 +56,14 @@ public class Piece {
public static final char BISHOP_CHAR='b';
public static final char KNIGHT_CHAR='n';
public static final char ROOK_CHAR='r';
//may be useful
public static final char PAWN_CHAR='p';
/****************
* PRIVATE DATA *
****************/
private static int pieceNumber;
private int pieceNumber;
/***************
* CONSTRUCTOR *
@ -64,6 +73,30 @@ public class Piece {
pieceNumber = piece;
}
public Piece(char piece, int color) {
pieceNumber = NONE;
switch (piece) {
case PAWN_CHAR:
pieceNumber = PAWN + color;
break;
case KING_CHAR:
pieceNumber = KING + color;
break;
case QUEEN_CHAR:
pieceNumber = QUEEN + color;
break;
case BISHOP_CHAR:
pieceNumber = BISHOP + color;
break;
case KNIGHT_CHAR:
pieceNumber = KNIGHT + color;
break;
case ROOK_CHAR:
pieceNumber = ROOK + color;
break;
}
}
/***********
* METHODS *
***********/

View File

@ -33,6 +33,16 @@ public class Square {
file = Character.toLowerCase(square.toCharArray()[0]);
fileNb = ((int)file) - ((int)'a') + 1;
rank = Integer.parseInt(square.substring(1,2));
//perform extra check ?
if (SuicideChess.SQUARE_CHECK_INVALID) {
if (fileNb<1 || fileNb>Board.NB_OF_FILES) {
throw new NotAValidSquare(square);
}
if (rank<1 || rank>Board.NB_OF_RANKS) {
throw new NotAValidSquare(square);
}
}
}
public char getFile() {

View File

@ -21,6 +21,8 @@ public class SuicideChess {
*/
//does BitBoard.class removePiece function checks if removing piece is legal ?
public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = true;
//does Square.class checks if the strings are valid (is "z9" a valid square ?
public static final boolean SQUARE_CHECK_INVALID = true;