CPSC 170 Post Lab 2
Mine Sweeper
Due Before Class on Friday, January 22nd


For this assignment you will create the classic time-wasting game Mine Sweeper. For those that do not know the game Mine Sweeper is a game where players use spatial logic to isolate cells on a grid that hold hidden bombs. The player is initially faced with a rectangular collection of blank cells. Upon selecting a square, the game reveals either a bomb or the number of neighboring cells that contain bombs. Play continues until a bomb is selected (the player loses), or until the number of remaining cells equals the number of bombs (the player wins). For an example of the game run Applications>Games>Mines in Linux.


The user interface of the game should use a grid layout manager to display a two dimensional array of buttons. In order to create buttons that behave like they should in the game (the buttons change their icon when they are pressed and can only be pressed once) you should create a new class that extends JButton called MineSweeperButton. The class should have instance variables for whether the button is a bomb, and for how many of its neighbors are bombs. You can use the method setIcon to set the icon the button displays before it is pressed (this image should be the same for every button), and the method setDisabledIcon to set the icon the button displays after it is pressed (this image will depend on if the button is a bomb or how many of its neighbors are bombs). You should also use the setBorderPainted to remove the operating system dependent imagery from around the icon. Finally, to get the image of the button to change when it is pressed set the button's state to disabled using setEnabled in the actionPerformed method. The tar file MineSweeperIcons.tgz contains images that you can use for the buttons.

The constructor of the game's panel should initialize the array of MineSweeperButtons to contain a random (but small) number of buttons that are bombs. It should also make sure that each button has the correct number stored for the number of neighbors that are bombs. Every time a button is pressed the program should check if the game is over. If the game is over the program should display whether the player has won or lost.

Submission: Tar and submit your code on the course blackboard site.