package checkers;
import java.util.LinkedList;
import java.util.ListIterator;
import jess.*;
/**
* The board is the representation of the whole 24 pieces. When this one is first declared, all the piece are instantiated, added into the Jess database, placed at their initial position and finally asserted into the parent LinkedList
*/
static private Board board = new Board ();
/**
* This method calls the super method.
*/
private Board () {
super ();
}
/**
* This method is used to initilize the board.
* @throws JessException if it is not able to execute the jess command
*/
void init () {
for (int number=0; number<12; number++) {
boolean color=Piece.BLACK;
for (int c=0; c<2; c++) {
color = !color;
String Jesscmd
= "(definstance Piece " +
"(new checkers.Piece "+ number
+" "+Boolean.
toString (color
).
toUpperCase () +")" +
" dynamic)";
try {
Checker.r.executeCommand (Jesscmd);
} catch (JessException e
) {System.
err.
println (e
);}
Piece piece = Board.select (number,color);
piece.init ();
}
}
}
/**
* This method finds which piece is located in at specify square.
* @return the piece if there is one else null.
* @param row square's row position
* @param col square's col position
*/
static public Piece select (int row, int col){
while (it.hasNext ()) {
Piece piece = (Piece) it.next ();
if ((piece.getSquare ().getRow() == row)
&& (piece.getSquare ().getCol() == col)) {
return (Piece) piece;
}
}
return null;
}
/**
* This method finds a spicified piece on the board if it exits
* @return the piece if there is one else null.
* @param number the number of the player's piece looked for
* @param color the color of the player's piece looked for
*/
static public Piece select (int number, boolean color){
while (it.hasNext ()) {
Piece piece = (Piece) it.next ();
if (piece.getNumber () == number && piece.getColor() == color) {
return (Piece) piece;
}
}
return null;
}
/**
* This method count how many pieces a player has on the board.
* @return how many pieces the player is left with.
* @param color the color of the player.
*/
static public int count (boolean color){
int count = 0;
while (it.hasNext ()) {
Piece piece = (Piece) it.next ();
if (piece.getColor() == color) {
count ++;
}
}
return count;
}
/**
* This method takes an object turns it into a string
* @return a string that contains the text version of containts in the fact address
*/
while (it.hasNext ()) {
retString += (Piece) it.next ();
}
return retString;
}
/**
* This method is used get all the information there is about the board.
* @return the information about the board.
*/
static public Board getInstance () {
return board;
}
}