src/GameAI/Algorithms/MonteCarlo/RandomSimulation.cs
/// <summary>
/// Return the move with the highest win-rate after performing the specified number of simulations on the game.
/// </summary>
/// <param name="game">The starting gamestate.</param>
/// <param name="simulations">The number of simulations to perform.</param>
Return the move with the highest win-rate after performing the specified number of simulations on the game.
Return the move with the highest win - rate after performing the specified number of simulations on the game .
public static TMove Search(TGame game, int simulations)
        {
            TPlayer aiPlayer = game.CurrentPlayer;
            List<TMove> legalMoves = game.GetLegalMoves();
            int count = legalMoves.Count;
            MoveStats[] moveStats = new MoveStats[count];
            int moveIndex;
            TGame copy;
            Random rng = new Random();
            for (int i = 0; i < simulations; i++)
            {
                moveIndex = rng.Next(0, count);
                copy = game.DeepCopy();
                copy.DoMove(legalMoves[moveIndex]);
                while (!copy.IsGameOver())
                    copy.DoMove(
                        copy.GetLegalMoves().RandomItem(rng));
                moveStats[moveIndex].executions++;
                if (copy.IsWinner(aiPlayer))
                    moveStats[moveIndex].victories++;
            }
            int bestMoveFound = 0;
            double bestScoreFound = 0f;
            for (int i = 0; i < count; i++)
            {
                double score = moveStats[i].Score();
                if (score > bestScoreFound)
                {
                    bestScoreFound = score;
                    bestMoveFound = i;
                }
            }
            return legalMoves[bestMoveFound];
        }
public static TMove Search ( TGame game , int simulations ) { TPlayer aiPlayer = game . CurrentPlayer ; List < TMove > legalMoves = game . GetLegalMoves ( ) ; int count = legalMoves . Count ; MoveStats [ ] moveStats = new MoveStats [ count ] ; int moveIndex ; TGame copy ; Random rng = new Random ( ) ; for ( int i = 0 ; i < simulations ; i ++ ) { moveIndex = rng . Next ( 0 , count ) ; copy = game . DeepCopy ( ) ; copy . DoMove ( legalMoves [ moveIndex ] ) ; while ( ! copy . IsGameOver ( ) ) copy . DoMove ( copy . GetLegalMoves ( ) . RandomItem ( rng ) ) ; moveStats [ moveIndex ] . executions ++ ; if ( copy . IsWinner ( aiPlayer ) ) moveStats [ moveIndex ] . victories ++ ; } int bestMoveFound = 0 ; double bestScoreFound = 0f ; for ( int i = 0 ; i < count ; i ++ ) { double score = moveStats [ i ] . Score ( ) ; if ( score > bestScoreFound ) { bestScoreFound = score ; bestMoveFound = i ; } } return legalMoves [ bestMoveFound ] ; }
Return the move with the highest win-rate after performing the specified number of simulations on the game.
Return the move with the highest win - rate after performing the specified number of simulations on the game .
game
TGame
The starting gamestate.
The starting gamestate .
simulations
int
The number of simulations to perform.
The number of simulations to perform .