调用随机值文本文件方法



我正在尝试制作一个刽子手游戏,从单词的文本文件中随机选择一个单词,然后要求用户猜测单词的每个字母。单词中的每个字母都以星号显示,当用户做出正确的猜测时,将显示实际字母。猜出单词后,它将显示错过的次数,并询问用户是否要猜测另一个单词。我创建了一个方法从文本文件中获取所有值并将其存储在数组中,然后返回要使用的值,但是当我调用该方法时,它希望具有 StreamReader reader 的参数,如果我将其放入它,则说它在给定上下文中无效。我不确定如何调用此方法以用于随机单词

static void Main(string[] args)
{
char[] guessed = new char[26];
char[] testword = "******".ToCharArray();
char[] word = RandomLine(StreamReader reader);
char[] copy = word;
char guess;
char playAgain;
int amountMissed = 0, index = 0;
Console.WriteLine(testword);
do
{
while (testword != word)
{
Console.WriteLine("I have picked a random word on animals");
Console.WriteLine("Your task is to guess the correct word");
for (int i = 0; i < 10; i++)
{
Console.Write("Please enter a letter to guess: ");
guess = char.Parse(Console.ReadLine());
bool right = false;
for (int j = 0; j < copy.Length; j++)
{
if (copy[j] == guess)
{
Console.WriteLine("Your guess is correct.");
testword[j] = guess;
guessed[index] = guess;
index++;
right = true;
}
}
if (right != true)
{
Console.WriteLine("Your guess is incorrect.");
}
else
{
right = false;
amountMissed++;
}
Console.WriteLine(testword);
}
Console.WriteLine($"The word is {word}. You missed {amountMissed} times.");
}
Console.WriteLine("Do you want to gues another word? Enter y or n: ");
playAgain = char.Parse(Console.ReadLine());
} while (playAgain == 'y' || playAgain == 'Y');
Console.WriteLine("Good-Bye and thanks for playing my  Hangman game.");

}
public string RandomLine(StreamReader reader)
{
// store text file in a array and return a random value
string[] lines = File.ReadAllLines("C:\Intel\Advanced1.csv"); 
Random rand = new Random();
return lines[rand.Next(lines.Length)];
}
}

你根本不需要 Stream reader 参数来 RandomLines。
它永远不会被使用,而是使用 File.ReadAllLines 来读取文件。 如果你只是摆脱了StreamReader,你遇到的任何问题都应该消失,你的代码应该仍然按原样运行。
例如:

public string RandomLine()
{
// store text file in a array and return a random value
string[] lines = File.ReadAllLines("C:\Intel\Advanced1.csv"); 
Random rand = new Random();
return lines[rand.Next(lines.Length)];
}

您对该方法的调用应如下所示:

char[] word = RandomLine();