I was trying to get the Negmax algorithm in my program. I understood it, but I am also a little doubt on the algorithm anyway. But after currentDepth reach to depth's maximum, it has to stop this loop. Please help me to solve this?
In other words, if the current depth is larger compared with maximum depth, it must stop the loop. But it will not happen here, please help me stop this loop. Thanks in advance.
However, Here is the problem, I put some methods also, but it is not worked, Therefore, I deleted and put as comments what I had experimented. This is a part of my code:
if(currentGameDepth == maximumGameDepth)
{
// I included some methods, but it won't execute to out of the loop
// I tried to return Mathf.Infinite(), but it is not work..
// please help me
//-----------------------------------------------------------------
}
The full code given as follows:-
Here you can see, two places add NegamaxAlgorithm
keyword:
The Negmax Algorithm is down here:-
private static float NegamaxAlgorithm(Piece game, float maximumGameDepth, float currentGameDepth, GamesModel moves)
{
if(currentGameDepth == maximumGameDepth)
{
// I included some methods, but it won't execute to out of the loop
// I tried to return Mathf.Infinite(), but it is not work..
// please help me
//-----------------------------------------------------------------
}
bestGameMove = null;
float bestGameScore = Mathf.NegativeInfinity;
foreach (GamesModel m in MovesList1)
{
Moves move = Piece.AccordaingToEvaluationWhiteSide(m,game);
float recursedGameScore;
Moves currentGameMove = move;
recursedGameScore = NegamaxAlgorithm(game , maximumGameDepth , currentGameDepth + 1, currentGameMove);
float currentGameScore = -recursedGameScore;
if (currentGameScore > bestGameScore)
{
bestGameScore = currentGameScore;
bestGameMove = m;
}
}
return bestGameScore;
}
..
The issue is probably that you are using floats to represent an integer depth and the ==
comparison is failing due to floating point precision issues.
Try something like:
private static float NegamaxAlgorithm(Piece game,
int maximumGameDepth, int currentGameDepth, GamesModel moves)
{
if(currentGameDepth >= maximumGameDepth)
{
// terminate recursion
return float.MinValue;
}
else
{
// ... use recursion to calculate next level ...
return bestGameScore;
}
}