Hi, i´m starting to develop a word game, much like a videogame implementation of Scrabble ( i know there are a lot of this out there, but it’s an implementation of a boardgame variation i’m about to publish and wanted to learn game creation with this project).
My question is this. How would you go about implementing a list of words that are valid, and checking if the player’s input is valid?
I’ve got and understanding that dictionaries are a good way to create lists, but given that the more expansive I can create the dictionary, would that be the optimal way? or should I use a Json file for it?
I think you are on the right track, but are getting ahead of yourself a little bit - yet in the context of scrabble, a dictionary seems attractive because that’s what we use in real life to determine if a word is legit. However, in this case a List will be much better. All you need is a list of words that are legal.
Thanks for the fast response!
The List would be fed some external file where the actual words are, right? so it can be updated without having to go back into the code?
This is incorrect, actually. Searching through a List to see if it contains a string takes longer as the list grows, meaning it can take quite a bit to check a list containing most of the words in the English language, but checking to see if a Dictionary contains a key doesn’t scale based on how many elements are in the dictionary.
That said, OP, a dictionary probably isn’t quite the correct collection type for what you want, since a dictionary consists of key/value pairs, and you probably don’t need to store information about each word (if you do, then it’s what you want)! The collection you’re looking for is a HashSet, which will allow you to store a list of words, but be able to nearly instantaneously check if the HashSet contains a certain word.
Yep, List would be slow for lookups and HashSet would be better.
Or an even better one could be something like a b-tree, or even a trie which is pretty much designed for this sort of thing. Since these are sorted as well as have quick lookup. Which is useful if you’re looking for near words as well (which scrabble is something you’d probably want to do).
A List could be a sane option if the contents don’t change at runtime, you keep the entries in sorted order, and you use a binary search for lookups. (At which point you are effectively using a List to implement your own binary search tree.)
But given the option of using an off-the-shelf implementation of a tree or hash table, that would probably be a better idea.