📄 backgammonevaluation301105669.java
字号:
/**File: BackgammonEvaluation301105669.java
*CMPT310 Assignment 2 - Part2
*@author: Yifu Diao
*@email: ayd@sfu.ca
*/
//package bkgm;
public class BackgammonEvaluation301105669 extends Evaluation
{
public BackgammonEvaluation301105669()
{
MAX_SCORE = (BackgammonBoard.CHECKERS_PER_player_num+Dice.DIE_MAX)*BackgammonBoard.NUM_POINTS*BackgammonBoard.NUM_POINTS;
MIN_SCORE = -MAX_SCORE;
}
//The following function checks the number of single checkers that can be hit by our opponent and penalise for it
private int penaliseForOneChecker(BackgammonBoard b, int player_num_num)
{
int SingleCheckerAmount=0;
if(player_num_num==1)
{
int i=b.NUM_POINTS-1;
//skip the checkers that cannot be threatened
while(i>=0 && b.board[i]>=0)
i--;
for(int y=0;y<i;y++)
{
if(b.board[i]==1)
SingleCheckerAmount++;
}
}
else
{
int i=0;
//skip the checkers that cannot be threatened
while(i<b.NUM_POINTS && b.board[i]<=0)
i++;
for(int y=(i+1);y<b.NUM_POINTS;y++)
{
if(b.board[i]==-1)
SingleCheckerAmount++;
}
}
return SingleCheckerAmount;
}
//The following function gives bonus for having a row
//of points of at least two checkers per point near the
//opponent's home
private int primeGameEvaluation(BackgammonBoard, int player_num)
{
int maxTwo=0; //at most two checkers per point
int LeatTwo=0; //at least two checkers per point
for(int i=(player_num+13*(1-player_num));i<(11*player_num+23*(1-player_num));i++)
{
if((player_num*b.board[i]-1+player_num)>(player_num+(1-player_num)*b.board[i]))
LeatTwo++;
else
{
if(LeatTwo>maxTwo)
maxTwo=LeatTwo;
LeatTwo=0;
}
}
if(LeatTwo>maxTwo)
maxTwo=LeatTwo;
return maxTwo*b.NUM_POINTS;
}
//The following function gives a bonus to blocking an opponent's checker on the base
private int blitzEvaluation(BackgammonBoard b, int player_num_num)
{
int k=0;
int i=(1-player_num_num)*(b.NUM_POINTS-1);
while((b.board[i]*player_num_num+player_num_num-1)>(player_num_num+b.board[i]*(1-player_num_num))&&(k<7))
{
k++;
i+=2*player_num_num-1;
}
return k*b.NUM_POINTS;
}
//This function is used to evaluate the board in the following way:
//It penalises you for the checkers you have on bar and rewards you
//for the opponents checkers on bar.
//It weights your checkers more if they are closer to your home,
//giving you the maximum weight for the checkers that are at home
//and penalises you for the opponent's checkers that are close
//to his or her home.
private int evaluateCheckers(BackgammonBoard b, int player_num)
{
int value=0;
for (int i=0;i<b.NUM_POINTS;i++)
{
if(b.board[i]>0)
value= value+player_num*b.board[i]*i-(1-player_num)*b.board[i]*i;
else
value= value-player_num*b.board[i]*(b.NUM_POINTS-i-1)+(1-player_num)*b.board[i]*(b.NUM_POINTS-i-1);
}
value+=b.in_home[player_num]*b.NUM_POINTS;
value-=b.in_home[1-player_num]*b.NUM_POINTS;
value-=b.on_bar[player_num]*b.NUM_POINTS;
value+=b.on_bar[1-player_num]*b.NUM_POINTS*b.NUM_POINTS;
return value;
}
//This function returns the board evaluation score given the board and the number of player_num from whose
//perspective we are evaluating the board
public float boardScore(BackgammonBoard b, int player_num)
{
//decide which strategy to adopt
int tmpEval=evaluateChecker(b,player_num);
boolean blitzGame=false;
if (tmpEval<=2*b.NUM_POINTS*b.NUM_POINTS && !blitzGame)
//it's a priming game
{
tmpEval+=primeGameEvaluation(b,player_num);
}
if(b.on_bar[1-player_num]>0)
{
//it's a blitz game
tmpEval+=blitzEvaluation(b,player_num);
blitzGame=true;
}
//At last, penalise for 1-checker points
tmpEval-=penaliseForOneChecker(b,player_num);
return (float)tmpEval;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -