C# 将数组中的随机字符串值显示为星号
我正在为一个刽子手游戏制作一个程序,它从一个单词文本文件中随机挑选一个单词,然后让用户猜单词的每个字母。单词中的每个字母都会显示一个星号,当用户做出正确猜测时,实际的字母就会显示出来。猜出单词后,它将显示错过的次数,并询问用户是否想猜另一个单词。 我有硬编码的星号,但我想星号是相同长度的随机字从文本文件中选择。我不知道我会怎么做 任何帮助都将不胜感激C# 将数组中的随机字符串值显示为星号,c#,console,C#,Console,我正在为一个刽子手游戏制作一个程序,它从一个单词文本文件中随机挑选一个单词,然后让用户猜单词的每个字母。单词中的每个字母都会显示一个星号,当用户做出正确猜测时,实际的字母就会显示出来。猜出单词后,它将显示错过的次数,并询问用户是否想猜另一个单词。 我有硬编码的星号,但我想星号是相同长度的随机字从文本文件中选择。我不知道我会怎么做 任何帮助都将不胜感激 static void Main(string[] args) { char[] guessed = new char[
static void Main(string[] args)
{
char[] guessed = new char[26];
char[] testword = "******".ToCharArray();
char[] word = RandomLine().ToCharArray();
char[] copy = word;
char guess;
char playAgain;
int amountMissed = 0, index = 0;
Console.WriteLine(testword);
do
{
Console.WriteLine("I have picked a random word on animals");
Console.WriteLine("Your task is to guess the correct word");
while (testword != word)
{
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.");
amountMissed++;
}
else
{
right = false;
}
Console.WriteLine(testword);
}
Console.WriteLine($"The word is {copy}. You missed {amountMissed} times.");
Console.WriteLine("Do you want to guess 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 static string RandomLine()
{
try
{
// store text file in an array and return a random value
string[] lines = File.ReadAllLines("C:\\Intel\\Advanced1.csv");
Random rand = new Random();
return lines[rand.Next(lines.Length)];
}
catch (Exception e)
{
Console.WriteLine("The file could not be read");
Console.WriteLine(e.Message);
}
}
static void Main(字符串[]args)
{
char[]猜测=新字符[26];
char[]testword=“*******”.ToCharArray();
char[]word=RandomLine().ToCharArray();
char[]copy=word;
猜字符;
再次播放;
int amountMissed=0,index=0;
控制台写入线(testword);
做
{
WriteLine(“我随便选了一个关于动物的词”);
WriteLine(“你的任务是猜测正确的单词”);
while(testword!=word)
{
控制台。写(“请输入一个字母来猜测:”);
guess=char.Parse(Console.ReadLine());
布尔右=假;
对于(int j=0;j
要显示与word长度相同的编码单词(带星号),您可以使用
char[] testword = new string('*',word.Length).ToCharArray();
在代码中做一些其他更正
char[] guessed = new char[26];
char guess;
char playAgain;
int amountMissed = 0, index = 0;
do
{
char[] word = RandomLine().ToCharArray(); // Change :This needs to be inside the loop so that new random word could be selected user selects to continue the game
char[] testword = new string('*',word.Length).ToCharArray(); // Change : Reordered initilization of word and testword so that we could generate testword with same length as original
char[] copy = word;
Console.WriteLine(testword);
Console.WriteLine("I have picked a random word on animals");
Console.WriteLine("Your task is to guess the correct word");
while (!testword.SequenceEqual(word)) // Change : Comparison of arrays
{
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.");
amountMissed++;
}
else
{
right = false;
}
Console.WriteLine(testword);
}
Console.WriteLine($"The word is {copy}. You missed {amountMissed} times.");
Console.WriteLine("Do you want to guess 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.");
char[]猜测=新字符[26];
猜字符;
再次播放;
int amountMissed=0,index=0;
做
{
char[]word=RandomLine().ToCharArray();//更改:这需要在循环中,以便可以选择新的随机词用户选择继续游戏
char[]testword=new string('*',word.Length).ToCharArray();//更改:重新排序word和testword的初始化,以便生成与原始长度相同的testword
char[]copy=word;
控制台写入线(testword);
WriteLine(“我随便选了一个关于动物的词”);
WriteLine(“你的任务是猜测正确的单词”);
while(!testword.SequenceEqual(word))//更改:数组的比较
{
控制台。写(“请输入一个字母来猜测:”);
guess=char.Parse(Console.ReadLine());
布尔右=假;
对于(int j=0;j
这样:
class Program
{
static void Main(string[] args)
{
bool found = false;
string originalWord = "test";
char[] word = originalWord.ToCharArray();
char[] asteriskedWord = new string('*', word.Length).ToCharArray();
while (found == false)
{
Console.WriteLine(asteriskedWord);
string input = Console.ReadLine();
if (input.Length != 1)
{
Console.WriteLine("Your input can be only one char.");
}
else
{
char c = char.Parse(input);
for (int i = 0; i < word.Length; i++)
{
if (word[i] == c)
{
asteriskedWord[i] = c;
word[i] = '*';
int missingCharsAmount = asteriskedWord.Count(x => x == '*');
Console.WriteLine("Correct, missing " + missingCharsAmount + " chars.");
if (missingCharsAmount == 0)
{
found = true;
}
break;
}
}
}
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
bool-found=false;
字符串originalWord=“test”;
char[]word=originalWord.ToCharArray();
char[]asteriskedWord=新字符串('*',word.Length).tocharray();
while(find==false)
{
控制台写入线(星号为kedword);
字符串输入=Console.ReadLine();
如果(input.Length!=1)
{
WriteLine(“您的输入只能是一个字符。”);
}
其他的
{
char c=char.Parse(输入);
for(int i=0;ix='*');