import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class th implements Runnable{
Container c;
JLabel time,con;
JPanel menu,ground;
JButton btn;
int h,m,s,ms;
public th() {}
public void get(JLabel time,Container c,JPanel menu,JPanel ground,JButton b,JLabel con) {
this.time=time;this.c=c;this.menu=menu;this.ground=ground;btn=b;this.con=con;
menu.setLocation(0,0);
}
public void reset() {
ms=0;s=0;m=0;h=0;
}
public void run() {
int width,height;
while(true) {
width=c.getWidth();
height=c.getHeight();
c.setFocusable(true);
c.requestFocus();
menu.setSize(width,height/15);
btn.setLocation(width/2-height/30, 0);
btn.setSize(height/15, height/15);
time.setLocation(width-80, 0);
time.setSize(80, height/15);
ground.setSize(width,(height/15)*14);
ground.setLocation(0, height/15);
con.setSize(80, height/15);
con.setLocation(10, 0);
ms++;
if(ms==1000) {ms=0;s++;}
if(s==60) {s=0;m++;}
if(m==60) {m=0;h++;}
time.setText(Integer.toString(h)+":"+Integer.toString(m)+":"+Integer.toString(s));
try {Thread.sleep(1);}
catch (InterruptedException e) {e.printStackTrace();}
}
}
}
public class Main extends JFrame{
static final int Height=18,Width=30,Mine=0;
JButton[][] btn=new JButton[Height][Width];
static Block[][] b = new Block[Height][Width];
static Fun f=new Fun(Height,Width,Mine,b);
static boolean fg=false;
JLabel con=new JLabel();
ImageIcon flag=new ImageIcon("images/flag.png");
ImageIcon digging=new ImageIcon("images/digging.png");
th t=new th();
public static void main(String[] args) {
new Main();
}
class start implements ActionListener{public void actionPerformed(ActionEvent e) {f.set();t.reset();}}
class flag extends KeyAdapter{public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='f'){
if(fg) {fg=false;con.setIcon(digging);}
else {fg=true;con.setIcon(flag);}
}
}
}
public static void mine() {f.mine();}
public static boolean getf() {return fg;}
public static void dig(int x,int y) {f.dig(x,y);}
public Main() {
con.setIcon(digging);
ImageIcon normall=new ImageIcon("images/normall.png");
JButton restart=new JButton(normall);
restart.setDisabledIcon(normall);
restart.addActionListener(new start());
f.get(restart);
for(int i=0;i<Height;i++) {
for(int j=0;j<Width;j++) {
btn[i][j]=new JButton();
b[i][j]=new Block(btn[i][j],normall,i,j);
}
}
setSize(900,700);
setVisible(true);
Container c=getContentPane();
c.addKeyListener(new flag());
c.setFocusable(true);
c.requestFocus();
JPanel menu = new JPanel();
JPanel ground = new JPanel();
ground.setLayout(new GridLayout(Height,Width));
c.setLayout(null);
c.add(ground);
c.add(menu);
for(int i=0;i<Height;i++) {
for(int j=0;j<Width;j++) {
ground.add(btn[i][j]);
}
}
JLabel time=new JLabel();
t.get(time,c,menu,ground,restart,con);
Thread th=new Thread(t);
th.start();
menu.add(restart);
menu.add(time);
menu.add(con);
f.set();
}
}
class Block{
int cnt=0,x,y;
boolean mine=false,f=false;
JButton btn;
ImageIcon normall;
ImageIcon flag=new ImageIcon("images/flag.png");
public Block(JButton btn,ImageIcon normall,int x,int y) {
this.btn=btn;
this.btn.addActionListener((ActionListener) new ac());
this.normall=normall;
this.x=x;
this.y=y;
}
class ac implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(Main.getf()&&!Fun.getd()) {
if(f) {f=false;btn.setText("?");btn.setIcon(null);}
else {f=true;btn.setText("");btn.setIcon(flag);}
}
else if(!f) {
if(mine) Main.mine();
else {
Main.dig(x,y);
}
}
}
}
}
class Fun{
Color[] clr = {Color.white, Color.cyan, Color.green,Color.red,Color.blue,Color.magenta,Color.yellow,Color.black,Color.lightGray};
int h,w,m,d;
Block[][] b;
JButton restart;
boolean[][] dig;
static boolean die;
ImageIcon normall=new ImageIcon("images/normall.png");
ImageIcon clear=new ImageIcon("images/clear.png");
ImageIcon fail=new ImageIcon("images/fail.png");
public Fun(int height, int width, int mine, Block[][] b) {
h=height;
w=width;
m=mine;
this.b=b;
dig=new boolean[h][w];
}
static boolean getd() {return die;}
void get(JButton b) {restart=b;}
void dig(int x,int y) {
if(!die) {
b[x][y].btn.setBackground(clr[b[x][y].cnt]);
b[x][y].btn.setText(Integer.toString(b[x][y].cnt));
d++;
if(d==(h*w)-m) {restart.setIcon(clear);}
b[x][y].btn.setIcon(null);
if(b[x][y].cnt==0) {
if(x!=0) {
if(dig[x-1][y]) {dig[x-1][y]=false;this.dig(x-1, y);}
if(y!=0&&dig[x-1][y-1]) {dig[x-1][y-1]=false;this.dig(x-1,y-1);}
if(y!=w-1&&dig[x-1][y+1]) {dig[x-1][y+1]=false;this.dig(x-1,y+1);}
}
if(x!=h-1) {
if(dig[x+1][y]) {dig[x+1][y]=false;this.dig(x+1,y);}
if(y!=0&&dig[x+1][y-1]) {dig[x+1][y-1]=false;this.dig(x+1,y-1);}
if(y!=w-1&&dig[x+1][y+1]) {dig[x+1][y+1]=false;this.dig(x+1,y+1);}
}
if(y!=0&&dig[x][y-1]) {dig[x][y-1]=false;this.dig(x,y-1);}
if(y!=w-1&&dig[x][y+1]) {dig[x][y+1]=false;this.dig(x,y+1);}
}
}
}
void set() {
die=false;
d=0;
restart.setIcon(normall);
int i,j;
int rx,ry;
for(i=0;i<h;i++) {
for(j=0;j<w;j++) {
b[i][j].btn.setText("?");
b[i][j].btn.setIcon(null);
b[i][j].btn.setBackground(Color.darkGray);
b[i][j].cnt=0;
b[i][j].mine=false;
b[i][j].f=false;
dig[i][j]=true;
}
}
for(i=0;i<m;i++) {
rx=(int)(Math.random()*h);
ry=(int)(Math.random()*w);
if(b[rx][ry].mine) i--;
else {
b[rx][ry].mine=true;
if(rx!=0) {
b[rx-1][ry].cnt++;
if(ry!=0)b[rx-1][ry-1].cnt++;
if(ry!=w-1)b[rx-1][ry+1].cnt++;
}
if(rx!=h-1) {
b[rx+1][ry].cnt++;
if(ry!=0)b[rx+1][ry-1].cnt++;
if(ry!=w-1)b[rx+1][ry+1].cnt++;
}
if(ry!=0)b[rx][ry-1].cnt++;
if(ry!=w-1)b[rx][ry+1].cnt++;
}
}
}
void mine() {
die=true;
restart.setDisabledIcon(fail);
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
if(b[i][j].mine) {
restart.setIcon(fail);
b[i][j].btn.setIcon(null);
b[i][j].btn.setText("|||");
b[i][j].btn.setBackground(Color.pink);
}
}
}
}
}