我试图为我的期末项目创建一个基本的 Monty Python 游戏。我不知道如何将奖品放入随机门或打开随机门。我搜索了其他资源,但找不到任何信息来帮助我完成这项任务。谢谢你。
这是我尝试运行的代码,但出现了越界异常:
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;
}
}
}
}
有什么建议吗?谢谢你。
我在您的代码中看到的第一个问题,这导致它抛出异常并且无法在all:
工作Convert.ToInt32(new Random());
new Random()
创建一个类型为 Random
的新对象。这是一个随机数生成器类,它不能转换为整数。
不要像这里那样手动在 1 和 3 处添加“Nothing”,并在 2 处添加“Car”:
Door.Add(1, "Nothing");
Door.Add(2, "Car");
Door.Add(3, "Nothing");
您需要使用随机数生成器(您使用
Random rng = new Random();
创建的)来随机生成奖品将位于哪扇门后面。假设您使用三扇门,这是典型的“蒙蒂霍尔问题”门数,这就是我要做的。这可能不是最有效的方法,但我正在努力将其保持在初学者水平。
// 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");
}
}
现在你的门字典在随机位置有“汽车”,在另外两个位置有“无”。
我假设您尚未使用 LINQ,因此我将为您提供一个不使用 LINQ 的解决方案。再说一遍,有更干净、更优化的方法可以做到这一点,但我会让你自己解决这部分问题。我的回答更多的是从概念上为您指出正确的方向。
// 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"
此时,您可以使用
chosenDoorNumber
和chosenDoor
做任何您需要的事情。显示给用户等