Sunday, April 10, 2011

Minesweeper as an Introduction to Computer Science

The very first computer game that I remember playing is Minesweeper, in spanish Buscaminas (literally translates as Minefinder). It was probably 1997 during one day I was visiting the workplace of my father, I started exploring a computer with Windows 95. I think my father had loosely explained to me some of the basics about computers but I think it was mainly my experience with video games that made the transition smooth. I quickly navigated through the task bar looking for games and ended up clicking on the salient smiley face icon. I randomly clicked through the cells and ended up losing the game quickly and with disappointment. I had to wait some 3 years later until my family could finally acquire a personal computer for our house.

I liked minesweeper very much and I want to explain here how I embraced it in my education and what are the things I believe we can learn from such game that according to Wikipedia has been there since the early mainframes in the 60's. The first thing I liked about the game is that it is very self contained, it's more exciting if you figure out the game rules by yourself while trying it. You will quickly realize what are the meanings of the numbers when you start uncovering cells: The number of bombs around that cell. Then you will start realizing how to use this information and start finding patterns: The 1's in the corners, the 2's in the corners, several combinations of 2's and 3's and so on, those patterns that allow you to become faster and really master the game. Shortly after I started playing, my father and sister also liked the game and started playing it, often challenging ourselves in our computer.

After some time and with lots of spare time and a computer at home, I started exploring some basic programming. What I was actually most interested was in learning how to create webpages but at some point I found myself with programming some Javascript, mostly to open the infamous popup windows and creating some more stylish navigation menus. But I also started to wonder how to encode the rules of a game like minesweeper and thought maybe it would be a good exercise to try. I couldn't do it at the time and in my naive attempts without any guidance other than Yahoo Search and Altavista and the less known newcomer Google, I tried to learn the Pascal programming language to do the job but honestly I couldn't get very far on my own. I had to wait another 2 or 3 years until I was in my second year of college.

So I'm talking now about 2004, after an introduction to programming class and a data structures class under my belt. I was enrolled in the 'Object Oriented' programming class, a still hot topic at the time, at least in the local tech community back there. So I set my mind that I would use this class as an opportunity to implement minesweeper and play around with the rules of the game to create some variants of the basic game. The basic game requires an understanding of several things, there are two obvious things that you will gain from the experience:

1. Be good at manipulating arrays/matrices: Obvious! The game even looks like a matrix so this is the data structure you will need. You will have to traverse the matrix up and down, forward and backwards in every way possible. I implemented this in Java so I didn't need to think about dynamic allocation of arrays explicitly but if you want your game dimensions to be variable (beginner, medium, expert), then in a language like C you will want to go dynamic.

2. Be good at using recursion: This might not be too obvious but uncovering a cell with no number and no mine requires propagating a recursion call in several directions until you find a numbered cell. There's always a way to do it without recursion but recursion just comes naturally.

Beyond these two things you can learn about the power of random number generation when you're writing the routine to place the mines and also a basic convolution-like operation when you're assigning the numbers to the cells after the placement of the mines. Also if you're really into it you will notice things from the Windows Minesweeper like the fact that you never hit a bomb in your first move.

In the realm of Object Oriented programming itself which was the excuse for getting into this project, you can also learn to encapsulate your objects so well as to have the ability to create a new game by instantiating a Minesweeper class. Things like: new Minesweeper(), new Minesweeper('expert'), new Minesweeper(width, height, mineCount). Or even more, generalize your game to add the extra features in this way: SuperMinesweeper extends Minesweeper. Which effectively addresses the whole purpose of programming with objects in mind.

Minesweeper is not the only game that I get to program when I was on my first steps in the world of programming but now that I'm usually writing code for image processing and computer vision I get to remember the first times I was traversing matrices and performing convolutions and recursion calls over rows and columns. I'm including in this post a caption of the game that looks as close as it can get to the one distributed in older Windows versions. The code I wrote back then is still fully working but nowadays you can find lots of minesweeper implementations out there, even in Javascript which needs you to install nothing, you can try this one. Indeed you can find so many variants of it online and from so many places around the world. I, for one, thank all the programmers who wrote the early versions of minesweeper and also Microsoft's decision to include this nice game in his most popular software. This is a game that I believe has truly inspired many people even so far as to somebody proving that Minesweeper is NP-complete! How cool is that?

1 comment:

  1. Very interesting example taken for introduction to the trade!

    ReplyDelete