Pass 1 of Design

I started this process by reading over the specification several times. I took a look at the spec, along with what experience I had with minesweeper, and tried to think of some classes that I might use. I started with the normal "find the nouns" approach. Here's what I came up with: Location and Square seem to be the same thing, with different names. It seems like a Square could be in three different states: Flagged, Covered, or Uncovered. A Square could also contain either a Mine, or a number of adjacent mines. I'll have to think about exactly how to represent those.

I'll also think about objects to use by taking a look at the GUI. The board looks to be comprised of a grid of squares. A grid is generally represented by a 2-D array. An array of what? Hmmmm, I'll get back to that. I am also going to have some buttons which specify which mode I'm in. Which reminds me, I'll have to come up with some way of representing what the current mode is. There are also buttons to start up one of three levels. A level seems to be the size of the board.

So, now I know I'll need buttons (both push and toggle) to deal with the user interaction. There will also be some containing classes. The number of classes seem to be growing quite a bit.

So, now that I have some classes to think about, I'll focus on a few important ones in depth. I'll start with Square, since it seems that it is an important class. What do I know about a Square? Well, each square can be a mine, or not be a mine. Each location has one of the two. If it's not a mine, then it has a number associated with it, which represents the number of surrounding mines. Also, when you click on a square, it gets uncovered. Well actually, that's not quite true. When you click on it, its behavior varies depending on what mode the user is in.

So, how should I represent this? I'll probably be using either inheritance or containment (or both.) So when should I use what? Well, inheritance defines what class you are, so is something about a class that doesn't change (an instance can't change what class it is.) So it can't model things that change. Well, whether a Square is a mine or a non-mine doesn't change. So let's try using inheritance there. I'll have a Square superclass, and then two subclasses: Mine and EmptySquare. What will be different about them? Well, the EmptySquare will keep track of how many neighbors it has. Also, clicking on them causes different responses to happen. So, Square will have at least one abstract method that gets overridden.

Should the covered/uncovered/flagged distinction be modeled with inheritance? No. These are attributes which can change, so the Square should contain some sort of state. Maybe I'll use the State pattern...

So, a Square has to deal with getting clicked on. Well, any GP.Graphics.Shape has a React() method. I'll make Square a subclass of some GP.Graphics.Shape so I'll have that functionality, probably GP.Graphics.FilledRectangle. So I'll just have the React() method do... hmmm... I guess what it should do varies depending on what mode the user is in. How to model the mode? Well, this is very similar to the case of Swarm, in which there were different modes for clicking on shapes. So, I can try using a Holder or Proxy pattern to do this.

So, let's say I'm going to have a Holder class. What is it going to hold on to? It has to be something that represents what mode the user is in. So let's say there's a class representing Uncover, Flag, and Check. The difference will be what happens when a square is clicked on. So, if a square is clicked on, and Flag is the current mode, then that square is (un)flagged.

So, this leads me to two conclusions. The Square class must have methods for Flag, Uncover, and Check. It will also have a reference to this holder, and it will get the current state from the holder. The state should have some method like clickMe() which is redefined in the subclasses to call the appropiate method on the Square. The Square will probably be passed as a parameter to that method.

So, I'm looking good so far.

So, the Board seems to be an important class. I decided it should have a 2-D array, since there's a grid. So, it seems to make sense that it would be a 2-D array of Squares. At each location, there could actually be a Mine or an EmptySquare there. The Mines should be randomly distributed around the board, to make for a good game. And there will be an EmptySquare wherever there isn't a Mine. The Board should probably be constructed with a size, so that way I can easily vary how big it should be.

Well, it looks like I have a bunch of classes defined. Let's try to write some of this up, so everything seems to be feasible. I'll just put some empty method stubs that print out information if I run into methods I haven't figured out yet (such as Uncover, or Flag). Once that seems to be working, I'll fill in what those methods do.

My first OMT diagram

Last modified: Wed Mar 11 03:19:01 EST 1998