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.
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
public
,protected
orprivate
- 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.
- Fork and clone down this repository
- On line 4, create two integer constants
NUM_ROWS
andNUM_COLS
and initialize them each with the value 20 - Go to line 18, use the constants to initialize the 2d array
buttons
to have 20 rows and 20 columns - Use nested loops to create a
new MSButton
for 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
bombs
to be anew
emptyArrayList
of typeMSButton
- Go to line 25 and write the
setBombs()
function. It should generate a randomrow
andcol
umn number. Use thecontains()
function to check to see ifbuttons[row][col]
is already inbombs
. If it isn't thenadd
it - 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
MSButton
class and finish these three member methods:public boolean isValid(int row, int col)
returnstrue
if (row
,col
) is a valid location on the grid andfalse
otherwisepublic 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:- sets
click
to true - if
keyPressed
istrue
, togglesmarked
to either eithertrue
orfalse
. Ifmarked
isfalse
setclick
tofalse
- else if
bombs
containsthis
button display the losing message - else if
countBombs
returns a number of neighboring mines greater than zero, set the label to that number - else recursively call
mousePressed
with the valid, unclicked, neighboring buttons in all 8 directions
- sets
- Next, finish
public boolean isWon()
which determines if the player has won the game - Then finish
public void displayWinMessage()
which usessetLabel
to 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
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:
http://www.bitstorm.org/gameoflife/
http://www.math.com/students/wonders/life/life.html
Thien
Melody
Jeremy
Helen
Xiao Qin
Charles
Jacky
Noah
Alex
Matthew
Rebecca
Winnie
Evan
Esther
Raymond
Aliya
Brian
Ka Ki
Alex
Elliott
Theo
Lin
Jimmy
Zachary
Erika
Thomas