Beispiel #1
0
        private async void RunSim(object sender, RoutedEventArgs e)
        {
            RPSResult results = null;

            if (SimRunAmount < 1)
            {
                MessageBoxResult result = MessageBox.Show("Must input a valid simulation range", "Range Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                if (!simRunning)
                {
                    simRunning = true;

                    StartAndWaitOnResultConsole();

                    var engine = new RPSEngine();

                    await Task.Run(() =>
                    {
                        results = engine.RunSimulation(SimRunAmount);
                    });

                    StopTimer();
                    PrintResults(results);

                    simRunning = false;
                }
            }
        }
Beispiel #2
0
        private void PrintResults(RPSResult result)
        {
            OutputConsole  = "Sim finished in: " + result.TimeElapsedDuringSimulation + " milliseconds\n";
            OutputConsole += "Total simulations played: " + result.TotalSimulationsRun + "\n";

            OutputConsole += "\n";

            OutputConsole += "Total player one wins: " + result.TotalPlayerOneWins + "\n";
            OutputConsole += "Total player one losses: " + result.TotalPlayerOneLosses + "\n";

            OutputConsole += "\n";

            OutputConsole += "Total player two wins: " + result.TotalPlayerTwoWins + "\n";
            OutputConsole += "Total player two losses: " + result.TotalPlayerTwoLosses + "\n";

            OutputConsole += "\n";

            OutputConsole += "Total rock wins: " + result.RockWinCount + "\n";
            OutputConsole += "Total paper wins: " + result.PaperWinCount + "\n";
            OutputConsole += "Total scissors wins: " + result.ScissorWinCount + "\n";

            OutputConsole += "\n";

            OutputConsole += "Total tie games: " + result.TotalTieGames;
        }
Beispiel #3
0
        public RPSResult RunSimulation(long NumberOfSimsToRun)
        {
            InitializeWinStates();
            var result = new RPSResult();

            simTimer = new Stopwatch();
            var rand = new Random();

            simTimer.Start();

            for (int simNumber = 0; simNumber < NumberOfSimsToRun; simNumber++)
            {
                result.TotalSimulationsRun++;

                //randomize player hands
                var p1 = rand.Next(0, 3);
                var p2 = rand.Next(0, 3);

                var winner = DetermineWinner((RPSEnum)p1, (RPSEnum)p2, result);

                UpdateResults(result, winner);
            }

            simTimer.Stop();
            result.TimeElapsedDuringSimulation = simTimer.ElapsedMilliseconds;

            return(result);
        }
Beispiel #4
0
 private void UpdateResults(RPSResult result, int roundWinner)
 {
     if (roundWinner == playerOne)
     {
         result.TotalPlayerOneWins++;
         result.TotalPlayerTwoLosses++;
     }
     else if (roundWinner == playerTwo)
     {
         result.TotalPlayerTwoWins++;
         result.TotalPlayerOneLosses++;
     }
     else
     {
         result.TotalTieGames++;
     }
 }
Beispiel #5
0
        private int DetermineWinner(RPSEnum playerOneChoice, RPSEnum playerTwoChoice, RPSResult result)
        {
            bool output;

            if (WinStateDictionary.TryGetValue(Tuple.Create(playerOneChoice, playerTwoChoice), out output))
            {
                if (playerOneChoice == RPSEnum.Rock)
                {
                    result.RockWinCount++;
                }
                else if (playerOneChoice == RPSEnum.Paper)
                {
                    result.PaperWinCount++;
                }
                else
                {
                    result.ScissorWinCount++;
                }

                return(playerOne);
            }
            else if (WinStateDictionary.TryGetValue(Tuple.Create(playerTwoChoice, playerOneChoice), out output))
            {
                if (playerTwoChoice == RPSEnum.Rock)
                {
                    result.RockWinCount++;
                }
                else if (playerTwoChoice == RPSEnum.Paper)
                {
                    result.PaperWinCount++;
                }
                else
                {
                    result.ScissorWinCount++;
                }

                return(playerTwo);
            }

            return(0);
        }