Click here and the assortment of tiles you saw last week should show up once again. However, there is an important difference this time. Try dragging the tiles up, down, left and right. Notice anything?
That's right! The tiles move now!
How I did this was by using a two-dimensional array. What that means is that you have one array (we'll call it "tileGrid"), and each slot in that array is another array. Each slot in the first array represents a row of tiles, and each slot in each of the other arrays represents a single tile. However, it's not as easy as that, because when you move the tiles, the arrays get messed up. TileGrid[0][0] might now be at position (0, 4). So you can't just write a for loop shifting every tile up or down by 1 for each scroll and expect it to work after you've moved the tiles more than once. The underlying array has to line up with what's displayed on the screen.
I fixed this by having the array shift along with the tiles. Another exceedingly obvious problem that caused me to pull my hair out because I couldn't figure it out at first. All part of the learning process, I guess.
After this I just have to work on the actual game aspect--in other words, selecting and checking words from the tile grid.
Portrait of the Blogger as a Compsci Major
Sunday, August 22, 2010
Saturday, August 14, 2010
Look, look! I have tiles!
And here they are!
Reload. And reload. And reload again. That's right; the letters are randomly generated. But the process isn't entirely random; it's based on letter frequencies that occur in the English language. For those interested, here's how I did it:
One of the things Flash comes with is a Math class, which contains several useful mathematical functions. I'll do a Flash coding primer when I'm finished with this project, but for now let's just take it for granted that the Math.random function produces a random number between 0 and 1. This may sound like an oxymoron in terms, but keep in mind that a number doesn't have to be a whole number; you can have 0.513, for instance.
Wikipedia's article on letter frequency in English was kind enough to include a list of letter frequencies as percentages. By taking these as percentages of 1 and using the resulting percentages as cutoff points for a particular letter, I was able to code a rather massive if...else block. For example, if the random number is less than 0.08167, create a tile with an A on it. If the random number is greater than that but less than 0.09659 (0.08167 + 0.01492), create a tile with a B on it... et cetera, et cetera. Like all things, it was really obvious once I thought of it, and I'm mad I didn't think of it sooner.
Next up, scrolling! Won't that be fun!
Reload. And reload. And reload again. That's right; the letters are randomly generated. But the process isn't entirely random; it's based on letter frequencies that occur in the English language. For those interested, here's how I did it:
One of the things Flash comes with is a Math class, which contains several useful mathematical functions. I'll do a Flash coding primer when I'm finished with this project, but for now let's just take it for granted that the Math.random function produces a random number between 0 and 1. This may sound like an oxymoron in terms, but keep in mind that a number doesn't have to be a whole number; you can have 0.513, for instance.
Wikipedia's article on letter frequency in English was kind enough to include a list of letter frequencies as percentages. By taking these as percentages of 1 and using the resulting percentages as cutoff points for a particular letter, I was able to code a rather massive if...else block. For example, if the random number is less than 0.08167, create a tile with an A on it. If the random number is greater than that but less than 0.09659 (0.08167 + 0.01492), create a tile with a B on it... et cetera, et cetera. Like all things, it was really obvious once I thought of it, and I'm mad I didn't think of it sooner.
Next up, scrolling! Won't that be fun!
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.
-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.
Saturday, August 7, 2010
I hate XML.
Today I started on the XML dictionary hoping it would be the easiest part. It wasn't.
The problem is that, when you convert a dictionary consisting of 120,000 words (give or take) into XML, it takes up 2.81 MB of space. That, friends, is a BIG file, and it turns out Flash won't load it without some serious hemming and hawing. Unfortunately, Flash has a built-in bailout feature where if a function takes longer than 15 seconds to execute, it will automatically terminate it. How long does it take to load a 2.81 MB file into Flash? I was never able to figure it out exactly, but it's longer than 15 seconds.
Fortunately, there was another way I hadn't thought of. In addition to XML, I also have this dictionary in a simple .txt format. That file takes up about 1.24 MB, which, while still huge for a plain text file, is less than half the size of the XML file. More importantly, in this file, each word is on a new line. I will explain why this makes things easier for me.
The non-programming types among you may not know this, but every time you press the Enter key on your keyboard, your operating system inserts what's called a "newline" character. You don't see this character, but it has to be there or the computer doesn't know where to put a new line. This character is different on different computers, but on mine it's "\r." Hence, if I have this file:
a
an
the
it's really more like this:
a\ran\rthe
When the file is loaded, the "\r"s are replaced with new lines, so we as end users never actually see these characters. But Flash knows they're there, and it also comes with a handy "split" function that can be used on chunks of text (called "strings" in Flash, incidentally). The split function, as the name suggests, splits a string apart wherever a specified character occurs and saves the resulting collection of strings as an array, which is Flash's way of keeping a group of things together. Therefore, if you tell the split function to split up the string "a,an,the" at the commas, you get an array containing the strings "a", ",an", and ",the." Do you see where I'm going with this?
YES! If I use the split function to break up the text file at the newline characters, then trim off the first character from each resulting string, I get an array containing all the words in my dictionary. Nifty, eh?
And the rest, as they say, is history. To load the text file into Flash so I could modify it, I used something called a URLLoader, which is how Flash loads data from external sources. This gives me the dictionary as one long string that I can break up into an array of smaller strings. XML isn't necessary, and best of all, this way actually works, a cornerstone of any good Flash application. Sweet.
Tomorrow I'll start working on adding all the words into a trie, which should make searching the dictionary for a specific word considerably faster. Until then, folks, until then.
The problem is that, when you convert a dictionary consisting of 120,000 words (give or take) into XML, it takes up 2.81 MB of space. That, friends, is a BIG file, and it turns out Flash won't load it without some serious hemming and hawing. Unfortunately, Flash has a built-in bailout feature where if a function takes longer than 15 seconds to execute, it will automatically terminate it. How long does it take to load a 2.81 MB file into Flash? I was never able to figure it out exactly, but it's longer than 15 seconds.
Fortunately, there was another way I hadn't thought of. In addition to XML, I also have this dictionary in a simple .txt format. That file takes up about 1.24 MB, which, while still huge for a plain text file, is less than half the size of the XML file. More importantly, in this file, each word is on a new line. I will explain why this makes things easier for me.
The non-programming types among you may not know this, but every time you press the Enter key on your keyboard, your operating system inserts what's called a "newline" character. You don't see this character, but it has to be there or the computer doesn't know where to put a new line. This character is different on different computers, but on mine it's "\r." Hence, if I have this file:
a
an
the
it's really more like this:
a\ran\rthe
When the file is loaded, the "\r"s are replaced with new lines, so we as end users never actually see these characters. But Flash knows they're there, and it also comes with a handy "split" function that can be used on chunks of text (called "strings" in Flash, incidentally). The split function, as the name suggests, splits a string apart wherever a specified character occurs and saves the resulting collection of strings as an array, which is Flash's way of keeping a group of things together. Therefore, if you tell the split function to split up the string "a,an,the" at the commas, you get an array containing the strings "a", ",an", and ",the." Do you see where I'm going with this?
YES! If I use the split function to break up the text file at the newline characters, then trim off the first character from each resulting string, I get an array containing all the words in my dictionary. Nifty, eh?
And the rest, as they say, is history. To load the text file into Flash so I could modify it, I used something called a URLLoader, which is how Flash loads data from external sources. This gives me the dictionary as one long string that I can break up into an array of smaller strings. XML isn't necessary, and best of all, this way actually works, a cornerstone of any good Flash application. Sweet.
Tomorrow I'll start working on adding all the words into a trie, which should make searching the dictionary for a specific word considerably faster. Until then, folks, until then.
Friday, August 6, 2010
Welcome!
Welcome to the blog of a reclusive, technologically-inclined, and often slightly insane girl from Massachusetts. I'm a computer science major at Mount Holyoke College, and when I'm not going insane trying to juggle schoolwork, my job as a teacher's assistant, a social life, and sleep (read: not often) I like to program small projects to amuse myself. I've started this blog to chronicle and publish my programming projects, in the hopes that someone out there on the Internet will get a few minutes of enjoyment and maybe a chuckle out of what I churn out. Maybe that someone will be you.
Keep in mind that this is a programming blog. Where things may be unclear to the average layperson, I have tried to insert reasonable explanations, but my clarity is not guaranteed. I recommend having at least a basic understand of computer programming before you read this blog.
With no further ado, let me introduce to you Project 1: The Sliding Word Puzzle
It is dark. Before you is a grid of tiles bearing assorted letters. Both the rows and columns of the letter grid slide back and forth easily, almost as if this were a Flash game ignoring the problem of friction completely. The purpose becomes clear: you must slide the rows and columns to form words either vertically or horizontally. And so, your task begins.
The idea for this seemed so simple that I actually spent about an hour Googling to make sure no one else had made this game already. So far it looks like no, but please tell me if this already exists.
Problems I anticipate:
Keep in mind that this is a programming blog. Where things may be unclear to the average layperson, I have tried to insert reasonable explanations, but my clarity is not guaranteed. I recommend having at least a basic understand of computer programming before you read this blog.
With no further ado, let me introduce to you Project 1: The Sliding Word Puzzle
It is dark. Before you is a grid of tiles bearing assorted letters. Both the rows and columns of the letter grid slide back and forth easily, almost as if this were a Flash game ignoring the problem of friction completely. The purpose becomes clear: you must slide the rows and columns to form words either vertically or horizontally. And so, your task begins.
The idea for this seemed so simple that I actually spent about an hour Googling to make sure no one else had made this game already. So far it looks like no, but please tell me if this already exists.
Problems I anticipate:
- Storing the lexicon in a format that a.) is easily readable by Flash and b.) won't take up too much space. XML seems like the easiest format since Actionscript already has classes and methods for processing that.
- The whole sliding thing. You can click and drag a mouse in any direction, but the tiles can only move up and down or left and right. How do I program this so that the tiles know which way they're going and don't move all weird?
- Checking whether a word is valid without putting undue strain on the processor. Checking a lexicon of several thousand words one by one is going to take a lot of time and effort. Perhaps I could devise a way to check only words that start with the appropriate letter? Or... wait... perhaps I could form a trie of letters that when read from root to end spell out all the words in the lexicon! Yes, I think I will go with the trie option. Mostly because I've already written Java code for doing that that I can convert to Actionscript relatively easily.
Subscribe to:
Posts (Atom)