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

3
TODO
View File

@ -1,2 +1 @@
Create a variable called SECURITY. Capture is not implemented in Move.class
If SECURITY = false then many test that are 'a priori' useless are not done.

View File

@ -1,5 +1,7 @@
package suicideChess; package suicideChess;
import suicideChess.Square.NotAValidSquare;
/** /**
* @author djib * @author djib
* *
@ -58,10 +60,46 @@ public class Board {
/*************** /***************
* CONSTRUCTOR * * CONSTRUCTOR *
* @throws NotAValidSquare
***************/ ***************/
public Board() { public Board() throws NotAValidSquare {
bitBoards = new long[NB_OF_BITBOARDS]; 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,14 +108,23 @@ public class Board {
* METHODS * * 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]; return bitBoards[bitboard_number];
} }
*/
private void updateBitBoards(String move) {
//TODO
}
private int squareToBitBoardSquare(Square square) { private int squareToBitBoardSquare(Square square) {

View File

@ -19,11 +19,16 @@ public class Move {
********/ ********/
//integers //integers
private Piece movingPiece;
private Square fromSquare; private Square fromSquare;
private Square toSquare; private Square toSquare;
private String promotionPiece; private Piece promotionPiece;
private boolean isPromotion; private boolean isPromotion;
private boolean isCapture;
private Piece capturePiece;
public class NotAValidMoveException extends Exception { public class NotAValidMoveException extends Exception {
/** /**
* Generated by Eclipse * Generated by Eclipse
@ -38,13 +43,17 @@ public class Move {
***************/ ***************/
//The string is of type e2e4 (4 chars), b7b8q (5 chars) for promotions //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; isPromotion = false;
promotionPiece = ""; isCapture = false;
switch (move.length()) { switch (move.length()) {
case 5: case 5:
isPromotion = true; isPromotion = true;
promotionPiece = move.substring(4,5); promotionPiece = new Piece(move.toCharArray()[4], movingPiece.getColor());
//no break statement here on purpose //no break statement here on purpose
case 4: case 4:
fromSquare = new Square(move.substring(0,2)); fromSquare = new Square(move.substring(0,2));
@ -55,6 +64,28 @@ public class 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,9 +101,34 @@ public class Move {
public boolean isPromotionMove() { public boolean isPromotionMove() {
return isPromotion; return isPromotion;
} }
public String getPromotionPiece() { public Piece getPromotionPiece() {
if (isPromotion) {
return promotionPiece; 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) { public void setFromSquare(Square square) {
fromSquare = square; fromSquare = square;
@ -80,16 +136,17 @@ public class Move {
public void setToSquare(Square square) { public void setToSquare(Square square) {
toSquare = square; toSquare = square;
} }
public void setIsPromotionMove(String piece) { public void setPromotionMove(String piece) {
if (promotionPiece.length()==0) { if (piece.length()==0) {
isPromotion=false; isPromotion=false;
} else { } else {
isPromotion=true; isPromotion=true;
promotionPiece=piece; promotionPiece=new Piece(piece);
} }
} }
public void setMovingPiece(Piece piece) {
movingPiece = piece;
}
public String toString() { */
return fromSquare.toString()+toSquare+promotionPiece;
}
} }

View File

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

View File

@ -33,6 +33,16 @@ public class Square {
file = Character.toLowerCase(square.toCharArray()[0]); file = Character.toLowerCase(square.toCharArray()[0]);
fileNb = ((int)file) - ((int)'a') + 1; fileNb = ((int)file) - ((int)'a') + 1;
rank = Integer.parseInt(square.substring(1,2)); 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() { public char getFile() {

View File

@ -21,6 +21,8 @@ public class SuicideChess {
*/ */
//does BitBoard.class removePiece function checks if removing piece is legal ? //does BitBoard.class removePiece function checks if removing piece is legal ?
public static final boolean BITBOARD_REMOVEPIECE_CHECK_REMOVE = true; 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;

View File

@ -5,6 +5,7 @@ import java.io.InputStreamReader;
import suicideChess.*; import suicideChess.*;
import suicideChess.Board.NoPieceOnSquare; import suicideChess.Board.NoPieceOnSquare;
import suicideChess.Move.NotAValidMoveException;
import suicideChess.Square.NotAValidSquare; import suicideChess.Square.NotAValidSquare;
/** /**
@ -22,7 +23,11 @@ public class TestMoves {
public static void main(String[] args) { public static void main(String[] args) {
BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in)); BufferedReader moveInput = new BufferedReader(new InputStreamReader(System.in));
try {
BitBoardTest bitboard = new BitBoardTest(); BitBoardTest bitboard = new BitBoardTest();
bitboard.display();
int playerColor = Piece.WHITE;
while (true) { while (true) {
try { try {
@ -35,42 +40,77 @@ public class TestMoves {
//new SquareTest(move).display(); //new SquareTest(move).display();
//System.out.println(new Square(move)); //System.out.println(new Square(move));
/** /**
* Test of the add function in class BitBoard * Test of the moves
* Moves must be displayed in the following form :
* PieceLetter move(4lettes) PieceCaptureCode(2digits)
* example p e4d5 p
*
* Not very convenient but it is for test purposes :-P
*/ */
String whereToAdd = moveInput.readLine(); String whatMove= moveInput.readLine();
if (whereToAdd.startsWith("rm ")) { MoveTest theMove;
bitboard.rm(new Square(whereToAdd.substring(3)), new Piece(Piece.BLACK_PAWN)); Piece movePiece = new Piece(whatMove.toCharArray()[0],playerColor);
if (whatMove.length()==6) {
theMove = new MoveTest(whatMove.substring(2,6),movePiece);
} else { } else {
bitboard.add(new Square(whereToAdd), new Piece(Piece.BLACK_PAWN)); Piece capturePiece;
if (playerColor==Piece.WHITE) {
capturePiece = new Piece(whatMove.toCharArray()[7],Piece.BLACK);
theMove = new MoveTest(whatMove.substring(2,6),movePiece,capturePiece);
} else {
capturePiece = new Piece(whatMove.toCharArray()[7],Piece.WHITE);
theMove = new MoveTest(whatMove.substring(2,6),movePiece,capturePiece);
} }
bitboard.display(Piece.BLACK_PAWN); }
theMove.display();
bitboard.doMove(theMove);
bitboard.display();
if (playerColor == Piece.WHITE) {
playerColor = Piece.BLACK;
} else {
playerColor = Piece.WHITE;
}
} catch (Exception err) { } catch (Exception err) {
System.out.println(err); System.out.println(err);
break; break;
} }
} }
} catch (NotAValidSquare e) {
e.printStackTrace();
}
} }
} }
class MoveTest extends Move { class MoveTest extends Move {
public MoveTest(String move) throws NotAValidMoveException, NotAValidSquare { public MoveTest(String move, Piece pieceToMove) throws NotAValidMoveException, NotAValidSquare {
super(move); super(move, pieceToMove);
} }
public MoveTest(String move, Piece pieceToMove, Piece pieceToCapture) throws NotAValidMoveException, NotAValidSquare {
super(move, pieceToMove, pieceToCapture);
}
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("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() : "No Promotion")); System.out.println((isPromotionMove() ? "Promotion: "+getPromotionPiece() : "No Promotion"));
System.out.println("==============="); System.out.println((isCaptureMove() ? "Capture: "+getCapturedPiece().getPieceNumber() : "No Capture"));
System.out.println("==============");
System.out.println(" "); System.out.println(" ");
System.out.println(" "); System.out.println(" ");
} }
} }
/*
class SquareTest extends Square { class SquareTest extends Square {
public SquareTest(String square) throws NotAValidSquare { public SquareTest(String square) throws NotAValidSquare {
super(square); super(square);
@ -87,27 +127,91 @@ class SquareTest extends Square {
} }
} }
*/
class BitBoardTest extends Board { class BitBoardTest extends Board {
public void display(int whatToDisplay) { public BitBoardTest() throws NotAValidSquare {
super();
}
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 = "| ";
for (int rank=1; rank <= NB_OF_RANKS; rank++) { for (int rank=1; rank <= NB_OF_RANKS; rank++) {
boolean displayedSomething = false;
long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS]; long mask = mapSquaresToBits[rank-1+(file-1)*NB_OF_RANKS];
if ((bitBoards[whatToDisplay] & mask)==0) {
display+=" | "; if (displayPiece(Piece.BLACK_PAWN,mask)) {
} else { displayedSomething=true;
display+="1 | "; 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(display);
} }
System.out.println("+---+---+---+---+---+---+---+---+"); System.out.println("+---+---+---+---+---+---+---+---+");
} }
private boolean displayPiece(int whatToDisplay, long mask) {
if ((bitBoards[whatToDisplay] & mask)==0) {
return false;
} else {
return true;
}
}
/*
public void add (Square square, Piece piece) { public void add (Square square, Piece piece) {
addPiece(square, piece); addPiece(square, piece);
} }
public void rm (Square square, Piece piece) throws NoPieceOnSquare { public void rm (Square square, Piece piece) throws NoPieceOnSquare {
removePiece(square, piece); removePiece(square, piece);
} }
*/
} }