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 slides 71-106 of the 2d Arrays slide presentation and codingbat minesweeper problems 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.
- Make sure all class member variables and methods are labeled appropriately as either
public
orprivate
- If the user clicks on a button that contains a mine, the game ends and all the mines are displayed
- If the user clicks on a button that does not contain a mine, a number appears at that location indicating the number of neighbors that DO contain mines. Recall that each position has at most 8 neighbors. Note that the buttons on the edges have fewer than 8 neighbors. For example, a corner button has only three neighbors.
- If the user clicks on a button and no number appears, then there are no mines neighboring that button. Your program should then recursively keep pressing those neighboring buttons that are not next to a mine.
- The game should end when all the buttons that contain mines have been correctly marked and all other buttons have been clicked (assuming that you have not been blown up by then!)
- Your game should use
Math.random()
to randomly place the mines.
- Work through the minesweeper codingbat problem set
- Fork this repository. As you work through the following steps, make sure that your program runs correctly before going to the next step.
- On line 2, delete the comment and create two integer constants
NUM_ROWS
andNUM_COLS
and initialize them each with the value 20 - Go to line 16, 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 the first two lines in the MSButton constructor (around lines 68 and 69), You should now see a grid of buttons. If you click on the button it should turn white.
- Go back to lines 2 and 3 and reduce the number of rows and columns to 5. This will make testing the program easier. We'll set the rows and columns back to a larger number when we finish the game. Make sure the program still runs correctly with the smaller number of rows and columns.
- Now, got to line 5, and initialize
mines
to be anew
emptyArrayList
of typeMSButton
- Go to line 26 and write the
setMines()
function. It should generate a randomrow
andcol
umn number. Use thecontains()
function to check to see if the button at that random row and col is already inmines
. If it isn't thenadd
it - Uncomment the 3rd and 4th lines of the
MSButton
draw()
function (around lines 98 and 99) 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. - Go to line 59 and finish
public boolean isValid(int row, int col)
which returnstrue
if (row
,col
) is a valid location on the grid andfalse
otherwise. Be sure to use your constants for the number of rows and columns - Go to line 67 and finish
public int countMines(int row, int col)
which counts the bombs in the 8 neighbors--(remember to check to see if the neighboring button is valid first before checking to see if it's a mine) - Now go to the
MSButton
class and finishpublic void mousePressed()
which should:- set
clicked
to true - if
mouseButton
isRIGHT
, setflagged
to its opposite value (If it'strue
set it tofalse
or if it isfalse
set it totrue
) Ifflagged
was set tofalse
also setclicked
tofalse
- else if
mines
containsthis
button, display the losing message - else if
countMines
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
- set
- Next, finish
public boolean isWon()
which determines if the player has won the game - Then finish
public void displayWinMessage()
. One way to display the message is to usesetLabel
to change the labels of the buttons - Finish
public void displayLosingMessage()
to display the positions of all the bombs as well as a losing message - Finally, adjust the number of rows, columns and mines in your game to your desired difficulty level.
Maxwell
Ravi
Robert
Mike
Erica
Zhenwen
Nghi
Kenny
Juan
Darya
Brandon
Kyle
Hannah
Sophie
Emma
Nghi
Mi-Kaela
Andrew
Michelle
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