package checkers;
import jess.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
/**
* This object represents a piece on a board and is used in both java and jess. The piece is defined by its position (on a Square), its colour, its number and its status (king or men). Each piece also knows their eight neighbour squares: the potential positions into which one the piece can move.
* @author k380h11
*/
final static boolean WHITE = true;
final static boolean BLACK = false;
/**
* This Method is used to set the number and color of a piece
* And it makes a call to the Board to also set it.
* @param number Piece'number
* @param color Piece's color
*/
public Piece(int number, boolean color) {
this.color = color;
this.number = number;
Board.getInstance().add(this);
}
/**
* This Method is also to set the number and color of a piece but it also
* sets the level and square
* And it makes a call method calculPos.
* @param number Piece'number
* @param color Piece's color
* @param square square move to
* @param level Piece's level
*/
public Piece(int number, boolean color, Grid.Square square, int level) {
this.color = color;
this.number = number;
this.level = level;
this.square = square;
this.virtual = true;
calculPos();
}
/**
* This Method initilizes the pieces where they are placed and of what color they are
*
*/
public void init() {
int row
= (int) Math.
ceil((number
)/4);
int col = (((row+1)%2)+(number%4)*2);
if (color == Piece.WHITE) {
row = Grid.ROW_MAX-row-1;
col = Grid.COL_MAX-col-1;
}
move( Grid.getSquare(row, col) );
}
/**
* jump the piece over a square
* @param squareToJump square to be jumped
*/
public void jump(Grid.Square squareToJump) {
Piece pieceToJump = Board.select(squareToJump.getRow(),squareToJump.getCol());
Board.getInstance().remove(pieceToJump);
squareToJump.setFree(true);
int newRow =squareToJump.getRow() + squareToJump.getRow() - square.getRow();
int newCol =squareToJump.getCol()+ squareToJump.getCol() - square.getCol() ;
Grid.Square moveto = Grid.getSquare(newRow, newCol);
move(moveto);
Grid.getInstance().repaint();
}
/**
* This Method is used to move a piece from one square to another.
* @param square square to meve the piece to
*/
public void move(Grid.Square square) {
if (!virtual)
this.square.setFree(true);
this.square = square;
if (!virtual)
this.square.setFree(false);
calculPos();
}
/**
* This Method is used to convert the piece information to a string
* @return a string containing the information of the piece.
*/
String retString
= (king
? "King" : "men")+
" " + (color ? "white":"red") +" "+ number + " on "+ square;
return retString ;
}
/**
* This Method finds out if two piece are equal same colour, number, square, level
* @return True if they are else false
* @param anObject object to be compared
*/
public boolean equals
(Object anObject
) {
if (anObject != null && anObject instanceof Piece) {
Piece piece = (Piece) anObject;
return ((piece.getNumber() == number && piece.getColor() == color)
&& (piece.square.equals(square)) && (piece.level == level));
}
else
return false;
}
/**
* This Method is used to get the number of a piece
* @return the number of a piece
*/
public int getNumber() {
return number;
}
/**
* This Method is used to set the number of a piece, from an older location
* @param number the number of the piece
*/
public void setNumber(int number) {
this.number = number;
}
/**
* This Method is used to get the square of a piece, from an older location
* @return the square.
*/
public Grid.Square getSquare() {
return square;
}
/**
* This Method is used to get the colour of a piece, form an older location
* @return the colour of the piece.
*/
public boolean getColor() {
return color;
}
/**
* This Method is used to get the next left square of the piece, from an ekstern location
* @return the next left squares location
*/
public Grid.Square getNLS() {
return nls;
}
/**
* This Method is used to get the next right square of the piece, from an ekstern location
* @return the next right squares location
*/
public Grid.Square getNRS() {
return nrs;
}
/**
* This Method is used to get the next-next left square of the piece, from an ekstern location
* @return the next-next left squares location
*/
public Grid.Square getNNLS() {
return nnls;
}
/**
* This Method is used to get the next-next right square of the piece, from an ekstern location
* @return the next-next right squares location
*/
public Grid.Square getNNRS() {
return nnrs;
}
/**
* This Method is used to get the previous left square of the piece, from an ekstern location
* @return the previous left squares location
*/
public Grid.Square getPLS() {
return pls;
}
/**
* This Method is used to get the previous right square of the piece, from an ekstern location
* @return the previous right squares location
*/
public Grid.Square getPRS() {
return prs;
}
/**
* This Method is used to get the 2*previous left square of the piece, from an ekstern location
* @return the 2*previous left squares location
*/
public Grid.Square getPPLS() {
return ppls;
}
/**
* This Method is used to get the 2*previous right square of the piece, from an ekstern location
* @return the 2*previous right squares location
*/
public Grid.Square getPPRS() {
return pprs;
}
/**
* This Method is used to get the level of the piece, from an ekstern location
* @return the level of the piece
*/
public int getLevel() {
return level;
}
/**
* This Method is used to set the level of the piece, for an ekstern location
* Set the level of the piece to level in the parameter
* @param level the level of the piece
*/
public void setLevel(int level){
this.level = level;
}
/**
* This Method is used to set the virtual effekt of a piece (but is not used)
* @param virtual if virtual
*/
public void setVirtual(boolean virtual) {
this.virtual = virtual;
}
/**
* This Method is used to read if a piece is virtual or not (but is not used)
* @return the virtual informantion TRUE or FALSE
*/
public boolean isVirtual() {
return virtual;
}
/**
* This Method is used to find out if a piece is a king or just a "man"
* @return the true if the piece is a king else false
*/
public boolean isKing() {
return king;
}
/**
* This Method is to add a propertychangelistener
* @param pcl the propertychangelistener object
*/
pcs.addPropertyChangeListener(pcl);
}
/**
* This Method is to remove a Propertychangelistener.
* @param pcl the propertychangelistener object
*/
pcs.removePropertyChangeListener(pcl);
}
/**
* This Method calculate the next legal positions of the piece and
* it set the piece to be a king if is on the backline of the board
* The pcs.
*
*
*/
private void calculPos() {
int mov = (color ? -1 : 1);
nls = Grid.getSquare(square.getRow() + mov, square.getCol() + mov);
nrs = Grid.getSquare(square.getRow() + mov, square.getCol() - mov);
pls = Grid.getSquare(square.getRow() - mov, square.getCol() + mov);
prs = Grid.getSquare(square.getRow() - mov, square.getCol() - mov);
mov = mov * 2;
nnls = Grid.getSquare(square.getRow() + mov, square.getCol() + mov);
nnrs = Grid.getSquare(square.getRow() + mov, square.getCol() - mov);
ppls = Grid.getSquare(square.getRow() - mov, square.getCol() + mov);
pprs = Grid.getSquare(square.getRow() - mov, square.getCol() - mov);
if (Grid.backLine(square, color))
king = true;
pcs.firePropertyChange("square", square, square);
pcs.firePropertyChange("nls", nls, nls);
pcs.firePropertyChange("nrs", nrs, nrs);
pcs.firePropertyChange("pls", pls, pls);
pcs.firePropertyChange("prs", prs, prs);
pcs.firePropertyChange("nnls", nnls, nnls);
pcs.firePropertyChange("nnrs", nnrs, nnrs);
pcs.firePropertyChange("ppls", ppls, ppls);
pcs.firePropertyChange("pprs", pprs, pprs);
pcs.firePropertyChange("king", king, king);
}
/**
* The default values of the piece's parameters.
*/
private boolean color;
private int actionPoint = 0;
private int number = 0;
private boolean king = false;
private boolean virtual = false;
private int level = 0;
private Grid.Square square = Grid.getInstance().new Square();
private Grid.Square nls = Grid.getInstance().new Square();
private Grid.Square nrs = Grid.getInstance().new Square();
private Grid.Square nnls = Grid.getInstance().new Square();
private Grid.Square nnrs = Grid.getInstance().new Square();
private Grid.Square pls = Grid.getInstance().new Square();
private Grid.Square prs = Grid.getInstance().new Square();
private Grid.Square ppls = Grid.getInstance().new Square();
private Grid.Square pprs = Grid.getInstance().new Square();
}