BTW, Here's my attempt at the Monty Hall solution. I did via Object-Orientated programming, but its probably a bit more cumbersome than Damien's attempt. Still at least its good simple intro it using it as most of the important base principals are used. Its not pure OO though but thereabouts
Spoiler:
Code:
namespace PCTestMontyHall
{
//class to represent the door
class Door
{
public int doorNumber;
public Boolean open = false;
public String prize;
//constructor, this happens when a door is created
public Door(int doorNumber, String prize)
{
this.doorNumber = doorNumber;
this.prize = prize;
}
//this is called when we Console.WriteLine a door. We have to override the default ToString() method
public override String ToString()
{
//conditional shorthand. If open is true then it sets "Open" to status. If not it sets "Closed" to status
String status = open ? "Open" : "Closed";
return "Door " + doorNumber + ": " + prize + " (" + status + ")";
}
}
class Program
{
//create a random number generator
public static Random random = new Random();
static void Main(string[] args)
{
//set initial counters and parameters
int ferraris = 0;
int goats = 0;
int gamesToPlay = 1000;
bool contestantSwapping = false;
//play the game
for (int i = 0;i<gamesToPlay;i++)
{
//true parameter means contestant will swap, false means he doesn't.
string result = PlayGame(contestantSwapping);
if (result == "Ferrari")
{
ferraris++;
}
else
{
goats++;
}
}
//print out statistics
Console.WriteLine("Contestant swapping? " + contestantSwapping);
float ferrariPercentage = ((float)ferraris / gamesToPlay) * 100;
float goatPercentage = ((float)goats / gamesToPlay) * 100;
Console.WriteLine(String.Format("\n\nFerraris: {0} {1}%\nGoats: {2} {3}%", ferraris, ferrariPercentage, goats, goatPercentage));
//Hit enter to quit
Console.ReadLine();
}
//the actual game
static string PlayGame(Boolean swapChoice)
{
//create an array to hold the doors
Door[] doors = new Door[3];
//set contestant's choice of door randomly
int contestantsChoice = random.Next(0, 3);
//add door objects to the array of doors
for (int i = 0;i < doors.Length; i++)
{
doors[i] = new Door(i+1,"Goat");
}
//choose which door a Ferrari should be behind
int ferrari = random.Next(0, 3);
doors[ferrari].prize = "Ferrari";
//host has to remove a choice
int removeChoice;
//keep chosing a door at random until the door contains a goat and isn't the same as the contestants choice
do
{
removeChoice = random.Next(0, 3);
} while ((doors[removeChoice].prize == "Ferrari")|| (removeChoice == contestantsChoice));
//"open" the door
doors[removeChoice].open = true;
//if contestant is swapping, then choose the other door with a goat
int oldChoice = contestantsChoice;
if (swapChoice)
{
do
{
contestantsChoice = random.Next(0, 3);
} while ((doors[contestantsChoice].open == true) || (contestantsChoice == oldChoice));
}
//return the prize as a result
return doors[contestantsChoice].prize;
}
//print out the status of the doors. I added this as a debug whilst writing but illustrates overriding the default ToString method
static void ShowDoors(Door[] doors)
{
Console.WriteLine("Doors:");
for (int i = 0; i < doors.Length; i++)
{
Console.WriteLine(doors[i]);
}
}
}
}
Result:
Contestant swapping? False
Ferraris: 330 33%
Goats: 670 67%
Contestant swapping? True
Ferraris: 671 67.1%
Goats: 329 32.9%
I'll have a go at the mortgage calculator tonight probably.