Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building an Evil Hangman game (like Guess the Word, but very hard to win by the user, because the computer cheats by keeping its secret word options open)

I have a Dictionary of strings and string Lists. The strings are the keys and they represent patterns of word families (i.e ----, e---, -e--). The lists of strings are the values, and each list contains words that correspond to the pattern (---- includes all words that don't have the user input letter, e--- includes all words that start with e and only have 1 'e' throughout the word and so on)

The Dictionary gets updated with each letter guess from the user. Basically, new families get created based on each new guess, and the computer selects the largest family as its current list of "secret words". I am using Max() to get the int value of the biggest family, and then compare that value to the Count of each family in order to select just largest.

int max_value = families.Max(family => family.Value.Count());


While testing and guessing more and more, at some point the code breaks and I get the error
"Sequence contains no elements"


What I have tried:

I looked up the error and found it was caused by the Max() function, and found solutions like DefaultIfEmpty(), but I'm not sure how I could apply it in my situation.
I am also confused, as the code breaks when, for example, there are still 6 lists of families in the dictionary, and hence should not be empty.

Any suggestions are welcome, both including Max() or something I could replace Max with.
Posted
Updated 1-Dec-20 4:16am
v2

1 solution

The problem is pretty clear - families is an empty collection. Since it has no elements, it has no maximum or minimum value and the aggregate methods will understandably complain because there is no sensible value they can return!

The real problem is why. Why is families empty? What code was supposed to fill it? Did it get executed? If not, why not?

We can't tell you that - we don't have access to your code, and you need it running to work out what is behind this.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and on everywhere that should be filling your collection, and then run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900