Esempio n. 1
0
        /// <summary>
        /// Create a new instance of a PickADoorSimulator
        /// </summary>
        /// <param name="doorSelectOption"></param>
        public PickADoorSimulator(DoorSelectOptionEnum doorSelectOption, bool swap, RandomDoorCallBack dealer)
        {
            // Store the Dealer
            this.Dealer = dealer;

            // Create the doors collection
            this.Doors = CreateDoors();

            // If the Dealer object exists
            if (this.HasDealer)
            {
                // Store the value for DoorSelectOption
                this.DoorSelectOption = doorSelectOption;

                // Set the value for Swap
                this.Swap = swap;

                // Set the value for SecretDoorNumberThatContainsCar
                HideCarInSecretLocation();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This method Start Simulation
        /// </summary>
        private void StartSimulation()
        {
            // Create a
            RandomDoorCallBack dealer = new RandomDoorCallBack(CanPullNextItem, PullNextItem);

            // Create a report
            this.Report = new RandomReport(PickADoorSimulator.MinValue, PickADoorSimulator.MaxValue);

            // You can't start a second simulation without restarting.
            this.StartSimulationButton.Enabled = false;

            // Show the initializing label
            this.InitializingLabel.Visible = true;
            this.Graph.Visible             = true;

            // Show the Setup
            RefreshUI();

            // locals
            DoorSelectOptionEnum doorSelectOption     = GetDoorSelectOption();
            PickADoorSimulator   simulatorWithoutSwap = null;
            PickADoorSimulator   simulatorWithSwap    = null;

            // Get the number of simulations to run
            int simulations = NumericHelper.ParseInteger(this.SimulationsControl.Text.Replace(",", ""), 0, -1);

            // If the value for simulations is greater than zero
            if (simulations > 0)
            {
                // create the total possibilities to choose from
                int possibilities = PickADoorSimulator.MaxValue * 10;

                // Create a Shuffler
                this.Shuffler = new RandomShuffler(PickADoorSimulator.MinValue, PickADoorSimulator.MaxValue, possibilities, PickADoorSimulator.TimesToShuffle, 1);

                // Set the report
                this.Shuffler.Report = this.Report;

                // Create the two lists
                this.SimulationsSwapping    = new List <PickADoorSimulator>();
                this.SimulationsNotSwapping = new List <PickADoorSimulator>();

                // Set the maximum
                this.Graph.Maximum = simulations;

                // iterate the number of simuations to create them
                for (int x = 1; x <= simulations; x++)
                {
                    // Create a simulation for Without Swap
                    simulatorWithoutSwap = new PickADoorSimulator(doorSelectOption, false, dealer);

                    // Create a simulation for Swap
                    simulatorWithSwap = new PickADoorSimulator(doorSelectOption, true, dealer);

                    // Add each simulation
                    this.SimulationsNotSwapping.Add(simulatorWithoutSwap);
                    this.SimulationsSwapping.Add(simulatorWithSwap);

                    // Set the graph value
                    this.Graph.Value = x;

                    // Refresh everything
                    this.RefreshUI();
                }

                // Hide the setup
                this.InitializingLabel.Visible = false;
                this.Graph.Visible             = false;

                // refresh the UI
                RefreshUI();

                // for debugging only
                // StringBuilder sb = new StringBuilder();

                // Now run the simulations
                for (int x = 0; x < simulations; x++)
                {
                    // Run the simulations
                    this.SimulationsNotSwapping[x].RunSimulation();
                    this.SimulationsSwapping[x].RunSimulation();

                    // if it was a correct guess
                    if (this.SimulationsNotSwapping[x].CorrectGuess)
                    {
                        // Increment the value for this
                        this.DoNotSwapCorrect++;
                    }
                    else
                    {
                        // Increment the value for this
                        this.DoNotSwapWrong++;
                    }

                    // if it was a correct guess
                    if (this.SimulationsSwapping[x].CorrectGuess)
                    {
                        // Increment the value for this
                        this.SwapCorrect++;
                    }
                    else
                    {
                        // Increment the value for this
                        this.SwapWrong++;
                    }

                    // Display the results
                    this.DoNotSwapCorrectValueLabel.Text = String.Format("{0:n0}", DoNotSwapCorrect);
                    this.DoNotSwapWrongValueLabel.Text   = String.Format("{0:n0}", DoNotSwapWrong);
                    this.SwapCorrectValueLabel.Text      = String.Format("{0:n0}", SwapCorrect);
                    this.SwapWrongValueLabel.Text        = String.Format("{0:n0}", SwapWrong);

                    // refresh the user interface
                    RefreshUI();
                }

                // Get the report
                if (this.Report.HasOccurrences)
                {
                    // local
                    int tempCount = 0;

                    // iterate the occurrences
                    foreach (NumberOccurrence occurrence in this.Report.Occurrences)
                    {
                        // local
                        int thisNumber = occurrence.Number;
                        tempCount = occurrence.Count;

                        // write out the number
                        Console.WriteLine(thisNumber + ": " + tempCount + " of " + this.Report.TotalItemsPulled);
                    }
                }
            }
            else
            {
                // Show the user a message
                MessageBoxHelper.ShowMessage("You must enter the number of simulations", "Invalid Input");
            }

            // Add Finished so we know it is done.
            this.Text = this.Text + " - Finished";
        }