简体   繁体   English

在C#unity中以随机生成的字符查找有效词

[英]find valid words in randomly generated characters in c# unity

I have generated random characters at runtime and after generating I need to know the count of valid words formed with the listed characters. 我在运行时生成了随机字符,生成后我需要知道与列出的字符形成的有效单词的数量。 And that is not in a matrix form, it will show in a single line. 而且这不是矩阵形式,它将显示在一行中。

This is the code for generating random characters: 这是生成随机字符的代码:

  public void  GenerateLettersAtStart()
 {
 for (int i = 0; i <= 6; i++)
 {
    wordRan.wordsStore.Clear();
    a = UnityEngine.Random.Range(0,consonants.Length);
    availablesetConsonants [i].GetComponent<TextMesh> ().text  = 
   consonants[a].ToString();
   }
   for (int j = 0; j <= 2; j++)
    {
    wordRan.wordsStore.Clear();
    b = UnityEngine.Random.Range(0, vowels.Length);
    availablesetVowels [j].GetComponent<TextMesh> ().text = 
   vowels[b].ToString();
    }
   }

its literally like permutations but the word should be meaningful. 它的字面意思是排列,但该词应有意义。 i have text document to compare i have load that through text asset.. it is a word scrambler game actually here i need to show the count of unscrambled valid words from the listed random letters – 我有文本文档可以比较,我是通过文本资产加载的。.实际上,这是一个单词打乱游戏,我需要显示列出的随机字母中未打扰的有效单词的数量–

void Awake() { string[] w = wordAsset.text.Split(new char[] { '\n', '\r' 
    }, System.StringSplitOptions.RemoveEmptyEntries); allWords = new 
    HashSet(w);

    }
  public void Start()
   {
  //RepeatedWordsCheck ();    
   }
    public void Update()
   {

    }

  public bool CheckWord(string wrd)
  {
  return allWords.Contains(wrd.ToLower());

   }
  public void ClickFun() 
   {
  Debug.Log ("acx");
  bool Check = CheckWord (GameController.currentWord);
  if (Check == true  )
  {
  Debug.Log("correct word");
  }
     }

Reorder letters in each real word in alphabetical order, eg. 例如,按字母顺序对每个实词中的字母重新排序。 word "cat" will change to "act". 单词“ cat”将变为“ act”。 Having such a dictionary you can sort randomly generated characters in same order and then you can just match strings by == operator. 有了这样的字典,您可以按相同的顺序对随机生成的字符进行排序,然后可以通过==运算符匹配字符串。

There are many solutions to this problem. 有许多解决此问题的方法。 The issue (as you probably guessed) is that basic brute force solution might be extremely slow (checking many combinations). 问题(您可能已经猜到过)是基本的蛮力解决方案可能非常慢(检查许多组合)。

Here is how I would do it: 这是我的方法:

Make a list of pairs: 列出配对:

var listOfPairs = new List<KeyValuePair>();

each word is saved as a pair like this: 每个单词都保存为一对,如下所示:

{{word, word}, {fork, fork}, {other, other}, ...} {{word,word},{fork,fork},{other,other},...}

now for each pair, compare it with all your random letter. 现在,将每一对与您的所有随机字母进行比较。 So if you have N words in the dictionary and 10 random letters, this will be done in max 10*N operation (complexity O(N), which is rather good) 因此,如果词典中有N个单词和10个随机字母,则将以最大10 * N的操作来完成(复杂度O(N),这相当不错)

Each time you compare a word with a letter, remove the letter from the first element of the pair: Lets take an example: you test the word word : 每次将一个单词与一个字母进行比较时,请从该对的第一个元素中删除该字母:让我们举个例子:测试单词word

{word, word} {字,字}

if the first random letter is o , you end up with 如果第一个随机字母为o ,则结果为

{wrd, word} {wrd,单词}

if the second random letter is a , you end up with 如果第二个随机字母是a ,则最终得到

{wrd, word} {wrd,单词}

if the second random letter is d , you end up with 如果第二个随机字母是d ,则最终得到

{wr, word} {wr,单词}

And so on. 等等。 To this for all words in your dictionary. 为此,对于字典中的所有单词。

At the end, all the pairs in the list with the first element being an empty string can be written with your random letters. 最后,列表中所有第一个元素为空字符串的对都可以用随机字母书写。

You can retrieve this using a for loop checking for empty keys, or using Linq: 您可以使用for循环检查空键或使用Linq来检索此内容:

var listOfPairs = new List<KeyValuePair>();

// Do the algorithm described above

// Retrieve words that can be written with the random letters
possibleWords = listOfPairs.Where(pair => pair.key == "").Select(pair => pair.value)

By the way, you could choose a random number of vowels in the generation code, to vary even more the possibilities: 顺便说一下,您可以在生成代码中选择随机数量的元音,以进一步改变可能性:

public void  GenerateLettersAtStart()
{
    int nbVowels = (int)Random.Range(2, 5);
    int nbConsonants = 10-nbVowels;
    for (int i = 0; i < nbConsonants; i++)
    {
        wordRan.wordsStore.Clear();
        a = UnityEngine.Random.Range(0,consonants.Length);
        availablesetConsonants [i].GetComponent<TextMesh> ().text  = 
        consonants[a].ToString();
    }

    for (int j = 0; j < nbVowels; j++)
    {
        wordRan.wordsStore.Clear();
        b = UnityEngine.Random.Range(0, vowels.Length);
        availablesetVowels [j].GetComponent<TextMesh> ().text = 
       vowels[b].ToString();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM