Esempio n. 1
0
        public int[] GetDoorsToShow(Doors doors, int contestantChoice)
        {
            var list = new List <int>();

            var choosableDoorId = new List <int>();

            foreach (Door door in doors)
            {
                if (door.Prize == PrizeType.Good)
                {
                    continue;
                }

                if (door.Id == contestantChoice)
                {
                    continue;
                }

                // Choose the doors without the prize and
                // not chosen by contestant.
                list.Add(door.Id);
                door.State = DoorState.Open;

                if (list.Count > doors.Count - 3)
                {
                    break;
                }
            }

            return(list.ToArray());
        }
Esempio n. 2
0
        private static void RunContests(Random rnd, int nbrOfRuns, int nbrOfDoors, Type contestantType)
        {
            var host = new Host(rnd);

            var wins   = 0;
            var losses = 0;

            const string fmtSummary = "{0} Wins {1:N0}, Losses {2:N0}";
            // const string fmtResult = "Round {0:D3} InitialChoice: {1:D2} OpenDoor: {2:D2} FinalChoice: {3:D2}  PrizeDoor: {4:D2} ";

            string contestantDescription = null;

            for (int i = 0; i < nbrOfRuns; i++)
            {
                var doors = new Doors(rnd);

                IContestant contestant = null;

                if (contestantType == typeof(SteadfastContestant))
                {
                    contestant            = new SteadfastContestant(rnd);
                    contestantDescription = "Steadfast Contestant";
                }
                else if (contestantType == typeof(SwitchyContestant))
                {
                    contestant            = new SwitchyContestant(rnd);
                    contestantDescription = "Switchy Contestant";
                }

                var contest = new Contest(rnd)
                {
                    Doors = doors, Host = host
                };
                contest.Setup(contestant);

                var prize = contest.Play();

                if (prize == PrizeType.Good)
                {
                    wins++;
                }
                else
                {
                    losses++;
                }

                //Console.WriteLine(fmtResult, i + 1, contestant.InitialDoorChoice, doors.ShownDoor.Id, contestant.DoorChoice, doors.WinnerDoor.Id);
            }

            Console.WriteLine();
            Console.WriteLine(fmtSummary, contestantDescription, wins, losses);
            Console.WriteLine();
            Console.WriteLine();
        }
Esempio n. 3
0
        public PrizeType Play()
        {
            // Contestant makes initial choice.
            Contestant.Decide(Doors.Count);

            // Host shows a door (or doors)
            var shownDoors = Host.GetDoorsToShow(Doors, Contestant.DoorChoice);

            // Contest decides to switch or stay
            Contestant.Decide(Doors);

            // See what contestant won
            var door = Doors.GetDoorById(Contestant.DoorChoice);

            return(door.Prize);
        }
Esempio n. 4
0
        public void Setup(IContestant contestant)
        {
            const int nbrOfDoors = 3;

            Contestant = contestant;

            if (Doors == null)
            {
                Doors = new Doors(_rnd);
            }

            if (Host == null)
            {
                Host = new Host(_rnd);
            }

            Doors.Setup(nbrOfDoors);
        }
Esempio n. 5
0
        private Doors GetNewRound(int doorCount, bool autoChooseDoor)
        {
            var doors = new Doors();

            for (int i = 0; i < doorCount; i++)
            {
                doors.Door.Add(EPrize.Goat);
            }

            //let's set the prize door
            doors.Door[GetRandomDoor()] = EPrize.Car;

            if (autoChooseDoor)
            {
                doors.ChosenDoor = GetRandomDoor();
            }
            return(doors);
        }
Esempio n. 6
0
        public void Decide(Doors doors)
        {
            InitialDoorChoice = DoorChoice;

            foreach (Door d in doors)
            {
                // We are switchy! We don't want to stay on our original choice.
                if (d.Id == InitialDoorChoice)
                {
                    continue;
                }

                // We don't want the loser prize in the door the host has shown us.
                if (d.State == DoorState.Open)
                {
                    continue;
                }

                DoorChoice = d.Id;
            }
        }
Esempio n. 7
0
 private void InitialiseDoors()
 {
     Doors.Add(new GoatDoor());
     Doors.Add(new GoatDoor());
     Doors.Insert((int)RandomGenerator.NextPrizeDoorIndex(), new PrizeDoor());
 }
Esempio n. 8
0
        public void Decide(Doors doors)
        {
            InitialDoorChoice = DoorChoice;

            // Nothing to do here folks. Player is standing fast.
        }