package cn.hncu.gui.swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.InterfaceAddress;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalBorders.ToolBarBorder;
public class Clearbomb extends JFrame implements MouseListener, ActionListener {
private int number = 9;// 几行几列
private int bombNumber = 10;// 多少个雷
private int qiNumber = bombNumber;
String[] areaStr = { "9*9", "16*16", "30*30" };
JComboBox Area = new JComboBox(areaStr);
String[] countStr = { "10", "40", "125" };
JComboBox bombCount = new JComboBox(countStr);
JToolBar toolBar = new JToolBar();
int[] a = bRandon();// 随机数组确定雷所在的JButton
int[][] tt = {
{ -1, 1, -number, number, -number - 1, -number + 1, number - 1,
number + 1 },// 0,在中间的;
{ 1, number, number + 1 },// 1,左上角;
{ -1, number, number - 1 },// 2,右上角;
{ 1, -number, -number + 1 },// 3,左下角;
{ -1, -number, -number - 1 },// 4,右下角;
{ -1, 1, number, number - 1, number + 1 },// 5,上边 ;
{ -1, 1, -number, -number - 1, -number + 1 },// 6,下边 ;
{ -number, number, 1, -number + 1, number + 1 },// 7,左边 ;
{ -number, number, -1, -number - 1, number - 1 } // 8,右边;
};
Icon[] image = new ImageIcon[13];
MyJButton[] b = new MyJButton[number * number];
public Clearbomb(String str) {
super(str);
for (int i = 0; i < 13; i++) {
image[i] = new ImageIcon("pictures\x" + i + ".png");
}//这个地方的的图片要放在同一个环境下,建立在相同的文件夹中。这个地方的文件名是按照i的顺序大小来命名的。
toolBar.add(Area);
toolBar.add(new JLabel("平铺网格"));
Area.addActionListener(this);
toolBar.add(bombCount);
toolBar.add(new JLabel("个雷"));
bombCount.addActionListener(this);
this.getContentPane().add(toolBar, BorderLayout.NORTH);
Dimension dim = getToolkit().getScreenSize();
setSize(number * 30, number * 30 + 60);
setLocation(dim.width / 4, dim.height / 4);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
addButton(number);
reset();
addMenu();
setVisible(true);
}
//加面板,再到panel上面加Button
public void addButton(int reNumber){
JPanel leiArea = new JPanel();
leiArea.setLayout(new GridLayout(reNumber, reNumber));
for (int i = 0; i < reNumber * reNumber; i++) {
b[i] = new MyJButton(image[9]);
b[i].addMouseListener(this);
leiArea.add(b[i]);
}
getContentPane().add(leiArea);
}
public void reset() {
for (int i = 0; i < number * number; i++) {
b[i].setImages(image[9], true,0,false,false,true);
}
a = bRandon();
System.out.print("有雷JButton:");
for (int j = 0; j < a.length; j++) {// 把雷布好
b[a[j]].bomb = true;
b[a[j]].BianLi = false;
System.out.print("->" + a[j]);
}
System.out.println();
for (int i = 0; i < number * number; i++) {// 确定每个格子的雷数
int temp = Loc(i);
for (int j = 0; j < tt[temp].length; j++) {
if (b[i + tt[temp][j]].bomb == true) {
b[i].ge++;
}
}
}
}
private void addMenu() {
JMenuBar bar = new JMenuBar();
this.setJMenuBar(bar);
bar.setSize(number * number, 20);
String menuStr[] = { "游戏", "帮助" };
JMenu[] menu = new JMenu[menuStr.length];
for (int i = 0; i < menuStr.length; i++) {
menu[i] = new JMenu(menuStr[i]);
bar.add(menu[i]);
}
JMenuItem gameReset = new JMenuItem("重新游戏");
menu[0].add(gameReset);
JMenuItem gameExit = new JMenuItem("退出");
menu[0].add(gameExit);
gameReset.addActionListener(this);
gameExit.setActionCommand("exit");
gameExit.addActionListener(this);
}
// 游戏结束
public boolean gameOver() {// 有雷的.qq是true,无雷的qOK都是false
for (int j = 0; j < number * number; j++) {
if (b[j].bomb == true && b[j].qq == false) {// 有雷的.qq是false
return false;
}
if (b[j].bomb == false && b[j].qOk == true) {// 无雷的qOK都是true
return false;
}
}
return true;
}
// 产生number个雷的随机按钮数组
public int[] bRandon() {
int a[] = new int[bombNumber];
int i = 0;
boolean ling = true;
while (i < bombNumber) {
int temp = (int) ((Math.random() * 40000)) % (number * number);
boolean t = true;
for (int j = 0; j < bombNumber; j++) {
if (temp == 0 && ling) {
a[i] = temp;
i++;
ling = false;
}
if (temp == a[j]) {
t = false;
break;
}
}
if (t) {
a[i] = temp;
i++;
}
}
for (int j = 0; j < bombNumber - 1; j++) {
for (int k = j + 1; k < bombNumber; k++) {
if (a[j] > a[k]) {
int temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
return a;
}
public static void main(String[] args) {
new Clearbomb("扫雷游戏");
}
// 按钮的位置
public int Loc(int i) {
if (i == 0) {
return 1;
} else if (i == number - 1) {
return 2;
} else if (i == number * (number - 1)) {
return 3;
} else if (i == number * number - 1) {
return 4;
} else if (i < number - 1 && i > 0) {
return 5;
} else if (i > number * (number - 1) && i < number * number - 1) {
return 6;
} else if (i % number == 0 && i != 0 && i != number * (number - 1)) {
return 7;
} else if ((i + 1) % number == 0 && i != number - 1
&& i != number * number - 1) {
return 8;
}
return 0;
}
public void DD(int i) {
b[i].setIcon(image[0]);
int temp = Loc(i);
for (int x = 0; x < tt[temp].length; x++) {
b[tt[temp][x] + i].setIcon(image[b[tt[temp][x] + i].ge]);
b[tt[temp][x] + i].qOk = false;
if (b[tt[temp][x] + i].ge == 0 && b[tt[temp][x] + i].BianLi == true) {// 旁边一个雷都没有
b[tt[temp][x] + i].BianLi = false;
b[tt[temp][x] + i].qOk = false;
DD(tt[temp][x] + i);
}
}
}
class MyJButton extends JButton {
private boolean BianLi = true; // 用来遍历的标记
private int ge = 0;// 旁边雷的个数
private boolean bomb = false;// true有雷,false无雷
private boolean qq = false;// 没有插旗,与qNumber一起控制旗子的数量
private boolean qOk = true;// 可以插旗,已经翻出来的不能再插旗了
public MyJButton(Icon image) {
super(image);
}
public void setImages(Icon image, boolean BianLi,int ge,boolean bomb,boolean qq,boolean qOk) {
setIcon(image);
this.bomb = bomb;
this.BianLi = BianLi;
this.ge = 0;
this.bomb = bomb;
this.qq = qq;
this.qOk = qOk;
}
public void setGe(int ge) {
this.ge = ge;
}
public int getGe() {
return ge;
}
public void setBomb(boolean bomb) {
this.bomb = bomb;
}
public boolean getBomb() {
return bomb;
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int p = e.getButton();
if (p == MouseEvent.BUTTON1) {// 左键
for (int i = 0; i < number * number; i++) {
if (e.getSource() == b[i] && b[i].qq == false) {
if (b[i].bomb == true) {// 踩雷了
System.out.println("非常抱歉,你输了!");
for (int j = 0; j < number * number; j++) {// 显示所有的视图
if (b[j].bomb != true)
b[j].setIcon(image[b[j].getGe()]);
else {
if (b[j].qq == true) {// 有雷,插旗了
b[j].setIcon(image[12]);
} else {
b[j].setIcon(image[10]);
}
}
}
int k = JOptionPane.showConfirmDialog(this, "非常抱歉,你输了!
是否重新游戏?");
if (k == JOptionPane.NO_OPTION) {
System.exit(0);
}
if (k == JOptionPane.OK_OPTION) {
reset();
return;
}
}
if (e.getSource() == b[i] && b[i].bomb == false) {// 没有踩到雷
if (b[i].ge == 0 && b[i].BianLi == true) {// 旁边一个雷都没有
b[i].BianLi = false;
b[i].qOk = false;
DD(i);
} else {
b[i].BianLi = false;
b[i].qOk = false;
b[i].setIcon(image[b[i].ge]);
}
}
}
}
}
if (p == MouseEvent.BUTTON3) {// 右键
for (int i = 0; i < number * number; i++) {
if (e.getSource() == b[i] && b[i].qOk) {// 可以插旗
if (!b[i].qq && qiNumber > 0) {// 没插旗并且还有旗子可插
b[i].setIcon(image[11]);
b[i].qq = !b[i].qq;
qiNumber--;
} else if (b[i].qq) {// 插旗了,变回来
b[i].setIcon(image[9]);
b[i].qq = !b[i].qq;
qiNumber++;
} else {
JOptionPane.showMessageDialog(this, "没旗子了");
}
}
}
}
if (gameOver()) {
JOptionPane.showMessageDialog(this, "恭喜你赢了");
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JMenuItem) {
if (e.getActionCommand().equals("重新游戏")) {
reset();
}
if (e.getActionCommand().equals("exit")) {
int i = JOptionPane.showConfirmDialog(this, "是否继续游戏?");
if (i == JOptionPane.NO_OPTION) {
System.exit(0);
}
if (i == JOptionPane.OK_OPTION) {
reset();
}
}
}
if (e.getSource() instanceof JComboBox) {
if (e.getSource() == Area) {
String str = (String) Area.getSelectedItem();
if (str.equals("9*9")) {
}
if (str.equals("16*16")) {
}
if (str.equals("30*30")) {
}
}
if (e.getSource() == bombCount) {
String str = (String) bombCount.getSelectedItem();
if (str.equals("10")) {
}
if (str.equals("40")) {
}
if (str.equals("125")) {
}
}
}
}
}