Show Analyzer.java syntax highlighted
package chess.logic;
import java.util.Enumeration;
import chess.data.Piece;
import chess.data.Square;
/**
* Make analysis about the state of all pieces.
* Uses utitily-based function for this, taking
* in account the preferred situation of the pieces
* and material factors.
*
* @author Juho Karppinen
* @version $Id: Analyzer.java 102 2004-11-12 14:31:37Z jkarppin $
*/
public class Analyzer {
// bonus points by pawns' position
public static final int [] pawnPositions = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 3, 4, 5, 4, 3, 2,
3, 4, 5, 8, 5, 5, 4, 3,
4, 5, 6, 7, 7, 6, 5, 4,
5, 6, 7, 8, 8, 7, 6, 5,
6, 7, 8, 9, 9, 8, 8, 7,
7, 8, 9, 10, 10, 9, 8, 7 };
// bonus points by knights' position
public static final int [] knightPositions = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 5, 5, 1, 0, 0,
1, 3, 5, 6, 6, 5, 3, 1,
1, 3, 5, 6, 6, 5, 3, 1,
0, 0, 1, 6, 6, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 , 0 };
// bonus points by bishops' position
public static final int [] bishopPositions = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 4, 0,
0, 3, 5, 3, 3, 5, 3, 0,
0, 0, 0, 6, 6, 0, 0, 0,
0, 0, 0, 6, 6, 0, 0, 0,
0, 3, 5, 3, 3, 5, 3, 0,
0, 4, 0, 0, 0, 0, 4, 0,
0, 0, 0, 0, 0, 0, 0, 0 };
// bonus points by rooks' position
public static final int [] rookPositions = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 5, 5, 1, 0, 0,
1, 3, 5, 6, 6, 5, 3, 1,
1, 3, 5, 6, 6, 5, 3, 1,
0, 0, 1, 6, 6, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 , 0 };
// bonus points by quens' position
public static final int [] queenPositions = {
0, 0, 0, 3, 3, 0, 0, 0,
0, 0, 0, 3, 3, 0, 0, 0,
0, 0, 2, 5, 5, 2, 0, 0,
1, 3, 5, 6, 6, 5, 3, 1,
1, 3, 5, 6, 6, 5, 3, 1,
0, 0, 1, 6, 6, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 , 0 };
// bonus points by kings' position
public static final int [] kingPositions = {
3, 5, 3, 1, 1, 3, 5, 3,
3, 3, 2, 0, 0, 2, 3, 3,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0 };
private GameState state;
public Analyzer(GameState gamestate) {
this.state = gamestate;
}
/**
* Calculate the value of current situation,
*
* @param white is white color
* @return positionValues + 50*materialValues + checkValue
*/
public int analyze(boolean white) {
int posValue = 0;
int matValue = 0;
int checkValue = 0;
for(Enumeration e=state.allPositions();e.hasMoreElements();){
int position = ((Integer) e.nextElement()).intValue();
Piece piece = state.pieceAt(position);
int pVal = 0;
int mVal = 0;
if(piece.getType() == Piece.PAWN){
pVal = getPositions(piece.getColor(), position, pawnPositions);
mVal = 1;
} else if(piece.getType() == Piece.BISHOP){
pVal = getPositions(piece.getColor(), position, bishopPositions);
mVal = 3;
} else if(piece.getType() == Piece.KNIGHT){
pVal = getPositions(piece.getColor(), position, knightPositions);
mVal = 3;
} else if(piece.getType() == Piece.KING) {
pVal = getPositions(piece.getColor(), position, kingPositions);
mVal = 10;
} else if(piece.getType() == Piece.ROOK) {
pVal = getPositions(piece.getColor(), position, rookPositions);
mVal = 5;
} else if(piece.getType() == Piece.QUEEN) {
pVal = getPositions(piece.getColor(), position, queenPositions);
mVal = 8;
}
if(white) {
posValue += pVal;
matValue -= mVal;
} else {
posValue -= pVal;
matValue += mVal;
}
}
//System.out.println(matValue);
return posValue + 50 * matValue + checkValue;
}
/**
* Get position value
* @param myColor
* @param myIndex
* @param values
* @return
*/
private int getPositions(int myColor, int myIndex, int[] values) {
int row = Square.getColumn(myIndex);
int col = Square.getRow(myIndex);
if(myColor == Piece.BLACK) {
col = 9-col;
}
int tableIndex = 8*(col-1) + row - 1;
return values[tableIndex];
}
/**
* Is one of the players checking the other player
* @param side
* @return
*/
public static boolean isCheck(int side) {
if(side == Piece.WHITE) {
} else {
}
return false;
}
}
See more files for this project here