*/
public void defense(Player player) {
//get an array containing enemy piece and the highest value piece it can capture
Piece[] highestValPiece = highestValuePieceAbleToCapture(player.getEnemySide());
//get the highest value piece that enemy can capture
Piece pieceInDanger = highestValPiece[1];
//find possible squares where piece can move
ArrayList<ArrayList<Integer>> possibleMoves = move.possiblePieceMoves(pieceInDanger, false);
ArrayList<Integer> possibleMovesX = possibleMoves.get(0); //get x coordinates
ArrayList<Integer> possibleMovesY = possibleMoves.get(1); //get y coordinates
//find possible squares enemy pieces can move
ArrayList<ArrayList<Integer>> possibleEnemyMoves = getAllSquaresPossibleToMove(player.getEnemySide());
ArrayList<Integer> possibleEnemyMovesX = possibleEnemyMoves.get(0); //get x coordinates
ArrayList<Integer> possibleEnemyMovesY = possibleEnemyMoves.get(1); //get y coordinates
//find a square own piece can move safely
/*
* compare possible own piece movements to enemy piece movements to find
* a safe square.
*/
ListIterator<Integer> pmxList, pmyList; //iterators for possible own piece moves
pmxList = possibleMovesX.listIterator(); //row iterator
pmyList = possibleMovesY.listIterator(); //column iterator
//convert arraylist of enemy x movement coordinates into an array
int[] pexArray = new int[possibleEnemyMovesX.size()];
for (int i=0; i < pexArray.length; i++) {
pexArray[i] = possibleEnemyMovesX.get(i).intValue();
}
//convert arraylist of enemy y movement coordinates into an array
int[] peyArray = new int[possibleEnemyMovesY.size()];
for (int i=0; i < peyArray.length; i++) {
peyArray[i] = possibleEnemyMovesY.get(i).intValue();
}
int pmx=0, pmy=0, pex, pey;
boolean match = true;
//check possible own piece moves until safe square is found or possible moves run out
while (pmxList.hasNext() && pmyList.hasNext() && match) {
match = false; //reset to false
//get next own piece move
pmx = pmxList.next();
pmy = pmyList.next();
enemy: for (int i=0; i<pexArray.length; i++) {
//get enemy piece move
pex = pexArray[i];
pey = peyArray[i];
//compare
//if match found no need to check other enemy moves
if (pmx==pex && pmy==pey) {
match = true;
break enemy;
}
}
}
//if none of the enemy moves match -> move own piece to that square
if (!match) {
move.doMove(player, board.coordinatesToNotation(pieceInDanger.getRow(),
pieceInDanger.getCol()), board.coordinatesToNotation(pmx, pmy));
} else if (match) {
/*
* if the piece can't be safed by moving it to a safe square
* try to trade a less valuable piece.
*