Archive for June, 2011
A simple puzzle game (part 2)
In part one of this post, I walked through the logic and game rules of a puzzle game, written in functional style. This time I’m going to turn that into a real game, that you can give to people to play: it will have graphics, animations, and run without installing Lua or any other dependencies.
I’m going to do this by using a game framework called Löve. I don’t want this blog to turn into library-of-the-week, but it’s impossible to do this stuff in a blog entry without using some framework, and Löve is a good one: it’s free and open-source, works on all three platforms, and its “Hello, World” is under five lines. So let’s download it and put some game rules into it!
Read the rest of this entry »
A simple puzzle game (part 1)
You know how if you have a box of little square things, and you rotate it, all the pieces thunk to the bottom and it’s kinda satisfying? Even more so if all the pieces are (say) dice, and they make a neat stack when they hit. That was my inspiration when I decided to make a puzzle game the other day. In this game, you have a square board partially full of colored pieces, and each turn you can rotate it ninety degrees either left or right, and all the pieces fall to the bottom. The pieces are four different colors, and there are four special pieces (one of each color) that are crushers. A crusher landing on top of a piece of its color will crush that piece and shift down one square.
What I’m going to do is talk about the design of the game and how I implemented the rules in this post, and then how I wrote the graphics and interface code in the next post. The post after that will have the final version of the game, with some explanation about how to organize code like this. But first, let’s write the game rules.
Read the rest of this entry »
A Toy Regular-Expression Matcher
There’s a book I have, Beautiful Code, which is a bunch of essays about programming by various famous programmers. It’s a great book. The chapters are on all different topics; the only thing they have in common is a piece of code that makes you think “wow, that’s really clever”. This post is about chapter one of that book, which is about a regular expression matcher.
Regular expressions are a domain-specific language for describing finite state machines. Specifically, finite state machines where the state transitions are successive characters in a string: you can use them to check if input strings match certain formats, like, a phone number: three digits followed by a dash followed by four digits. Kernighan’s matcher, which I’m going to translate to Lua and then extend a little bit, is really compact and beautiful. It’s about thirty lines, and recognizes these tokens:
- A dot (“.”) matches any single character
- A caret (“^”) matches the beginning of the string
- A dollar sign (“$”) matches the end of the string
- A star (“*”) matches zero or more repetitions of the previous token
- Any other character (like “a”) matches itself.
My plan is first to duplicate Kernighan’s matcher in Lua and then extend it to also handle character sets, like “[abc]” would match a single “a”, “b”, or “c”. Because of this, I’m not going to port his code across directly, but rather make some changes for extensibility.
Read the rest of this entry »