Homework 4
comp 125-609, Goldwasser
Due: 6:00pm Tuesday, October 12, 1999 (worth 5 points)
Late Option: if submitted between 6:00pm October 12 and 6:00pm October 26, you can recieve up to 3 of 5 points. No late homeworks will be accepted after 6:00pm October 26.
Purpose: Using two-dimensional arrays
Overview: Other than Visual Basic, nothing reflects the inherent nature and history of the Windows system as much as the minesweep game! This week, we will design a program which can be the basis to the minesweep game.

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).


We are providing you an almost complete program this week (see mine.vbp and mine.frm). However your task will be to write the part of the program which takes the two-dimensional array of mine locations and creates a separate two-dimensional array of integers specifying for each square how many of its neighbors have mines.

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.


Advice: Watch out for special cases, especially near the edges or corners of the game.

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).


Using the program: Here is a brief user's guide to the interface. The user specifies four integers:
  • rows - the number of rows in the puzzle
  • columns - the number of columns in the puzzle
  • mines - the number of mines to place in the puzzle
    (make sure it is less than rows x columns)
  • GameID - Picking a different GameID will get you a different placement of mines. Picking the same GameID gets you the same placement of mines each time.
  • Once the parameters are chosen, there are two buttons the user can choose between. The one labeled "Test Code" will simply display the entire (uncovered) map of the board game. There is no game to play, but this is a way to see how your procedure labeled all of the squares.

    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.


    Extra Credit (1 point): Another helpful feature of the original minesweep game is that if you uncover a spot which has zero mines as neighbors, then the game automatically does you the favor of uncovering all of the neighbors (since it is clear those are all safe spots to uncover). In fact, while doing this, if another square is uncovered which has zero neighboring mines, this process continues.

    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)


    To submit your homework you should,
  • Print out your project as follows. Click "File". Click "Print". In the Range box, select "Current project". In the Print What box, click only on the "Code" choice.
  • Save both the form and the project to a floppy disk
  • Place the printouts and the floppy disk inside a large manilla envelope. Please make sure that your name appears on the envelope as well as the disk and the printouts.