Tuesday, August 10, 2010

The Tries and Tribulations of Allison

No, that's not a typo in the subject line--I just got through coding the trie I talked about earlier for the lexicon. This was difficult for two reasons:

-Flash doesn't have a built-in trie structure, and
-Flash doesn't have generics.

For the non-programming inclined, you can think of generics like labels. When you clean out the basement, you try to put everything into boxes with things of the same kind. For the sake of argument let's say you end up with a box of My Little Ponies. Obviously you would put a label on the box and write "My Little Ponies" so you don't forget what it is. Java has the same sort of thing with data structures, so for instance, you can tell Java that you want a Vector (a specialized type of array that I won't discuss right now) of Strings. Then every time you get something out of the Vector, Java knows it's a String.

Flash does not work this way. That would be too simple. If you want to create a general data structure with Flash, you have to tell it to accept a nebulous class of items called "Objects" which can be just about anything--words, numbers, cupcakes, what have you. Imagine throwing an "Objects" label on every box you fill up while cleaning the cellar. Not very useful, is it? Well, it's about that useful in Flash. When you get something out of that data structure, Flash has no blooming idea what it is. To get it to recognize the Object as a more specific type of Object, you have to do this thing called "casting," which is basically telling Flash "Remember this thing here? It's a String." And you have to do that every time you retrieve something from your data structure.

The way around this is to create a data structure that only holds the specific type of information you're looking for. An analogy for this would be maybe getting the box straight from the manufacturer with "My Little Ponies" already printed on it (silly, I know, but work with me here). On the one hand, you know exactly what's in the box; on the other hand, the box isn't good for much else. It works the same way in Flash: while coding my trie, I had to tell it "okay, you're only going to accept Strings." Now I know that everything I get out of that trie is going to be a String, but I can't really use the trie for anything else unless I completely recode it.

Stupid? Yes, but Flash is much better for doing animations than Java, so I'll let it slide.

The end result of all this is that I now have my own specialized trie (which I've named a LexiconTrie, for obvious reasons) to hold my lexicon. I'd explain how it all works here, but Wikipedia's article on tries has an example pretty much identical to what I did, so go there and listen to people who can type better than I do. I'll be starting on the graphics for the game soon, so hopefully there should be some pretty pictures for you to look at in the near future. Til then, seeya, folks.

No comments:

Post a Comment