Here is my algorithm for AlphaBeta
This is my code below.
public Node AlphaBetaSearch2(Node state, AI ai, Boolean playerTurn, double alpha2, double beta2){
if(state.play.checkWin(state.play)||state.depth == max){
return state;
}
expanded = 0;
threeinarow = false;
Node alphaNode = null;
Node betaNode = null;
//the resulted node will be the longtem goal
if(playerTurn==false){ //it is max turn
state.expand(state.play, ai, this,false);
for(int i = 0; i<7;i++){
if(state.child.get(i) != null){
Node minState = AlphaBetaSearch2(state.child.get(i),ai, true, alpha,beta);
if(minState.score>alpha){// || (value ==minState.score && minState.depth< valueNode.depth)){
alpha = minState.score;
alphaNode = minState;
}
if(alpha >=beta)
return state;
}
}
if(alphaNode == null){//for some reason alphaNode can be null which I have no IDEA WHY?! It should never be null. Alpha is -inf and beta is +inf, so it should never return null.
return state;
}
return alphaNode;
}
else{//it is mins turn
state.expand(state.play, ai, this,true);
for(int i = 0; i<7;i++){
if(state.child.get(i) != null){
Node maxState = AlphaBetaSearch2(state.child.get(i), ai, false,alpha,beta);
if(maxState.score<beta){// || (value ==maxState.score && maxState.depth< valueNode.depth)){
beta = maxState.score;
betaNode = maxState;
}
//if(alpha>=beta)
//return state;
}
}
if (betaNode==null){
return state;
}
return betaNode;
}
}
Hopefully my code is understandable. If you need some clarification on what object does what, please feel free to ask.
Edited by splinter500, 16 December 2011 - 04:30 AM.


Sign In
Create Account

Back to top









