在C#unity中随机生成的字符中查找有效单词 [英] Find valid words in randomly generated characters in C# unity

查看:135
本文介绍了在C#unity中随机生成的字符中查找有效单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时生成了随机字符,生成后我需要知道用列出的字符组成的有效字数。这不是矩阵形式,它将显示在一行中。



这是生成随机字符的代码:

 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 =
辅音[a] .ToString();
}
for(int j = 0; j< = 2; j ++)
{
wordRan.wordsStore.Clear();
b = UnityEngine.Random.Range(0,vowels.Length);
availablesetVowels [j] .GetComponent< TextMesh> ()。text =
元音[b] .ToString();
}
}



它的字面意思是排列,但这个词应该是有意义的。我有文本文件来比较我通过文本资产加载..它实际上是一个单词加扰器游戏在这里我需要显示从列出的随机字母中解读的有效单词的数量 -



void Awake(){string [] w = wordAsset.text.Split(new char [] {'\ n','\ r'

},System.StringSplitOptions .RemoveEmptyEntries); allWords = new

HashSet(w);



}

public void Start()

{

// RepeatedWordsCheck();

}

public void更新()

{



}



public bool CheckWord(string wrd)

{

返回allWords.Contains(wrd.ToLower());



}

public void ClickFun()

{

Debug.Log (acx);

bool Check = CheckWord(GameController.currentWord);

if(Check == true)

{

Debug.Log(正确的单词);

}

}



我尝试了什么:



i试图找到permutaions但我不能在foreach中使用数组,所以我可以只获取单字符串值

public void Sorting()

{

storeConsonants.Sort();

foreach (storeConsonants中的字符串f)

{

Debug.Log(string.Format({{{0} {1}}},f,f);

}

}

解决方案




这是WinForms版本 - 字谜 - C#中的文字游戏 [ ^ ]



这里是WPF版本 - Anagrams2 - 简单的WPF游戏应用程序 [ ^ ]



BTW,这些应用都没有使用Unity。



你感兴趣的是 Globals .Contains()方法。它被称为游戏词典中的所有单词。字典中的所有单词都是 Scrabble 游戏中的有效单词。



有趣的笔记 - 我写了Winforms版本帮助我学习.Net和C#,然后写了WPF版本来帮助我学习WPF。


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");
}
}

What I have tried:

i have tried in finding permutaions but i cant use array in foreach so i can only get single string value
public void Sorting()
{
storeConsonants.Sort ();
foreach (string f in storeConsonants)
{
Debug.Log(string.Format ("{{{0} {1}}}", f,f);
}
}

解决方案

I wrote an article that might fit your needs. It's also word scramble game (I call it Anagrams). It starts out by selecting a word in the dictionary (from 6 to 10 characters), scrambles the letters, and presents the scrambled sequence to the user. At that point, the user can attempt to manually discover the valid words (3-n letters, where n is the number of characters in the scrambled word) in the scrambled text. Check out the article and see if you can use any of the code.

Here's the WinForms version - Anagrams - A Word Game in C#[^]

And here's the WPF version - Anagrams2 - A Simple WPF Game Application[^]

BTW, neither of these apps use Unity.

What you're interested in is the Globals.Contains() method. It's called for all words that are in the dictionary of game words. All words in the dictionary are valid words in the game of Scrabble.

Interesting note - I wrote the Winforms version to help me learn .Net and C#, and then wrote the WPF version to help me learn WPF.


这篇关于在C#unity中随机生成的字符中查找有效单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆