I was trying to create a basic Monty Python Game for my final project. I do not know how to put the prize in a random door or reveal a random door. I searched up other resources and cannot find any information to help me with this assignment. Thank you.
Here is the code that I tried to run, and got an Out Of Bounds Exception on:
using System;
class Guess
{
static void Main (string[] args)
{
Dictionary<int, string> Door = new Dictionary<int, string>();
Door.Add(1, "Nothing");
Door.Add(2, "Car");
Door.Add(3, "Nothing");
Random rng = new Random();
int x = rng.Next(0, Door.Count);
int doors = Door.Keys.ElementAt(x);
string prize = Door.Values.ElementAt(x);
int n = Door.Count;
int PrizeDoor = Convert.ToInt32(new Random());
Console.WriteLine("Which door do you want to select?");
int SelectedDoor = Convert.ToInt32(Console.ReadLine());
int RevealedDoor = Convert.ToInt32(new Random());
Console.WriteLine("Do you want to change your selection?");
string selection = Console.ReadLine();
if (selection == "Yes")
{
// ask user which new door to choose
Console.WriteLine("Which door would you like to choose now?");
int NewSelectedDoor = Convert.ToInt32(Console.ReadLine());
if (NewSelectedDoor == SelectedDoor)
{
Console.WriteLine("You cannot select the same door twice. Please try again.");
}
else if (NewSelectedDoor == PrizeDoor)
{
Console.WriteLine("Congratulations! You won the game!");
return;
}
else
{
Console.WriteLine("Sorry, you lost the game!");
return;
}
}
else
{
// stay with same door and reveal what's in the door
if (SelectedDoor == PrizeDoor)
{
Console.WriteLine("Congratulations! You won the game!");
return;
}
else
{
Console.WriteLine("Sorry, you lost the game!");
return;
}
}
}
}
Any suggestions? Thank you.
First issue I see in your code, that's causing it to throw an exception and not work at all:
Convert.ToInt32(new Random());
new Random()
creates a new object of the type Random
. This is a random number generator class, it can't be converted to an integer.
Instead of manually adding "Nothing" at 1 and 3, and the "Car" at 2, like you do here:
Door.Add(1, "Nothing");
Door.Add(2, "Car");
Door.Add(3, "Nothing");
You'll want to use your random number generator (which you created with Random rng = new Random();
) to randomly generate which door the prize will go behind. Assuming you are using three doors, which is the classic "Monty Hall problem" number of doors, here's what I would do. This may not be the most efficient way of doing it, but I'm trying to keep it at a beginner level.
// instantiate your doors dictionary
// and random number generator
Dictionary<int, string> doors = new Dictionary<int, string>();
Random rng = new Random();
int totalNumberOfDoors = 3;
// use your random number generator to generate a number
// between 1 (inclusive) and 4 (exclusive)
// whatever number it generates, that's what door the prize is behind
int prizeBehindDoor = rng.Next(1, totalNumberOfDoors + 1);
// fill your doors dictionary now
// I'm using a plain for loop b/c it's a basic concept
// it starts at 1 because your doors are numbered "1", "2", "3" and not "0", "1", and "2"
for (int i = 1; i <= totalNumberOfDoors; i++)
{
// if `i` is the door the prize is behind, add "Car"
if (i == prizeBehindDoor)
{
doors.Add(i, "Car");
}
// otherwise, add "Nothing"
else
{
doors.Add(i, "Nothing");
}
}
Now your doors dictionary has "Car" at a random spot, and "Nothing" in the other two spots.
I am assuming you're not using LINQ yet, so I'll give you a solution that doesn't use LINQ. And again, there's cleaner, more optimized ways to do this, but I'll let you figure that part out on your own. My answer is more to point you in the right direction concept-wise.
// the prize could be behind any of the 3 doors,
// so you can't JUST randomly generate a number between 1 and 3 again
// because it might pick the one with the prize
// so let's assign the door numbers that AREN'T the prize to a list
List<int> nonPrizeDoors = new List<int>();
// loop thru the keys of your doors dictionary, and only add items that are not the prize
// the "keys" of the dictionary are your door numbers (1, 2, and 3)
// there's definitely other ways of doing this
foreach (int key in doors.Keys)
{
// only add this key to "nonPrizeDoors" if it's NOT the prize
if (key != prizeBehindDoor)
{
nonPrizeDoors.Add(key);
}
}
// now your "nonPrizeDoors" list contains all the door numbers that are not prize doors
// now you want to use your random number generator to randomly pick one of those
// you're picking an array/list INDEX here
// so you want to generate a number between 0 (inclusive) and the number of items in the list (exclusive)
int randomIndex = rng.Next(0, nonPrizeDoors.Count);
// remember that was just the INDEX,
// saying which of the (two) items in the Keys list you're going to choose
// now you want to get the randomly-chosen Key (door number)
int chosenDoorNumber = nonPrizeDoors[randomIndex];
// now you can get that item from the doors dictionary, and do whatever you need with it
string chosenDoor = doors[chosenDoorNumber]; // will be "Nothing"
At this point, you can do whatever you need with chosenDoorNumber
and chosenDoor
. Display it to the user, etc.