import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class minesweeper extends javax.swing.JFrame
{
    private static final int boardsize = 10;
    private static final int nummines = 10;
    private final int gridsize = 45;
    private JButton[][] buttons;
    private JLabel scorelabel = new JLabel("0 points");
    private JLabel status = new JLabel("the game is afoot");
    private gridposition[][] thegrid;
    
    private int score = 0;
    private boolean gamegoing = true;
    public minesweeper() {
        super("Swept Away - A Mine Story...");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
        buttons = new JButton[boardsize][boardsize];
        thegrid = new gridposition[boardsize][boardsize];
        setSize(boardsize * gridsize + 60, boardsize * gridsize + 180);
        getContentPane().setLayout(null);
        getContentPane().add(scorelabel);
        scorelabel.setBounds(30,30+gridsize*boardsize,130,30);
        getContentPane().add(status);
        status.setBounds(160,30+gridsize*boardsize,130,30);
        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);
            }
        assignmines();
        setVisible(true);
    }
    public void assignmines() {
        int currentminecount = 0;
        int tryx;
        int tryy;
        Random randy = new Random();
        while (currentminecount < nummines) {
            tryx = randy.nextInt(boardsize);
            tryy = randy.nextInt(boardsize);
            if (!thegrid[tryx][tryy].getmine()) {
                thegrid[tryx][tryy].setmine(true);
                currentminecount++;
            }
        }                        
    }
   
    public static void main(String[] args) {
        new minesweeper();
    }
    public void click(int x, int y) {
        int xmin = (x > 0) ? x - 1 : 0;
        int xmax = (x < thegrid.length - 1) ? x + 1 : thegrid.length - 1;
        int ymin = (y > 0) ? y - 1 : 0;
        int ymax = (y < thegrid[0].length - 1) ? y + 1 : thegrid[0].length - 1;
    thegrid[x][y].settried(true);
        if (thegrid[x][y].getmine()) lose();
        else {
            score++;
            updatescore();
            buttons[x][y].setText(""+gridposition.countmines(thegrid,x,y));
            if (gridposition.countmines(thegrid,x,y) == 0)
            for (int a = xmin; a <= xmax; a++)
                for (int b = ymin; b <= ymax; b++) {
                if (!(a == x && b == y) && (!thegrid[a][b].gettried())) click(a, b);
            }
        }
    }
    public void updatescore() {
        scorelabel.setText(""+score+" points");
        if (boardsize*boardsize - score <= nummines) win();
    }
    public void lose() {
        status.setText("You're finished");
        gamegoing = false;
    }
    public void win() {
        status.setText("You won!");
        gamegoing = false;
    }
    
    private class gridclick implements ActionListener {
        private int xpos = 0;
        private int ypos = 0;
        public gridclick(int x, int y) {
            xpos = x;
            ypos = y;
        }
        public void actionPerformed (ActionEvent ev) {
            if (!thegrid[xpos][ypos].gettried() && gamegoing) {
                //thegrid[xpos][ypos].settried(true);
                click(xpos,ypos);
            }
        }
    }
}