In this lab, you'll write a puzzle program based on Minesweeper, using Java's built-in Swing libraries. You can access a version under the Games submenu of the K-menu. Our version will be considerably simpler; Figure 8.1 contains a screen shot.
This program has a 10x10 grid of buttons, hiding 20 randomly placed mines. Initially all are light gray, containing the label ``??''. If you click on a button hiding a mine, the button turns red and displays the new label ``B''. If you click on a button that has no mine beneath it, then the button turns green and displays the number of adjacent buttons that hide mines. (Adjacent buttons include buttons that share only a corner.)
The goal is to click on all the button that hide mines, without clicking on any that hide mines. Figure 8.1 illustrates a game where the user has won. The assigned program does not detect whether the game has been won or lost.
The File menu of the program contains a New option for resetting the board, and a Quit option for closing the program. Closing the window closes the program also.
MineButton, a subclass of JButton, represents a single button in the grid.
MineCamp, which extends JFrame, represents the application window. It primary job is to hold and initialize the 100 MineButtons, although it also must handle closing the program when requested and recompute the mines when appropriate.
The class should define the constants WIDTH, HEIGHT, and NUM_MINES to represent the width of the grid, the height of the grid, and the number of buttons hiding mines.
To hold the MineButtons, the constructor should use an instance variable that holds a two-dimensional array of MineButtons. I named this instance variable mines.
The constructor should fill this array with buttons, and then it should call a method reset() to initialize the buttons to a new board. (You want to define a separate method for this, so that you can use the same code when the user selects New from the File menu also.) The constructor should also place the buttons into the window, as well as set up the menu.
The following code is useful in the reset() method for initializing the MineButtons so that exactly NUM_MINES buttons hide mines.
After the above, reset() also needs to compute the counts for each MineButton.for(int i = 0; i < WIDTH; i++) { for(int j = 0; j < HEIGHT; j++) { mines[i][j].reset(); } } for(int k = 0; k < NUM_MINES; k++) { int i, j; do { i = (int) (WIDTH * Math.random()); j = (int) (HEIGHT * Math.random()); } while(mines[i][j].isMined()); mines[i][j].setMined(); }
The MineCamp class will define the main() method, which should simply create a new MineCamp object.