我刚开始学习c#,为了练习,我正在尝试创建一个绞刑游戏(猜单词的字母)。
我创建了一个类WordToGuess,并为这个类创建了多个实例,每个实例对应一个单词。
现在我想随机选择其中一个让玩家猜测。我不知道该怎么做。
我发现代码随机选择一个索引从数组,这似乎是一个很好的方式来做它。但现在我不知道我到底能做什么。我可以在数组中列出实例吗?如果不是,我又怎么能优雅地做到呢?我可以想到一个变通方法,但这不是练习的重点。
我的实例的例子:
WordToGuess duck = new WordToGuess();
duck.numberOfLetters = 4;
duck.theWord = "Duck";
duck.theLetters = new string[] { "d", "u", "c", "k" };
duck.difficulty = "easy";
duck.wordID = "e3";
我的随机生成尝试(我认为我可以生成字符串ID,然后以这种方式处理实例,我想我没有考虑过)
string[] easyWords = new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
Random rndE = new Random();
int indexE = rndE.Next(easyWords.Length);
数组示例:
class Program
{
static void Main(string[] args)
{
WordToGuess[] wtg = new WordToGuess[10];
wtg[0] = new WordToGuess("Duck", "easy", "e3");
wtg[1] = new WordToGuess("Dog", "easy", "e4");
wtg[2] = new WordToGuess("House", "easy", "e5");
wtg[3] = new WordToGuess("Pneumonoultramicroscopicsilicovolcanoconiosis", "difficult", "e6");
Console.WriteLine();
foreach (var item in wtg)
{
if (item is not null)
{
Console.WriteLine($"{item.wordID}, {item.theWord}, {item.difficulty}, {item.numberOfLetters}");
}
}
Console.WriteLine();
Random r = new Random();
WordToGuess randomWord = wtg[r.Next(0, 3)];
Console.WriteLine($"A random word: {randomWord.wordID}, {randomWord.theWord}, {randomWord.difficulty}, {randomWord.numberOfLetters}");
// this needs: using System.Linq;
randomWord = wtg.Where(x => x is not null).OrderBy(x => r.Next()).First();
Console.WriteLine($"Another random word: {randomWord.wordID}, {randomWord.theWord}, {randomWord.difficulty}, {randomWord.numberOfLetters}");
Console.ReadLine();
}
}
public class WordToGuess
{
public int numberOfLetters { get; set; }
public string theWord { get; set; }
public char[] theLetters { get; set; }
public string difficulty { get; set; }
public string wordID { get; set; }
public WordToGuess()
{}
public WordToGuess(string theWord, string difficulty, string wordID)
{
this.numberOfLetters=theWord.Length;
this.theWord=theWord;
this.theLetters=theWord.ToUpper().ToCharArray();
this.difficulty=difficulty;
this.wordID=wordID;
}
}
输出如下:
e3, Duck, easy, 4
e4, Dog, easy, 3
e5, House, easy, 5
e6, Pneumonoultramicroscopicsilicovolcanoconiosis, difficult, 45
A random word: e4, Dog, easy, 3
Another random word: e5, House, easy, 5
单词Pneumo....此处发现Iosis: https://irisreading.com/10-longest-words-in-the-english-language/
有很多方法可以做你想做的事。也许最好的方法之一是将WordToGuess
实例存储在Dictionary<string, WordToGuess>
中,其中键是该标识符。
但是我也会重构你的类,一些属性不需要像字母的string[]
,因为字符串已经实现了IEnumerable<char>
。我还将使难度enum
而不是string
,并为WordToGuess
提供一个构造函数,将最重要的属性作为输入:
public class WordToGuess
{
public enum GuessDifficulty
{
Easy, Medium, Hard
}
public WordToGuess(string word, string id, GuessDifficulty difficulty)
{
TheWord = word;
ID = id;
Difficulty = difficulty;
}
public string ID {get;set;}
public string TheWord {get;set;}
public GuessDifficulty Difficulty {get;set;}
}
现在你可以这样初始化和填充字典:
Dictionary<string, WordToGuess> wordDictionary = new Dictionary<string, WordToGuess>();
WordToGuess duck = new WordToGuess("Duck", "e3", WordToGuess.GuessDifficulty.Easy);
wordDictionary.Add(duck.ID, duck);
// initialize and add more ...
您的随机单词逻辑然后只是尝试在字典中查找具有Id的单词:
string[] easyWords = new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7" };
Random rndE = new Random();
int idIndex = rndE.Next(easyWords.Length);
WordToGuess randomWord = wordDictionary.TryGetValue(easyWords[idIndex], out WordToGuess w) ? w : null;
请注意,如果没有带有随机标识符的单词,则为null
。
创建一个列表,并将所有实例添加到列表中。然后在列表中使用随机索引代码。在c#中,我们很少使用[],而是使用列表。(是的,你可以在数组中有对象实例)