【发布时间】:2014-07-13 07:06:15
【问题描述】:
我正在开发一个国际象棋游戏,现在尝试实现一个极小极大算法,该算法使用一个棋子方表和棋子值来计算分数。我已经能够创建以下显示的方法,但不知道如何在 CalculateScore() 和 evaluatePieceScore() 方法中计算计件分数。拜托,我真的很困惑,需要帮助。
1) getPieceValue() 该方法返回分配给每个棋子的常量值 2) getScoreForPiecePosition() 获取指定位置的评价奖励 3) CalculateScore() 使用棋盘表计算并返回分数。 4) evaluatePieceScore() 根据轮到谁,将CalculateScore() 计算出的分数加到原始分数上。
public class MinimaxPlayer implements IPlayerHandler, EvaluationAlgorithm{
private Game chessgame;
private MoveChecker checker;
private BoardGUI boardgui;
private static int maxDepth = 2;
public MinimaxPlayer(Game chessgame){
this.chessgame = chessgame;
this.checker = chessgame.getMoveChecker();
this.boardgui = new BoardGUI(chessgame);
}
@Override
public int getPieceValue(int type) {
switch(type){
case Piece.TYPE_PAWN: return 100;
case Piece.TYPE_KNIGHT: return 320;
case Piece.TYPE_BISHOP: return 325;
case Piece.TYPE_ROOK: return 500;
case Piece.TYPE_QUEEN: return 975;
case Piece.TYPE_KING: return 32767;
default: throw new IllegalArgumentException("Unknown piece type: "+ type);
}
}
@Override
public int evaluatePieceScore() {
int scoreBrown = 0;
int scoreYellow = 0;
for (Piece piece : this.chessgame.getPieces()) {
if(piece.getColor() == Piece.BROWN_COLOR){
scoreBrown +=
getScoreForPieceType(piece.getType());
scoreBrown +=
getScoreForPiecePosition(piece.getRow(),piece.getColumn());
}else if( piece.getColor() == Piece.YELLOW_COLOR){
scoreYellow +=
getScoreForPieceType(piece.getType());
scoreYellow +=
getScoreForPiecePosition(piece.getRow(),piece.getColumn());
}else{
throw new IllegalStateException(
"unknown piece color found: "+piece.getColor());
}
}
// return evaluation result depending on who's turn it is
int gameState = this.chessgame.getGameState();
if( gameState == Game.GAME_STATE_BROWN){
return scoreBrwon - scoreYellow;
}else if(gameState == Game.GAME_STATE_YELLOW){
return scoreYellow - scoreBrown;
}else if(gameState == Game.GAME_STATE_END_YELLOW_WON
|| gameState == Game.GAME_STATE_END_BROWN_WON){
return Integer.MIN_VALUE + 1;
}else{
throw new IllegalStateException("unknown game state: "+gameState);
}
}
public int CalculateScore(int index, int pieceValue, int[] SquareTable ){
int score = pieceValue;
for(int i = 0; i < SquareTable.length; i++){
score += getPieceValue(index.get(SquareTable[i]));
}
return 0;
}
@Override
public int getScoreForPiecePosition(int row, int column) {
byte[][] positionWeight =
{ {1,1,1,1,1,1,1,1}
,{2,2,2,2,2,2,2,2}
,{2,2,3,3,3,3,2,2}
,{2,2,3,4,4,3,2,2}
,{2,2,3,4,4,3,2,2}
,{2,2,3,3,3,3,2,2}
,{2,2,2,2,2,2,2,2}
,{1,1,1,1,1,1,1,1}
};
return positionWeight[row][column];
}
private static int[] PawnSquareTable = new int[]
{
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
};
private static int[] KnightSquareTable = new int[]
{
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50,
};
private static int[] BishopSquareTable = new int[]
{
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20,
};
private static int[] RookSquareTable = new int[]
{
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0,
};
private static int[] QueenSquareTable = new int[]
{
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20,
};
private static int[] KingMiddleGameSquareTable = new int[]
{
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20,
};
private static int[] KingEndGameSquareTable = new int[]
{
-50,-40,-30,-20,-20,-30,-40,-50,
-30,-20,-10, 0, 0,-10,-20,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-30, 0, 0, 0, 0,-30,-30,
-50,-30,-30,-30,-30,-30,-30,-50,
};
public static int[] reverse(int[] arr){
ArrayUtils.reverse(arr);
return arr;
}
}
【问题讨论】:
-
你能用文字逐行描述这些方法应该做什么吗?
-
@ChiefTwoPencils,我已经更新了问题。
-
您对在计算中使用 PawnSquareTable、KnightSquareTable 等有疑问吗?
-
@philippelhardy,是的,这是我的问题。
标签: java artificial-intelligence chess