These are projects posted by the students of Dr. Gove Allen at Brigham Young University. These students have taken one semester-long course on VBA and generally have had no prior programming experience

Thursday, December 12, 2013

Minesweeper (Merrick Johnson)

Executive Summary - (Merrick Johnson)

Description of the business and application

Merrick, Inc. is a software company (imagined for the purpose of this project) whose employees routinely perform tasks that are essential and must be performed correctly, but are somewhat tedious. The managers of Merrick, Inc. have noticed that after long periods of time performing these tedious tasks, more mistakes are made as the employees become bored and sleepy. To promote a more interesting and productive work environment, the managers want to create a program that will break the monotony but also emphasize attention to detail and the negative results of mistakes.

Minesweeper is an obvious choice to fill this need, as it requires attention to detail, but is still strangely entertaining, and subtly teaches the consequences of mistakes (an explosive going off). Minesweeper is also an excellent choice to solve this problem because it is a relatively simple game to learn and only lasts a few minutes. However, due to possible copyright and licensing issues, the company must program the game in Excel itself.

Overview of the Minesweeper functionality

When the ‘New Game’ button is clicked, a subroutine executes which does everything necessary to prepare a minesweeper game for the user.  A 16 x 30 grid is created on the Excel spreadsheet, and mines are randomly assigned to an average of one-seventh of the squares in the grid. The font color and background color of the cells in the grid are then changed so that the user knows which cells have not been selected, and to hide the value assigned to the square. A total is also kept of how many mines there are, as well as how many squares the user has marked as mines. The objective of Minesweeper is to correctly detect and mark all of the mines without making a mistake and blowing up.

To play, the user selects cells in the grid. By selecting a cell, the user is guessing that the cell does not contain a mine. If the cell selected is a mine, then the game is lost and a message box is displayed to this effect. If the cell selected is not a mine, then a number is displayed in that cell indicating how many of the eight adjacent cells contain mines. If this number is zero, then this process is automatically repeated for all of the contiguous cells that do not contain mines (saving time for the user).

A game of Minesweeper in progress

If the user suspects that a certain cell contains a mine, then this cell should be marked in red to show that it is suspected to be a mine. To mark cells in red, the user first clicks the ‘Toggle Mode’ button. When this button is clicked, the background color of the spreadsheet changes to red to show that the user is currently marking mines, instead of selecting cells that are not mines. While the background is red, any cells selected will be marked as mines.  This effect lasts until the ‘Toggle Mode’ button is clicked again, at which point the user again selects cells that do not contain mines.

When ‘Toggle Mode’ is clicked, the red background reminds the user that he is currently marking mines

If the user correctly marks all of the mines in the minefield, then a congratulatory message box is displayed, and the game is over. In a normal game of minesweeper, the game would also end automatically upon clicking a cell that contains a mine. However, one of the frustrations of minesweeper is that occasionally the user has to guess. To solve this problem and to give my implementation of minesweeper a more personal touch, I decided to let the user continue playing after messing up (if they want to). This way, if a random guess (such as at the beginning of the game) results in a mine, the user does not have to start over, but can pretend that they guessed right.

Implementation Documentation
  • Two-dimensional arrays are used to keep track of which squares contain mines, as well as to keep track of which cells have already been selected and checked for mines, which prevents infinite loops from occurring.
  •  Nested loops are used frequently to iterate through the grid and the arrays in order to perform various tasks.
  • Nested conditional statements are used extensively in order to make sure that subroutines are only called for cells in the grid rather than the entire worksheet, and to make sure that the event handler only fires when cells in the grid are selected.
  • Message boxes are used to notify the user when they win or lose the game.
  • Properties of cells are manipulated constantly – when a cell is selected or marked, the background color is changed to either hide or display the number associated with the cell.
  •  Event handling, specifically the SelectionChange event, is used. Every time the user selects a new cell, the event handler calls the subroutines necessary to check for mines, display numbers, etc.
  • Multiple subroutines and functions are used to divide the code for the game into manageable sections and to return necessary values when needed.
  •  Module-level variables are used to allow all subroutines and functions to easily access the values they need, rather than having to pass a long list of parameters to every subroutine.
  • Recursion is used when a selected cell is not next to any mines. When this happens, the subroutine that checks for mines calls itself for every cell adjacent to it so that the user doesn’t have to.
  • Custom Ribbons are available if the user desires to use them, although they are unnecessary since the buttons on the spreadsheet perform the same functions and are more conveniently located.

What I Learned / Difficulties Encountered

In completing this project I learned several things. The most important thing I learned was how to apply the principles that we learned in class to a new, more complicated problem, and also how to use multiple principles together. For example, I used conditional statements within an event handler to only allow the event handler to perform the relevant operations under certain conditions, rather than every time the event fired.

I learned that it is important to write larger programs in small, testable portions so that debugging is easier. I also learned the importance of carefully planning how the index variables of loops work so that infinite loops can be avoided.

One difficulty I ran into was choosing which event handlers to use. I was hoping to use a Click event, but as far as I could tell, there is no Click event handler; there are Right-Click and Double-Click events instead, neither of which would be convenient enough to use as the main action the user performs. After some searching I found that the SelectionChange event, which fires every time the selection on the worksheet is changed, and I decided that that was as close to a Click event as I could get.

The only feature I wanted to include but was unable to do so, was that I wanted an explosion sound file to be played when the user selects a mine and loses the game. I was able to find a way to do so by searching online, but I decided that implementing this feature would be impractical, since it would require that the user have the same sound file as me, which I cannot guarantee easily.


I did not receive substantial help from anyone on this project. 

1 comment:

  1. It is really nice to visit your article. I could get lot more knowledge from your article.. thanks for sharing.
    how to play minesweeper


Blog Archive