PDA

View Full Version : MiniMax algorithm problem.



Toc777
February 19th, 2009, 07:54 PM
Hi everyone,
I am trying to create a connect 4 game and i am using a min max algorithm to implement the AI. I have been debugging this program for ages and it seems like the code is sound but it just doesn't give the right results. Here is the code, if anyone has any ideas on where i am going wrong please tell me. Thanks a lot.


private int bestMove;
private double bestScore;

public void minMaxAlgorithm(GameBoard currentBoard, int depth)
{
Deque<Column> validMoves = getValidMoves(currentBoard);
GameBoard newBoard;
Column currentUnfilledColumn;
int boardScore;
while(!validMoves.isEmpty())
{
newBoard = copyGameBoard(currentBoard);
currentUnfilledColumn = validMoves.pop();
newBoard.dropCounter(currentUnfilledColumn.getColu mnNumber());
boardScore = evalBoard(newBoard,currentUnfilledColumn);

if(depth != 0)
{
newBoard.changeCurrentPlayer();
minMaxAlgorithm(newBoard,depth-1);
newBoard.changeCurrentPlayer();
}//end if

if(newBoard.getCurrentPlayer().isComputer())
{
if(boardScore>bestScore)
{
bestScore = boardScore;
bestMove = currentUnfilledColumn.getColumnNumber();
}//end if
}//end if
else
{
if(boardScore<bestScore)
{
bestScore = boardScore;
bestMove = currentUnfilledColumn.getColumnNumber();
}//end if
}//end else

}//end while
}//end method

rrh
February 20th, 2009, 12:34 PM
Does evalBoard() always evaluate the score for the computer, or for the current player for the board?

Also maybe you could return an object containing bestScore and bestMove, rather than make them global variables.

Toc777
February 22nd, 2009, 05:40 PM
Hi thanks for the reply,
The evalBoard() method evaluates the board for the last player to drop a counter into the board. It returns a positive value if the computer is the current player and a negative value if the computer is not the current player.

rrh
February 23rd, 2009, 02:28 PM
Like I said I think the major thing is you should have it return an object or an array which contains bestMove and bestScore, rather than have them global.

You don't want to use the bestMove and bestScore from another depth and another board position.