我正在使用Minimax算法在WPF C#中制作Tic Tac Toe游戏。我认为我的算法工作正常,但我无法理解我应该怎样做才能从调用函数Minimax获得下一个状态。现在它只返回最终状态。算法的代码如下:
Move MiniMax(char[,] state, bool playerMAX, int depth)
{
if (depth == 0 || CountEmptyFields(state).Count() == 0 || CheckIfStateIsList(state)) //check if state is terminal or we reached depth
{
Move ret = new Move();
ret.SetState(state);
ret.SetHV(HeuristicValue(state)); //method that calculates heuristic value
return ret;
}
Move choice = new Move(); //class that has two variables: state and value
int val = 0;
if (playerMAX)
{
val = -10000;
}
else
{
val = 10000;
}
List<char[,]> M = new List<char[,]>();
M = GeneratePossibleMoves(state, playerMAX); //function that generates all possible moves
foreach(char[,] child in M)
{
Move candidate = new Move();
candidate = MiniMax(child, SwitchPlayer(playerMAX), depth - 1);
if (playerMAX && candidate.GetHV()>val)
{
val = candidate.GetHV();
choice.SetHV(candidate.GetHV());
choice.SetState(candidate.GetState());
}
else if(!playerMAX && candidate.GetHV() < val)
{
val = candidate.GetHV();
choice.SetHV(candidate.GetHV());
choice.SetState(candidate.GetState());
}
}
return choice;
}
正如我所说,算法返回游戏的最终状态......我怎样才能获得下一步?请帮忙。