Java Reference
In-Depth Information
currentCount++;
if (currentCount == mines) {
return;
}
}
}
}
}
if (currentCount < mines) {
populate(currentCount);
}
}
So why two populate methods? The first one is a convenience method that saves the trouble of
providing an argument. In this case, it's not all that convenient, because we call only the convenience
method once. You can rightfully say that we should have used just one method. However, we use this
pattern so often that it's essentially a habit. More importantly, it gives us a chance to talk about this
useful pattern.
So, again, why two methods? It's a useful technique in a number of situations. If you're exposing
methods to other objects (meaning the methods are not private), multiple methods of the same name let
developers (including you) call the same method with different parameters. Then each of those methods
calls some other method that applies the parameters. In other cases, the simpler method gets used
multiple times to make coding a bit easier (by not having to specify parameters that rarely change every
time).
Now let's talk about what the two populate methods do. The simple populate method calls the more
complex populate method with an argument (which is the value needed to start the process of
populating the minefield). That initial value is the number of mines that have been placed so far. That's
why the initial argument is 0.
The complex version of the populate method keeps a count (the currentCount variable) of the mines
placed so far. To figure out whether any given spot has a mine, the method uses the random number
generator to compare against the chance of a mine being in any one spot. That chance is the number of
possible spots divided by the number of mines.
If we happen to get to the required number of mines before going through the whole array, we can
stop. The if statement in the middle of the method accomplishes that goal. On the other hand, if we go
through the whole array and don't get to the required number of mines, we call the populate method
again. Let me repeat that bit: We call the method we're in again. That's called recursion, and I dedicate a
whole chapter to that relatively advanced topic later in the topic. Recursion. is one of my favorite
programming techniques, and you can do some interesting things with it. For now, though, let's finish a
MineSweeper game.
If we do have to re-enter the method, the second pass will go through the array again until we finally
have the required number of mines. In theory, this process could require more than two passes. In
testing, though, no more than two passes were ever needed, and the first pass never produced fewer
than seven mines. This algorithm gives a slight weighting to the top left corner of the minefield, but I
think that's an acceptable problem.
So, the populate methods get a little tricky, but they do solve the problem of how to randomly
populate a minefield. Let's press on to other methods.
The next two methods, getMinesFound and getMinesRemaining , return the values of fields. So let's
skip describing them in any further detail. The method after those two, though, is another complex bit of
code that requires some explanation. Listing 7-17 shows the cascade method.
Search WWH ::




Custom Search