I've wanted to create a web-based game for a little while, and this is my first attempt. The game itself is pretty simple: It pickes a word and clue at random from a list of about 200 words. After that, it's like any other hangman game: you pick a letter and add a limb for every wrong guess. Using great works of art as the "hangees" just seemed like a nice touch to make the game more fun. (Plus, who doesn't enjoy staring at naked figures every once in a while?)
Here's the tricky part of the app: As with any game, you need to remember the current state. If it's a normal app, you just keep the state in memory and wait for a player move. In web/CGI, however, the app resides on the server and only runs briefly (then terminates) when the user takes an action. I.e., you can't store anything in memory because the program will exit immediately after sending the response. I thought about keeping data files on the server to keep track of the state, but that gets hairy too, especially since you could have multiple players at once, each with a different game. You'd have to assign unique session ids to each player, and keep track of all the simultaneous games on the server.
After some thought, I ended up with a scheme that I like: The entire state of the game is encoded in the URL (after the ?). That way, when you click on the link, the browser sends all the information about the game, so my server doesn't have to remember anything. Here's a sample URL:
The s parameter is a pseudorandom session id, which keeps the browser from caching the page. The w is the word index (with 2 digit checksum to keep people from tampering with it). The g is a list of all the guesses so far. The i is the image number (David, Vetruvius, etc.) When you click on a link (A..Z), its URL is what the state of the game will be if you were to click it. Again, the entire state of the game is encoded in the HTML page on the browser, so the server doesn't need to remember anything. This also allows any number of people on the web to play at once.
If you're interested, here's the source code. It's written in ANSI C, and uses Thomas Boutell's cgic library to parse the CGI parameters. It's fairly well commented and should be pretty straightforward, though I'd be happy to answer any questions about it. Feel free to use or modify it as you see fit.
I really enjoyed writing this game, though it took me a lot longer to write than I expected. I got the basic part working in an afternoon at work (shh!), but it took me several long nights to get it the way I like it. I have some plans for more games in the future, which I'll do if I have time. For now, enjoy the game!Back