=========== 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...
153 lines
3.1 KiB
Java
153 lines
3.1 KiB
Java
package suicideChess;
|
|
|
|
import suicideChess.Square.NotAValidSquare;
|
|
|
|
/**
|
|
* @author djib
|
|
*
|
|
* This file contains the moves representation.
|
|
*
|
|
* $LastChangedDate$
|
|
* $LastChangedRevision$
|
|
* $LastChangedBy$
|
|
*/
|
|
|
|
public class Move {
|
|
|
|
/********
|
|
* DATA *
|
|
********/
|
|
|
|
//integers
|
|
private Piece movingPiece;
|
|
|
|
private Square fromSquare;
|
|
private Square toSquare;
|
|
private Piece promotionPiece;
|
|
private boolean isPromotion;
|
|
|
|
private boolean isCapture;
|
|
private Piece capturePiece;
|
|
|
|
public class NotAValidMoveException extends Exception {
|
|
/**
|
|
* Generated by Eclipse
|
|
*/
|
|
private static final long serialVersionUID = 2194133427162274651L;
|
|
|
|
NotAValidMoveException(String s) { super(s); };
|
|
}
|
|
|
|
/***************
|
|
* CONSTRUCTOR *
|
|
***************/
|
|
|
|
//The string is of type e2e4 (4 chars), b7b8q (5 chars) for promotions
|
|
|
|
//non capture move
|
|
public Move(String move, Piece pieceToMove) throws NotAValidMoveException, NotAValidSquare {
|
|
movingPiece = pieceToMove;
|
|
isPromotion = false;
|
|
isCapture = false;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
//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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********
|
|
* METHODS *
|
|
***********/
|
|
|
|
public Square fromSquare() {
|
|
return fromSquare;
|
|
}
|
|
public Square toSquare() {
|
|
return toSquare;
|
|
}
|
|
public boolean isPromotionMove() {
|
|
return isPromotion;
|
|
}
|
|
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 setPromotionMove(String piece) {
|
|
if (piece.length()==0) {
|
|
isPromotion=false;
|
|
} else {
|
|
isPromotion=true;
|
|
promotionPiece=new Piece(piece);
|
|
}
|
|
}
|
|
public void setMovingPiece(Piece piece) {
|
|
movingPiece = piece;
|
|
}
|
|
|
|
*/
|
|
}
|