A.I Rookie needs help on MiniMax Algorithm
Hello To everyone,
First of all I want to say thanks to the creator(s) of this site and mostly to all knowledge members because without you all then rookies like me are in serious troubles. With that said, I need a thorough explanation on how the MiniMax is coded in Java. I am currently trying to implement the algorithm with a simple game of Tic-Tac-Toe. My problem arises in that I do understand how the algorithm works underneath but you see I must admit that I do have this issue of debugging how the recursion bit works (which seems to be my problem). Also with my experience in Java I have noticed that the problem may be because of so much referencing in Java, so along the way I may be mixing up my values.
BreakDown :
I have a simple GUI displaying the GamePlay, have a class that keeps track of the GameState (positions filled with pieces and also positions set to true or false if a spot is taken vice-versa). The GameState class contains two 2D arrays one for the Pieces the other for positions represented by boolean values to determine if a spot is taken as said previously (The Pieces Class contains information on the Type of piece -"X" or "O" and the owner of the piece, p1 or p2 (human or AI) ). Also I have a class called bestmoves that stores the bestmove in a size 2 array (first slot for x position on my GUI, 2nd slot for the y position).......
Issue : The algorithm runs pretty well, when I force it to break out of the recursion on determining the value compared between Min & Max, but then the AI (whether playing as Min or Max) only tends to go for the first available spot (i.e: PLAYS DUMB)....
I would highly appreciate if anyone with Stronger knowledge of Java (preferably Recursion help me on this).... I can post up a snippet of the MiniMax bit
public BestMoves alphaBetaMiniMax(boolean side)
{
System.out.println("::: Calling alpha beta MiniMax :::");
Player temp;
if(side == user1.getTurn())
temp = user1;
else
temp = user2;
BestMoves myBest = new BestMoves();
BestMoves reply;
if( GS.WinOrLose( temp ) == true || GS.full() == true )
return (new BestMoves(GS));
else
System.out.println("::::: GRIDS ARE STILL EMPTY & NO WINNER YET FOUND :::::");
if(side == true)
{
// for side = true --- this is for the MAX algorithm looking for a Lower number
myBest.setScore( -2 );
}
else
{
// for side = true --- this is for the MIN algorithm looking for a Higher number
myBest.setScore( 2 );
}
// GS is the gameState checking for legalmoves for the current player
ArrayList<int[]> legal = GS.getLegalMoves();
for(int i = 0; i < legal.size(); i++)
{
int x = (legal.get(i))[0];
int y = (legal.get(i))[1];
Pieces[][] p = GS.getGameState();
GS.performMove( x , y , temp , p );
reply = alphaBetaMiniMax( !side );
System.out.println("about to undo");
GS.undoMove();
if( ((side == true) && (reply.getScore() > myBest.getScore() ) ||
((side == false) && (reply.getScore() < myBest.getScore() ) ) ) )
{
System.out.println("Decision Made");
System.out.println("my best score so far = "+myBest.getScore());
System.out.println("my reply score so far = "+reply.getScore());
int [] arr = new int[2];
arr[0] = x;
arr[1] = y;
myBest.setMove( arr );
myBest.setScore( reply.getScore() );
break;
// forcing a break out or else it Jams up
}
}// END OF FOR-LOOP
return myBest;
}
// the GS.undoMove() bit merely restores the initial gamestate before calling the minimax..
HUGE THANKS TO EVERYONE AHEAD ============ CANT WAIT TO HEAR YOUR RESPONSES....
I haven't checked your code for bugs, but:
You want the computer to take the earliest possible win and fear the earliest possible loss. Make a win in 1 move worth 9 points, and a win in 8 moves worth 1 point. Otherwise the computer will make all kinds of tactically sound moves that leave you scratching your head. :)
Another thing you could do, to make sure that there aren't any bugs, is to 'feed' your function a board like this:
o o x
x o .
x . .
and then step through the code to see what the computer thinks about each move.
You want the computer to take the earliest possible win and fear the earliest possible loss. Make a win in 1 move worth 9 points, and a win in 8 moves worth 1 point. Otherwise the computer will make all kinds of tactically sound moves that leave you scratching your head. :)
Another thing you could do, to make sure that there aren't any bugs, is to 'feed' your function a board like this:
o o x
x o .
x . .
and then step through the code to see what the computer thinks about each move.
Thanks WillH .... I would try out exactly what you said, although am a bit confused what you mean by feeding the function a certain board, do you mean for everytime I run the MiniMax algorithm, or do you mean I test it out that way to ssee if I get bugs... either way I'll try it out, but if you do have the time, kindly help me see if there are any bugs in the code I posted, as I am kind of fed up with the code and its driving me off the edge (the Recursion bit is annoying ).... Once again , thanks for checking it out...
I mean just to test it out, not every time. :)
You definately have a bug if it's not making a move to either tie or win.
You definately have a bug if it's not making a move to either tie or win.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement