Search code examples
minimax

How to store the first step of best value in a minimax tree?


I have a minimax tree and an evaluation function.The minimax function return only an integer(best value).How can i store the first move of the way of the founded best value ? here's my code :

    int Brain::MiniMax(GameBoard gb, int depth,int Turn,int lastcount) //0->Max 1->Min
{   
    if (depth == 5)
        return Evaluation(lastcount, gb);
    int bestval = 0;
    if (Turn == 0)
    {
        bestval = -100;
        vector<pair<int, pair<int, int>>> possibleFences = this->PossibleFences(gb);
        for (int i = 0; i < possibleFences.size(); i++)//ForFences
        {
            int cnt = 0;
            NextShortestPathMove(cnt, gb.OpponentPawn,gb);
            if (gb.CanPutFence(possibleFences[i].first, possibleFences[i].second.first, possibleFences[i].second.second) == 0)
                continue;
            gb.PutFence(possibleFences[i].second.first, possibleFences[i].second.second, possibleFences[i].first);          
            int value = MiniMax(gb, depth + 1,1, cnt);
            if (value > bestval)
            {
                bestval = value;
                move = possibleFences[i];
            }
        }
        return bestval;
    }
    else if (Turn == 1)
    {
        bestval = +100;
        int** possibleMoves = this->PossibleMoves(gb.OpponentPawn.Row, gb.OpponentPawn.Column, gb.OpponentPawn,gb);

        for (int i = 0; i < 6; i++)
        {
            if (possibleMoves[i][0] == -1)
                continue;
            int cnt = 0;
            NextShortestPathMove(cnt, gb.OpponentPawn,gb);
            gb.MoveOpponentPlayer(possibleMoves[i][0], possibleMoves[i][1]);
            int value = MiniMax(gb, depth + 1, 0,cnt);
            bestval = min(value, bestval);
        }
        return bestval;
    }   
}

for example at the end if the bestval = 10, i want the first move of the selection of this bestval. now i store the move in the 'move' variable but it doesn't work correctly.


Solution

  • In a practical minimax algorithm implementation, a hash table is used to enter evaluated scores and moves including hashkey, a value unique for each position of the players and pieces. This is also useful during implementation of "force move".

    During move evaluation, hashkey, score and move position are recorded in a struct and the hash table as well. So, after successful search, the entire struct is returned to enable update of the graphics and game status.

    A typical hash entry looks like so:

    struct HashEntry {
        int move;
        int score;
        int depth;
        uint64_t posKey;
        int flags;
    };