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
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