C#将提示链接到字典中的随机单词

C#将提示链接到字典中的随机单词,c#,C#,我使用以下命令显示单词的部分内容: public string GetPartialWord(string word) { if (string.IsNullOrEmpty(word)) { return string.Empty; } char[] partialWord = word.ToCharArray(); int numberOfCharsToHide = word.Le

我使用以下命令显示单词的部分内容:

 public string GetPartialWord(string word)
    {
        if (string.IsNullOrEmpty(word))
        {
            return string.Empty;
        }
        char[] partialWord = word.ToCharArray();
        int numberOfCharsToHide = word.Length / 2;
        Random randomNumberGenerator = new Random();
        HashSet<int> maskedIndices = new HashSet<int>();
        for (int i = 0; i < numberOfCharsToHide; i++)
        {
            int rIndex = randomNumberGenerator.Next(0, word.Length);
            while (!maskedIndices.Add(rIndex))
            {
                rIndex = randomNumberGenerator.Next(0, word.Length);
            }
            partialWord[rIndex] = '_';
        }
        return new string(partialWord);
    }
公共字符串GetPartialWord(字符串字)
{
if(string.IsNullOrEmpty(word))
{
返回字符串。空;
}
char[]partialWord=word.ToCharArray();
int numberOfCharsToHide=word.Length/2;
Random randomNumberGenerator=新的Random();
HashSet maskedIndices=新HashSet();
for(int i=0;i
因此:文字游戏看起来像:_a_e

我正在考虑添加一个提示按钮来显示另一个字符。关于如何进行有什么想法吗


你可以使用这样的东西:

public string GetWordAfterHint(string wordToProcess, string originalWord)
{
    List<int> emptyIndexes = new List<int>();
    for (int a = 0; a < wordToProcess.Length; a++)
    {
        if (wordToProcess[a] == '_')
        {
            emptyIndexes.Add(a);
        }
    }

    // in case if word doesn't have empty positions
    if (emptyIndexes.Count == 0)
    {
        return wordToProcess;
    }

    Random random = new Random();
    var indexForLetter = random.Next(emptyIndexes.Count);

    // create stringBuilder from string, because string is immutable and you can't change separate symbol
    StringBuilder sb = new StringBuilder(wordToProcess); 

    // insert symbol from originalWord in empty previously generated position
    sb[emptyIndexes[indexForLetter]] = originalWord[emptyIndexes[indexForLetter]]; // 

    //convert stringBuilder to string and return
    return sb.ToString();
}
private GameWord currentGameWord;
this.textBox1.Text = this.currentGameWord.GuessWord;
public string GetWordAfterHint(string-wordToProcess,string-originalWord)
{
List emptyIndexes=新列表();
for(int a=0;a

方法在提示后返回单词-如果作为
wordToProcess
传递
“\u a\u e”
和作为
原始单词
“game”
,则方法返回
ga\u e
\u ame

存储数组或列表中显示的字符的索引。按下提示按钮时,计算一个随机索引。将该索引与已显示的字母索引进行比较,必要时重新计算新的随机索引。

面向对象的方法:

GePartialWord
方法中,返回此类的实例,而不是简单的字符串:

public class GameWord
{
    public string OriginalWord { get; set; }
    public string GuessWord { get; set; }

    public string Hint()
    {
        int index = this.GuessWord.IndexOf('_');
        if (index != -1)
        {
            var builder = new StringBuilder(this.GuessWord);
            builder[index] = this.OriginalWord[index];

            this.GuessWord = builder.ToString();

            return this.GuessWord; // if needed
        }

        // No more hints, the world has no underscores
        return this.GuessWord;
    }
}
因此,在您的方法中,您将改为:

public GameWord GetPartialWord(string word)
{
    // The rest of your code

    // Change this line return new string(partialWord);
    // to this 
    return new GameWord{ OriginalWord = word, GuessWord = new string(partialWord)};
}
在表单中,创建一个私有字段,如下所示:

public string GetWordAfterHint(string wordToProcess, string originalWord)
{
    List<int> emptyIndexes = new List<int>();
    for (int a = 0; a < wordToProcess.Length; a++)
    {
        if (wordToProcess[a] == '_')
        {
            emptyIndexes.Add(a);
        }
    }

    // in case if word doesn't have empty positions
    if (emptyIndexes.Count == 0)
    {
        return wordToProcess;
    }

    Random random = new Random();
    var indexForLetter = random.Next(emptyIndexes.Count);

    // create stringBuilder from string, because string is immutable and you can't change separate symbol
    StringBuilder sb = new StringBuilder(wordToProcess); 

    // insert symbol from originalWord in empty previously generated position
    sb[emptyIndexes[indexForLetter]] = originalWord[emptyIndexes[indexForLetter]]; // 

    //convert stringBuilder to string and return
    return sb.ToString();
}
private GameWord currentGameWord;
this.textBox1.Text = this.currentGameWord.GuessWord;
当您有随机单词时,调用
GetPartialWord
方法并将返回的单词存储在
currentGameWord
中:

this.currentGameWord = GetPartialWord(someWord);
由于该方法现在返回一个对象,请按如下方式绑定文本框:

public string GetWordAfterHint(string wordToProcess, string originalWord)
{
    List<int> emptyIndexes = new List<int>();
    for (int a = 0; a < wordToProcess.Length; a++)
    {
        if (wordToProcess[a] == '_')
        {
            emptyIndexes.Add(a);
        }
    }

    // in case if word doesn't have empty positions
    if (emptyIndexes.Count == 0)
    {
        return wordToProcess;
    }

    Random random = new Random();
    var indexForLetter = random.Next(emptyIndexes.Count);

    // create stringBuilder from string, because string is immutable and you can't change separate symbol
    StringBuilder sb = new StringBuilder(wordToProcess); 

    // insert symbol from originalWord in empty previously generated position
    sb[emptyIndexes[indexForLetter]] = originalWord[emptyIndexes[indexForLetter]]; // 

    //convert stringBuilder to string and return
    return sb.ToString();
}
private GameWord currentGameWord;
this.textBox1.Text = this.currentGameWord.GuessWord;
然后在按钮的单击处理程序中执行此操作(处理程序将具有不同的名称):


你能把你的问题说得更清楚吗?@Vladimir,我为这个问题如此含糊而道歉。我只想添加一个提示按钮,在我的部分单词中添加另一个字母。@CodingYoshi我试过你的方法,效果很好。再次感谢您的建议和见解。我正在尝试罗马的方法,但是没有成功。我想我写的东西是我这边的错。谢谢Roma,如果它有效的话,我会实现它,因为它相对较短!请你解释一下下面几行好吗<代码>StringBuilder sb=新的StringBuilder(wordToProcess);sb[EmptyIndex[indexForLetter]=原始词[EmptyIndex[indexForLetter]]
@UnknownR,请参阅编辑后的答案,我添加了简短的评论。嗨@Roma请原谅我的知识不足,但是我在实现这段代码时仍然有困难。当我按下提示按钮时,从我上面发布的代码中看到的部分单词保持不变。为了显示另一个字符,我应该向hinttextBox.Text中添加什么?但是您只向该方法传递一个参数。我的实现需要两个参数:第一个-单词中缺少字母,第二个-相同的单词中包含所有字母。在这个示例中,您应该用word
“game”
通过以下方式调用此方法:
hintTextBox.Text=GetWordAfterHint(“\u a\u e”,“game”)