First, a little background: One peculiarity of academic life at Oberlin is the institution of Winter Term. At least three times during each student’s tenure here, (s)he must take the month of January to complete a faculty-approved project, either individually or in a group, either on- or off-campus.
My third Winter Term project, completed last January, was to build a relatively simple card game application, thereby learning the basics of Objective-C and Cocoa development. The name of the game is Mod10 (“mod ten”). It’s one I’ve known for a long time, but I’ve never been able to find any reference of it online. The rules are simple:
Begin with a shuffled deck, face down, and deal seven piles of two cards each, face up, in a left-to-right fashion. Then continue to deal cards from the deck one at a time, left to right, checking for exposed triplets where the sum of the values of the three cards is evenly divisible by 10. When such a triplet is found, pick it up and return it to the deck. Aces have value 1, all face cards have value 10, and all other cards have value equal to the number of the card. Piles that have been emptied are no longer dealt to. The game is won when all cards are returned to the deck, and the game is lost when the deck is empty.
That leaves some questions unanswered, among them “What is an exposed triplet?” By that I mean any run of three cards in the same pile, where at least one of the cards is at the top or bottom of the pile, and where the top and bottom are considered contiguous. Let’s try some pictures to clear this up; these are the four possible positions of a valid triplet, marked by x’s:
I’d be lying to you if I claimed this is a very rewarding game to play. It’s not. I’ll leave things like the win frequency and strategy choices to another post, but suffice it to say that it’s not often and there aren’t many. Perhaps its simplicity and its determinism—more on that coming later, too—are what keep me interested.
When I started this project, I’d taken the first of Oberlin’s two introductory “Principles of Computer Science” courses—these are taught in Java—but that was the limit of my formal CS training. (I knew a little Ruby and some shell scripting before that, but most of those insights were overshadowed by that first CS course.) So the first step was to learn Objective-C, which I did by working my way through Kochen’s Programming in Objective-C 2.0. From there I started to play with the language and the Cocoa libraries, first building a CLI version of Mod10 to work out the game logic, then learning how to register mouse events, update the game board, etc. The end result is what you see in the image above, plus some simple GUI elements in one corner. If you’d like to give the game a try, you can download a universal binary from the GitHub repository here. Bear in mind that this app is compatible with Mac OS X 10.6 and later, and it may not run on Leopard or earlier.
The Xcode project, will all the source code, is also available in that repository. I must admit a certain self-consciousness about that code. At the start of the project, words like “pointers” and “memory management” were almost entirely foreign to me. By the end, that was less true, but I wouldn’t exactly call my self “well-informed” at that point. It wasn’t until last semester’s systems programming course, in which I learned C the “right way” (as opposed to jumping into a confusing superset of C), that those concepts became clear. Perhaps I’ll make it a weekend project this semester to work through the code and fix all the mistakes I’m sure I made.
This project, while relatively small, had a big impact on my life. I’m not sensationalizing things when I say the time I spent on this project catalyzed my shift away from biochemistry and into computer science. I’ll leave all that internal drama, as well as some further musing on Mod10, to future posts. If you’re an OS X user, grab the game from GitHub and give it a try. Then come back and tell me in the comments what you think!