import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class minesweeper extends javax.swing.JFrame
{
// this final variable defines the # of elements in
the grid in each direction (the total # of elements is this number ^ 2)
private static final int boardsize = 10;
// this final variable defines the number of grid
positions on the board that have a mine on them
private static final int nummines = 10;
// this final variable defines the size of the
buttons on the screen for the grid positions
// (< 45 makes it so numbers don't show up well)
private final int gridsize = 45;
// this is the 2 dimensional array of the buttons
for the board
private JButton[][] buttons;
// this is a label that tells us how many points the
user has scored
private JLabel scorelabel = new JLabel("0 points");
// this is a label that tells us if the game is
going or not
private JLabel status = new JLabel("the game is
afoot");
// this is the 2-dimensional array of gridposition
elements to track information about the grid
private gridposition[][] thegrid;
// this is the instance variable to track the
current score (start off at 0)
private int score = 0;
// this is the instance variable that tells us if
the game is going or not
private boolean gamegoing = true;
public minesweeper() {
// change the line below to something better!
super("Swept Away - A Mine
Story...");
// this is so Java will close our program if we
close the window (should be default, but isn't)
setDefaultCloseOperation(EXIT_ON_CLOSE);
// this creates the 2-d arrays as having
boardsize*boardsize elements
buttons = new
JButton[boardsize][boardsize];
thegrid = new
gridposition[boardsize][boardsize];
// this sets the size of the window in x, y terms
setSize(boardsize * gridsize
+ 60, boardsize * gridsize + 180);
// this lets us place items on the window as we want
to
getContentPane().setLayout(null);
// this puts our scorelabel on the window
getContentPane().add(scorelabel);
// this has us place it where we want it
scorelabel.setBounds(30,30+gridsize*boardsize,130,30);
// this puts our status lable on the window
getContentPane().add(status);
// this has us place it where we want it
status.setBounds(160,30+gridsize*boardsize,130,30);
// this nested for loop has us create the neccesary
button objects, the neccesary gridposition
// objects, set the needed values for the
gridposition objects (telling them what their x and
// y values are, etc) and has us assign an
actionListener for the buttons passing in the x and
// y values for the gridclick objects so they know
what x and y they belong to
for (int a = 0; a <
boardsize; a++)
for
(int b = 0; b < boardsize; b++) {
buttons[a][b] = new JButton("");
getContentPane().add(buttons[a][b]);
buttons[a][b].setBounds(30+gridsize*a,30+gridsize*b,gridsize,gridsize);
buttons[a][b].addActionListener(new gridclick(a,b));
thegrid[a][b] = new gridposition();
thegrid[a][b].setx(a);
thegrid[a][b].sety(b);
thegrid[a][b].settried(false);
thegrid[a][b].setmine(false);
}
// this calls the method you need to write that
secretly places mines on the grid
assignmines();
// this makes it so we can see the window (sorta
important)
setVisible(true);
}
// the following method which you need to implement
will place nummines (a final variable
// defined above) mines on the board in random
locations. Use the methods in the gridposition
// class to set and check whether or not a grid
position has a mine or not. Also make sure
// that when you are picking locations that you do
not double-count positions that already have
// mines
// This is worth 4 points
public void assignmines() {
}
// a small, but neccesary main method to start up
the GUI
public static void main(String[] args) {
new minesweeper();
}
// the following method needs to be
implemented. This method takes in an x and y value and defines
// what should happen when the user clicks
there. First of all, make sure you tell the gridposition
// object that the user has now clicked here, then
find out if a mine exists here. If so, call the lose()
// method. Otherwise, bump up the score
display the new score, and put text on this button that tells
// the user how many grid positions surrounding it
contain mines.
// (there should be a method in the gridposition
class to tell you how many that you have to write)
// For extra credit (2 points) if no positions
surrounding this one contain mines, go to the surrounding
// positions and act as if you clicked on
them. This needs to be recursive, and should make sure that it
does
// not exceed array boundaries. Do not work on
this extension until you are done with the rest of the code.
// the online version has this extension implemented.
// Worth 4 points for the basic implementation of
this method (without recursion)
public void click(int x, int y) {
}
// a method to update the score that you have to
write. First set the score's label witht he number of points
// and then find out if you have won. If the
boardsize squares - the score is the same as the number of mines
// that means you have successfully clicked on all
grid positions you can without clicking on a mine!, so call
// win()
// Worth 2.5 points
public void updatescore() {
}
// this method needs to update the status text to
explain that you lost, and then change the instance variable
// that states that the game is going, and make it
false
// Worth 2.5 points
public void lose() {
}
// this method needs to update the status text to
explain that you won, and then change the instance variable
// that states that the game is going, and make it
false
// Worth 2.5 points
public void win() {
}
// this class defines what happens when the user
clicks on a button
private class gridclick implements ActionListener {
// the following instance variables state what x and
y position this
// clicker is controlling
private int xpos = 0;
private int ypos = 0;
// this constructor allows us to find out what
position we are controlling
public gridclick(int x, int
y) {
xpos
= x;
ypos
= y;
}
// this method (which you must define) will call
click on its x position and y
// position if the associate gridposition has NOT
been clicked AND the game is
// going. Note: since this is in a nested
class, you have access to all of the
// methods and variables defined for the entire
minesweeper class
// Worth 2.5 points
public void actionPerformed
(ActionEvent ev) {
}
}
}