简体   繁体   中英

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#. I have recently written the code for the very popular 'Cows and Bulls' word game. Its working for 2 players ie; One player thinks of a word and the other person has to guess it. I am giving only 10 chances for guessing.

I now want to make the game for a single player(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). I know I can use an Enumeration and write down all the 4 letter words. 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

You can use regex to select 4 letter unique words

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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