In undergrad, I worked in my university’s media company. They made everything: the website, the school newspaper, and the radio. The entire operation was (and probably still is) run exclusively on Macs, so I became a convert. To me, there really isn’t too much difference between Mac and PC because the majority of programs can be found on either platform. All you need do is get used to how it drives, and I just found myself drawn to the organizational capability of spaces.

However, there is one program which I have yet to find its Mac equivalent: Minesweeper. It’s one of those things that you don’t realize you miss until it’s gone. And so, in an effort to control my need to click on squares, I threw together my newest MATLAB game.

This is just a first cut, so I won’t be posting any code yet. Like I said, I just threw this together; mostly during lunch breaks this past week. Since it’s unfinished, I’ll just talk about some of the stumbling blocks I’ve hit.

Creating Mines

So the first hurdle was randomly distributing mines and then writing the solution array, that is, writing the little numbers that indicate how many mines are nearby. Once it clicked, I was pretty pleased with how elegant it turned out to be.

To start, you have a grid, let’s just say it’s 4×4 for simplicity.

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Next, let’s randomly put in two mines, denoted by 1.

mine_mask =
0 0 0 0
0 0 0 0
0 1 1 0
0 0 0 0

We’ll call this array our mine_mask. So now what? For each mine, we have to increment the adjacent cells by one. There’s a pretty slick way to do this. All it takes is convolution. By convolving mine_mask by the kernel [1 1 1; 1 1 1; 1 1 1] we can quickly write in the numeric values around each mine. Using our 4×4 example, we get the following

0 0 0 0
1 2 2 1
1 2 2 1
1 2 2 1

Now, all we need to do is use mine_mask one more time to write the mines into our solution. My personal convention is to call each mine 9.

solution =
0 0 0 0
1 2 2 1
1 9 9 1
1 2 2 1

Opening Multiple Squares

So far, opening multiple squares has been the most tricky thing to do. I thought about using mathematical morphology to do it, but that requires the image processing toolbox. So for compatibility’s sake, I threw out that idea.

In the end, I just used recursion to open up multiple squares. I’m terrible at recursion, so it was a gigantic pain in the butt, but I think I got it working. Suffice to say that recursively looking in eight directions without infinitely looping was tricky.

What’s Left?

There’s still a lot left to be done. There isn’t any “mine flag” functionality yet, and I’m still debating how to implement it.

I also need to figure out a real-time counter to show the player how long they’re taking. To that end, it also needs a “number of remaining mines” counter, which will be tied to the flagging functionality.

It’s also a little slow… this is the part that’s going to bother me the most. Before I continue, I really want to be able to click around and uncover mines in real-time. It could just be a limitation in MATLAB, but I’ll be trying to optimize the speed before doing anything else.

So there it is! Or at least what I’ve done so far… but it works!

Advertisements