Example #1
0
        public void SimulateGameButtonClick()
        {
            if (!CanSimulateStrategies())
            {
                return;
            }

            this.StrategyResultsAvailable.Value = false;

            var uiScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();

            Dominion.Strategy.Description.StrategyDescription player1Descr = this.player1Strategy.ConvertToDominionStrategy();
            Dominion.Strategy.Description.StrategyDescription player2Descr = this.player2Strategy.ConvertToDominionStrategy();

            Dominion.GameConfig gameConfig = this.GetGameConfig();

            System.Diagnostics.Debug.WriteLine("Player 1: ");
            System.Diagnostics.Debug.WriteLine(player1Descr.ToString());
            System.Diagnostics.Debug.WriteLine("Player 2: ");
            System.Diagnostics.Debug.WriteLine(player2Descr.ToString());

            System.Threading.Tasks.Task <StrategyUIResults> .Factory.StartNew(() =>
            {
                string player1nameAppend, player2nameAppend;
                GetStrategyNames(player1Descr, player2Descr, out player1nameAppend, out player2nameAppend);

                string player1Name = "Player 1 " + (!string.IsNullOrEmpty(player1nameAppend) ? "(" + player1nameAppend + ")" : "");
                string player2Name = "Player 2 " + (!string.IsNullOrEmpty(player2nameAppend) ? "(" + player2nameAppend + ")" : "");

                var playerActions = new Dominion.Strategy.PlayerAction[]
                {
                    player1Descr.ToPlayerAction(player1Name),
                    player2Descr.ToPlayerAction(player2Name)
                };

                bool rotateWhoStartsFirst = true;
                int numberOfGames         = 1000;

                var strategyComparison = new Dominion.Data.StrategyComparison(playerActions, gameConfig, rotateWhoStartsFirst, numberOfGames);

                Dominion.Data.StrategyComparisonResults strategyComparisonResults = strategyComparison.ComparePlayers(randomSeed: MainPage.random.Next(), shouldParallel: false);
                return(new StrategyUIResults()
                {
                    strategyComparisonResults = strategyComparisonResults,
                    Player1Name = player1Name,
                    Player2Name = player2Name,
                    Player1WinPercent = strategyComparisonResults.PlayerWinPercent(0),
                    Player2WinPercent = strategyComparisonResults.PlayerWinPercent(1),
                    TiePercent = strategyComparisonResults.TiePercent,
                });
            }).ContinueWith(async(continuation) =>
            {
                var results = (StrategyUIResults)continuation.Result;

                this.strategyReportDirty            = true;
                this.strategyComparisonResults      = results.strategyComparisonResults;
                this.Player1Name.Value              = results.Player1Name;
                this.Player2Name.Value              = results.Player2Name;
                this.Player1WinPercent.Value        = results.Player1WinPercent;
                this.Player2WinPercent.Value        = results.Player2WinPercent;
                this.TiePercent.Value               = results.TiePercent;
                this.StrategyResultsAvailable.Value = true;
            }, uiScheduler);
        }
        private void SimulateGameButtonClick(object sender, RoutedEventArgs e)
        {
            if (!CanSimulateStrategies())
            {
                return;
            }

            var uiScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();

            Dominion.Strategy.Description.StrategyDescription player1Descr = this.appDataContext.player1Strategy.ConvertToDominionStrategy();
            Dominion.Strategy.Description.StrategyDescription player2Descr = this.appDataContext.player2Strategy.ConvertToDominionStrategy();

            System.Threading.Tasks.Task <StrategyUIResults> .Factory.StartNew(() =>
            {
                string player1nameAppend, player2nameAppend;
                GetStrategyNames(player1Descr, player2Descr, out player1nameAppend, out player2nameAppend);

                string player1Name = "Player 1: " + player1nameAppend;
                string player2Name = "Player 2: " + player2nameAppend;

                var playerActions = new Dominion.Strategy.PlayerAction[]
                {
                    player1Descr.ToPlayerAction(player1Name),
                    player2Descr.ToPlayerAction(player2Name)
                };

                var builder = new Dominion.GameConfigBuilder();
                Dominion.Strategy.PlayerAction.SetKingdomCards(builder, playerActions[0], playerActions[1]);

                builder.useColonyAndPlatinum = false;
                builder.useShelters          = false;
                builder.CardSplit            = Dominion.StartingCardSplit.Split43;

                bool rotateWhoStartsFirst = true;
                int numberOfGames         = 1000;

                Dominion.GameConfig gameConfig = builder.ToGameConfig();
                var strategyComparison         = new Dominion.Data.StrategyComparison(playerActions, gameConfig, rotateWhoStartsFirst, numberOfGames);

                Dominion.Data.StrategyComparisonResults strategyComparisonResults = strategyComparison.ComparePlayers();

                var htmlGenerator = new HtmlRenderer.HtmlReportGenerator(strategyComparisonResults);

                var stringWriter = new System.IO.StringWriter();
                var textWriter   = new Dominion.IndentedTextWriter(stringWriter);

                htmlGenerator.CreateHtmlReport(textWriter);
                stringWriter.Flush();
                string resultHtml = stringWriter.GetStringBuilder().ToString();
                return(new StrategyUIResults()
                {
                    StrategyReport = resultHtml,
                    Player1Name = player1Name,
                    Player2Name = player2Name,
                    Player1WinPercent = strategyComparisonResults.PlayerWinPercent(0),
                    Player2WinPercent = strategyComparisonResults.PlayerWinPercent(1),
                    TiePercent = strategyComparisonResults.TiePercent,
                });
            }).ContinueWith(async(continuation) =>
            {
                var results = (StrategyUIResults )continuation.Result;

                this.appDataContext.StrategyReport.Value           = results.StrategyReport;
                this.appDataContext.Player1Name.Value              = results.Player1Name;
                this.appDataContext.Player2Name.Value              = results.Player2Name;
                this.appDataContext.Player1WinPercent.Value        = results.Player1WinPercent;
                this.appDataContext.Player2WinPercent.Value        = results.Player2WinPercent;
                this.appDataContext.TiePercent.Value               = results.TiePercent;
                this.appDataContext.StrategyResultsAvailable.Value = true;
            }, uiScheduler);
        }