The game works as follows. There is a two-dimensional grid of squares which the user sees. Hidden under some of those squares are landmines. If you step (i.e. click) on a square with a mine, the game is over. However, if you step on a square which does not have a mine, that square gets uncovered, and by standing there, you are able to detect how many mines are in the directly neighboring sqaures (either vertically, horizontally, or diagonally). Specifically, when you clear a safe square, that square gets labeled with a number between 0 and 8, specifying the number of the immediate neighbors which have mines. (okay, if there are zero such neighbors, the square is shown to be blank).
More specifically, there is a subroutine in the code which appears as
Private Sub countNeighbors()
End Sub
Your job will be to write this routine. (did you know that Visual
Basic allows you to define subroutines like this? This is just a
preview. We will see much more about this in weeks to come).
All of the information you need will already be stored in the
following global variables:
Dim numrow As Integer
Dim numcol As Integer
Dim nummine As Integer
Dim mines() As Boolean
Dim neighbors() As Integer
Where mines() and neighbors will already be set as
two dimensional arrays as follows:
ReDim mines(1 To numcol, 1 To numrow)
ReDim neighbors(1 To numcol, 1 To numrow)
The mines() array will be set to true for each
location which contains a mine and will be set to false for
the others. You should not modify this array; just view it.
The neighbors() array will be sized properly, but all of the values should be considered as junk. It is your job to write the correct value into each and every one of these locations.
That's all there is to it. If you can write this piece of the program, everything else should run beautifully.
Also, you should test your program on several different board sizes, and verify by hand that you are labelling the squares properly.
If you want to compare your program to an answer key, you are welcome to download my complete solution (mine.exe).
If you instead want to play the game, click the "Play Game" button. The screen will be setup so that all of the squares are covered. If you click with the left mouse button, this will dig to see what is at that square. If it is a mine, the game will be over, and the entire board is displayed. If it is not a mine, then the spot will be cleared, and you will see the number of neighbors with mines displayed.
As an additional feature, if you think you know where a mine is, you may "mark" the spot with an X by clicking the right mouse button. That square will get marked with an X whether it truly is a mine or not. To unmark it (if you change your mind), just right click on it again. An added advantage to marking potential mines is that if you (accidentally) left click on a marked spot, the game will not dig there.
To create a new board, the user may always choose to change the parameters and click one of the start buttons again.
For extra credit, change the overall program to have this feature. Note this is a huge extra credit problem because it will force you to read and understand a good deal of the program code we provided. (which you did not have to read at all to do the rest of the homework problem)