In this assignment you will be recreating the Microsoft Minesweeper game. If you are not familiar with minesweeper you should play the game until you are. You can find one version at http://minesweeperonline.com/. You can also try my slightly easier Minesweeper game. You may find the 2d Arrays PowerPoint slides helpful.
Install the Guido GUI Library
We'll be using a GUI library called Guido. To install it, start Processing and choose Sketch | Import Library | Add Library. Type Guido in the search box, the click on Guido by Florian Jenett and then click Install.
- Your minesweeper game will be a two dimensional array of buttons. Use constants for the number of rows and columns.
- The number of bombs should also be specified by a constant.
- Make sure all class member variables and methods are labeled appropriately as either
- If you click on a button that contains a bomb, the game ends and all the bombs are displayed
- If you click on a button that does not contain a bomb, a number appears at that location indicating the number of neighbors that DO contain bombs. Recall that each position has at most 8 neighbors. Note that the buttons on the boundary have fewer than 8 neighbors. For example, a corner has only three neighbors.
- If the user clicks on a button and no number appears, then there are no bombs neighboring that button. Your program should then recursively keep pressing those surrounding buttons that are not next to a bomb.
- The game should end when all the buttons that do not contain bombs have been marked (assuming that you have not been blown up by then!)
- Your game should use
Math.random()to randomly place the mines.
Suggested steps to completing this assignment:
- Fork and clone down this repository
- On line 4, create two integer constants
NUM_COLSand initialize them each with the value 20
- Go to line 18, use the constants to initialize the 2d array
buttonsto have 20 rows and 20 columns
- Use nested loops to create a
new MSButtonfor each row column pair
- Uncomment lines 55 and 56, You should now see a grid of buttons. If you click on the button it should turn white.
- Now, got to line 7, and initialize
bombsto be a
- Go to line 25 and write the
setBombs()function. It should generate a random
column number. Use the
contains()function to check to see if
buttons[row][col]is already in
bombs. If it isn't then
- Uncomment lines 97 and 98 so that cells with a mine turn red when clicked. Test out your program to make sure it has the number of mines you expect.
- Now go to the
MSButtonclass and finish these three member methods:
public boolean isValid(int row, int col)returns
col) is a valid location on the grid and
public int countBombs(int row, int col)counts the bombs in the 8 neighbors--(remember to check to see if the neighboring button is valid before checking to see if it's a mine)
public void mousePressed()which:
markedto either either
- else if
thisbutton display the losing message
- else if
countBombsreturns a number of neighboring mines greater than zero, set the label to that number
- else recursively call
mousePressedwith the valid, unclicked, neighboring buttons in all 8 directions
- Next, finish
public boolean isWon()which determines if the player has won the game
- Then finish
public void displayWinMessage()which uses
setLabelto change the labels of the buttons to display a winning message
- Finally finish
public void displayLosingMessage()to display the positions of all the bombs as well as a losing message
Alternative Assignment: The Game of Life
If you want an alternative to Minesweeper, you could write your own version of the The Game of Life. The Game of Life isn't what you think, it's a program that simulates how bacteria might grow. It's another program that is well suited to using a two dimensional array of buttons. You can find examples and descriptions at the following websites: