Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
new endgame feature: distance to opponent
  • Loading branch information
sos13004 committed Apr 30, 2017
1 parent ff46d67 commit 606c0b0
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 72 deletions.
54 changes: 33 additions & 21 deletions src/CheckersGameState3.java
@@ -1,5 +1,6 @@
import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;

public class CheckersGameState3 implements CheckersGameState{

Expand Down Expand Up @@ -401,7 +402,7 @@ public class CheckersGameState3 implements CheckersGameState{
11: ^ same but for the two smaller diagonals
12: bridge pattern TODO!!!
13: triangle pattern TODO
14: dog pattern TODO
14: dog pattern TODO, also take out useless features
]
*/
private boolean king(int piece){
Expand Down Expand Up @@ -456,41 +457,52 @@ public class CheckersGameState3 implements CheckersGameState{
/* computes feature vector:
[0: piece-ratio,
1: num attacking pieces
2: central pieces
4: opponents kings are on the side.
2: dist to oppon
]
*/
public double[] getEndGameFeatures(int player){
double[] features = new double[4];
double[] features = new double[3];
double total = 0.0;
double mypieces = 0.0;
ArrayList<Integer> mykingloc = new ArrayList<Integer>();
ArrayList<Integer> opponpieceloc = new ArrayList<Integer>();
for(int i = 0; i<this.board.length; i++){
if(i%9!=8){ //valid square
if(this.board[i] != 0 ) total+=1.0;
/****my pieces (pawns and kings)*****/
if(myPiece(this.board[i], player)){
mypieces+=1.0;
/*****pawns features****/
if(this.board[i] == player){
if(i == 10 || i == 11 || i == 14 || i == 15 || i == 19 || i == 20 || i == 23 || i ==24){
features[2] +=1.0; //central pawns
}
}
/****kings features****/
else if(this.board[i] == player+2){
if(i == 10 || i == 11 || i == 14 || i == 15 || i == 19 || i == 20 || i == 23 || i ==24){
features[2] +=2.0; //central kings
if(this.board[i] != 0 ) {
total+=1.0;
if(myPiece(this.board[i], player)){
mypieces+=1.0;
if(this.board[i] == player+2){ //king
mykingloc.add(i);
}
}
else{
opponpieceloc.add(i);
}
}
}
}
}
features[0] = mypieces/total; //piece ratio
features[1] = numAttacking(player);
features[3] = opponKingsOnSide(player);
features[2] = distToOppon(mykingloc, opponpieceloc);
return features;
}

/* takes in list of all my and all my opponent's pieces locations on the board
and returns an accumulative sum of the distances between our pieces*/
private double distToOppon(ArrayList<Integer> myloc, ArrayList<Integer> opploc){
int myrow, opprow;
int sum = 0;
for(int i = 0; i< myloc.size(); i++){
myrow = (myloc.get(i)-((myloc.get(i))/9))/4;
for(int j=0; j< opploc.size(); j++){
opprow = (opploc.get(j)-((opploc.get(j))/9))/4;
sum += Math.abs(myrow - opprow);
}
}
return sum * 1.0;
}

/* number of pawns and kings on the long diagonal*/
public int numOnMainDiag(int player){
int count = 0;
Expand Down Expand Up @@ -675,7 +687,7 @@ public class CheckersGameState3 implements CheckersGameState{
}

public boolean isEndGame(){
int mypieces = 0, others = 0, maxPieces =4;
int mypieces = 0, others = 0, maxPieces =3;
for(int i=0; i<board.length; i++){
if(board[i]!=0){
if(myPiece(board[i],player)) mypieces+=1;
Expand Down
4 changes: 2 additions & 2 deletions src/LearningEvaluator.java
Expand Up @@ -41,9 +41,9 @@ public class LearningEvaluator extends BaseEvaluator{
System.out.println(Arrays.toString(params.get(i)));
}
//System.out.println(pars);
reg.newSampleData(vals, pars); //add data
reg.setNoIntercept(true);
try {
reg.newSampleData(vals, pars); //add data
reg.setNoIntercept(true);
double[] new_weights = reg.estimateRegressionParameters(); //get parameters
for(double x: new_weights){
if(Math.abs(x) > 100000){
Expand Down

0 comments on commit 606c0b0

Please sign in to comment.