Mine Sweeper

Image result for images of sea mines

Specification

Mine Sweeper is a classic strategy game that used to come bundled with the Windows OS.

A player is presented with a view of a mine field consisting of a 10 x 10 array of patches. Exploding mines are hidden under 10% of randomly selected patches.

When a player probes (i.e., clicks) a patch one of two things happen. If the patch contains a mine, the mine explodes and the game ends in defeat. Otherwise, the patch reveals the number of neighboring patches that contain mines. Clicking on a patch while holding down the Shift key marks the patch as possibly containing a mine. The patch turns red and is marked with an "X".

In the following game the user has probed 40 patches in the top of the mine field. The player has correctly deduced that the patch in row 0, column 8 must be mined.

The Edit menu contains a single item: "Remaining". Selecting this item displays the number of unmined or "safe" patches remaining

Probing a mined patch ends the game in a loss.

Probing after the end of the game is prohibited:

Probing the last unmined patch ends the game in a victory.

The Help menu contains 2 items: About and Contents. Both display dialog boxes:

In addition to the MVC File and Help use cases, here are the additional use cases for Mine Sweeper:

Notes

·       Probing a patch could throw an exception if the patch is mined or if it's the last unmined patch in the minefield. In these cases an exception is thrown. Currently, the exception handler displays one of the dialog boxes shown above. In the future we might want some other behavior. This is one of those rare examples where the <<extend>> arrow can be used.

Design

Mine Sweeper is a customization of the MVC framework.

 

Notes

·       The MineField class extends Model and stores the application data (a 2-dimensional array of patches) and the application logic-- the probe method), which returns -1 if the patch in the specified row and column is mined, and the number of mined neighbors, otherwise.

·       An unmined patch has between 3, 5, or 8 neighbors depending on if it is in the corner, edge, or center of the mine field.

·       There is no View extension in this customization.

·       The MineFieldPanel class contains a 2-dimensioal array of buttons corresponding to the 2-dimensional array of patches in the associated mine field. Each button has a button controller that is automatically executed when the button is clicked. The button controller probes the corresponding patch and either ends the game if the probed patch is mined or is the last unmined patch. Otherwise it changes the button text to reveal the number of mined neighbors.

·       The ButtonController class can be nested inside of the MineFieldPanel class to simplify access to the model.

·       Not shown are the RemainingCommand extension of the mvc.Command class and the MineFieldFactory implementation of the AppFactory interface.

Implementation

Here's how the ButtonController can tell if the shift key was down when its button was clicked:

public void actionPerformed(ActionEvent ae) {
  MineField field = (MineField)model;
  if (field.gameOver()) {
     Utilities.error("Sorry, game over, restart for a new game");
  } else if ((ae.getModifiers() & ActionEvent.SHIFT_MASK) > 0) {
     JButton button = (JButton)ae.getSource();
     // turn button red and mark with "X"
  } else {
     // if mined or last unmined, end the game
     // change button label
  }
}

References

MineSweeper.html

Java Foundation Classes (lectures on swing and awt)

Model-View-Controller Framework