Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question about Dictionaries

Discussion in 'Scripting' started by FlamingVorpalCow, Oct 8, 2019.

  1. FlamingVorpalCow

    FlamingVorpalCow

    Joined:
    Oct 29, 2014
    Posts:
    6
    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?

    Any tips?
    Thanks in advance
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    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<string> will be much better. All you need is a list of words that are legal.
     
  3. FlamingVorpalCow

    FlamingVorpalCow

    Joined:
    Oct 29, 2014
    Posts:
    6
    Thanks for the fast response!
    The List <string> 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?
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Yes. A text file, for example.
     
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    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.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Yep, List would be slow for lookups and HashSet<string> 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).
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    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.