简体   繁体   English

如何在C#中获取英语中所有4个字母单词的列表

[英]How to get the list of all 4 letter words in the English language in C#

I am a beginner and have been playing around with C#. 我是初学者,一直在玩C#。 I have recently written the code for the very popular 'Cows and Bulls' word game. 我最近为非常受欢迎的“牛和公牛”文字游戏编写了代码。 Its working for 2 players ie; 它为2名球员工作,即; One player thinks of a word and the other person has to guess it. 一个玩家想到一个单词而另一个人必须猜测它。 I am giving only 10 chances for guessing. 我只有10次机会猜测。

I now want to make the game for a single player(Computer v Human). 我现在想要为单个玩家制作游戏(Computer v Human)。 I was wondering if there was anyway of getting all the 4 letter words in the English Language without Letter repetition(I have limited the game to 4 Letter words only). 我想知道是否还有没有字母重复的英语语言中的所有4个字母的单词(我将游戏仅限于4个字母单词)。 I know I can use an Enumeration and write down all the 4 letter words. 我知道我可以使用Enumeration并写下所有4个字母的单词。 But that's Hard Coding the Words. 但那是硬编码的话。 That would be my last option.I could type all the words in,but I would then have some idea of the word if I play the game. 这将是我的最后一个选择。我可以输入所有单词,但如果我玩游戏,我会对这个单词有所了解。

Assuming you have a list of words called words list 假设你有一个名为words列表的words列表

You can use regex to select 4 letter unique words 您可以使用正则表达式选择4个字母的唯一单词

 List<string> 4letterUniqueWords=words
.Where(x=>Regex.IsMatch(x,"^(?=^[a-zA-Z]{4}$)(?!^.*?(.).*?\1.*?$).*?$"))
.Select(y=>y)
.ToList<string>();

^(?=^[a-zA-Z]{4}$)(?!^.*?(.).*?\1.*?$).*?$
  ---------------  ------------------ ---
        |                  |           |->matches if both the lookaheads are true
        |                  |->checks if there are any repeating words
        |->checks if the words contains 4 letter alphabetic words

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

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