Jump to content

Alpha-Beta Algorithm

- - - - -

  • Please log in to reply
No replies to this topic

#1
splinter500

splinter500

    Newbie

  • Members
  • Pip
  • 1 posts
Hi guys, I am new to this forum and sorry to say..I need some help. I am making a Connect 4 game that utilizes Alpha-Beta Algorithm but it is not working at all. All the AI does is just stick its piece in the first column and when that is filled, it sticks it in the last column.

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users