2014/03/01

The Making of a Word Game

Originally published in the TGC Newsletter March 2014

There are many grid-based word games on the market at present, with other popular titles such as Words With Friends,Word Feud, Wordz Up and many more. Sean Mann of Napland Games gives us an insight on the makings of such an application.

Word lists

Wordspionage
All word games need a qualified word list. There are many free lists out there and the best public domain list is ENABLE (Enhanced North American Benchmark LExicon) available from the Google Project site
This list contains some 170,000 words and is widely accepted as one of the best. It did need some minor modifications to be ready for use in a game. For example, we removed all single letter words since you have to play at least 2 letters and we removed all words longer than 15 letters since the board is 15x15 tiles. We also removed any words which have a purely vulgar meaning as we felt that they were inappropriate for a game designed for all ages. We also added all of the commonly accepted 2-letter words from other games. 


The list also had to go through some modification so that it could be accessed in an optimal way. A word list of 170,000 words can be a bit cumbersome to search. If the list was stored on the device it would be very slow. We stored the list in our MySQL database on our server which is very fast for searches, but still the list was split into multiple small tables so that search time would never take long (indeed it takes 0.0019 seconds to search our largest list). The way we split up the lists is straight-forward. There is a list for every letter and word length combination (i.e. all words beginning with A and 2 letters long, A and 3 letters, A and 4 letters, etc.). To accomplish this I created a simple program that parsed the big word list and simply saved it into smaller text files. It created quite a few text files (about 26 * 14) but they were easy to mass import to a MySQL database.

Protecting the data One thing to look out for when importing is that text files have characters for new lines and these can get stored in your database, which potentially can cause searching issues. Another major advantage of having the list stored on a server is we can add and remove words on demand. We've already had a number of users request additional words and have added some of them to our dictionary immediately. Leveraging an online database to control elements in your game is very useful. We can also send out news messages to our players from our online database and lock out obsolete versions of the game if we ever need to.


Since our word lists are stored in our protected database online, we didn't have to worry about users potentially modifying the files. If you plan to store your files within the app you should consider your own method of encryption and validation to ensure that users haven't modified the files. One of my favorite methods is simply storing a key in an array for your program and using that key along with XOR to encrypt/decrypt the bytes in a data file.
This method made the basics of verifying a word to our dictionary very quick and simple. The more difficult part was verifying the words on the board since you have to take into consideration the various possible crosswords. The idea behind the algorithm for doing this is quite simple, but implementation was a bit tricky. Simply determine the upper-most, left-most letter that has been placed on the board and look left, right, up, and down for other unplayed letters or played letters. 

I hope this gives a brief insight into Word Games and inspires you to think about making your own in the near future.

2013/04/29

Preparing for Game 2 - Learning PHP and MySQL

In order for my next game to be able to save user data and transfer data for turns and chat I'll need to set up a server and learn how to get data on and off it vial simple HTTP commands. I began my adventure today and will chronicle what I've done so others can easily set up a test system. I feel that what I'm trying to do is so basic (send data to be written to a database and retrieve that data)

First thing's first, set up Apache Server. This was pretty easy: download the ZIP for Apache 2.0, extract the files to the directory of your choice (I chose c:/Apache), make a few minor changes to its config file, and then just open up some ports on your router and unblock them from your firewall. That's it! I had this part of the project going before noon today and was pretty happy with the information I was able to find.

There are a ton of guides out there on how to set up Apache server (2.0 Win32) in Windows, so I'm not going into great detail here. One thing I should mention is that you should be careful with security. If you're just testing an app then shut down your server when not testing. Once the server is installed you should see an icon in your taskbar's icon try (tiny up arrow over by the clock). Left clicking on this will allow you to quickly start/stop/restart the server.

After you've downloaded the ZIP for Apache server 2.0 for win32 x86 and extract it to the directory of your choice you can start meddling with the configuration file. It's called httpd.conf and located in the Apache/conf folder. Simply open it up with Notepad and make the following changes:

  • Find thisServerName localhost:80 and replace localhost with whatever name you want. This will be the name you type into your browser on the server computer to check things as you set them up. I left mine alone.
  • You also might want to change this ServerAdmin admin@localdomain to whatever email address you might want to use. Again I left this alone as my server won't need email functionality.
  • If you don't want to use the default directory for you website's files then change the DocumentRoot "C:/Apache/Apache2/htdocs" to the directory of your choice.
  • Change the line DirectoryIndex index.html to also include index.php - we'll use that later after we set up PHP.
  • Next we'll want to set up a password. This is done by going to the Apache/bin directory then hold shit and right click in a blank area of the Windows Explorer window and "Open Command Window Here" for a command prompt. Type htpasswd -c "c:/Apache/Apache2/pw.txt" yourName. Replace the directory with your actual directory and replace yourName with whatever name you want for the admin account. You'll then be prompted to create a password and you're done.
  • Now we need to create a script to manage the login. Create a file in Notepad and name it .htaccess then enter the following:
AuthType Basic
AuthName "This is a private area, please log in" 

AuthUserFile "c:/Apache/Apache2/pw.txt"
AuthGroupFile /dev/null 

<Limit GET POST PUT>
require valid-user
</Limit>
  • Restart Apache server and then open a browser and enter localhost if all went well you should be prompted to enter your username and password. If you didn't create any index.html or index.php yet then you won't see anything else, so go ahead and create that if you want.
  • Now you're going to want to set this up so that you can access it via the web so open up your router's settings and port forward port 80 for the IP address of your server. Then set your server's IP to be in the DMZ (usually under WAN setup).  You may also want to make sure the computer you're using as a server has a fixed IP on the network and the address has been reserved on the router. 
  • Finally you'll want to open up your firewall and add an exception for two-way traffic on port 80. Now you should be able to go to any computer on the internet and login to see your website.
That's all it takes. Many other guides I see have a bit more detail, but they seem like they expect you don't know much about changing your router settings or how to navigate Windows Explorer to find files. I hope that this guide is a bit more to-the-point and helps the slightly advanced user get up and running quickly. If you're stuck on any point you might want to search Google for a more detailed guide. For many I hope this will get you up and running in a jiffy.

Next up is setting up PHP and then MySQL. Thanks for reading, see you next time!