public class AnalogMatrix {// analogMatrix模拟矩阵
int[][] mineField;// mineField地雷阵
public int row, column, minesNumber;
public static int whoMarked;// whoMarked卫标记,出错时可以看卫标记数值,就知道哪里错了
static {
whoMarked = 0;
}
public AnalogMatrix() {
this(11, 11, 30);
}
public AnalogMatrix(int row, int column, int minesNumber) {
this.row = row > 0 ? row : 1;
this.column = column > 0 ? column : 1;
this.minesNumber = minesNumber;
refresh();
}
public void print(int[][] mat) {
if (mat == null || mat.length == 0) {// 卫条件,不满足就结束,目的拦阻非法数据
whoMarked = 1;// 卫标记为1
return;
}
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
System.out.print(String.format("%5d", mat[i][j]));
}
System.out.println();
}
}
public void print(int[] mat) {
if (mat == null || mat.length == 0) {// 卫条件,不满足就结束,目的拦阻非法数据
whoMarked = 4;// 卫标记为1
return;
}
for (int i = 0; i < mat.length; i++) {
System.out.print(mat[i] + " ");
if (i % 5 == 0 && i != 0) {
System.out.println();
}
}
System.out.println("mat.length" + mat.length);
System.out.println(whoMarked);
}
public void refresh() {
mineField = new int[row][column];// mineField地雷阵//初始化
layMines();// 将雷布好
}
public void layMines() {
int[] temp = layMines(0, row * column, minesNumber);// 布置个数
for (int i = 0; i < temp.length; i++) {
int test_i = temp[i] / column;
int test_j = temp[i] % column;
mineField[test_i][test_j] = -1;
}
}
public int[] layMines(int start, int end, int number) {// 左包含,右不包含,在start到end之间选出number个随机数,end<1000
// 布置累的数量
if ((end - start) < 1 || (end - start) < number) {
whoMarked = 2;// 卫标记为2
return null;
}
int[] temp = new int[number];
for (int i = 0; i < temp.length; i++) {
temp[i] = getADifferentRandomNumber(start, end, temp);
}
mySort(temp);
return temp;
}
private int getADifferentRandomNumber(int start, int end, int[] temp) {// 专为layMines而设计
int tempary = start + (int) (Math.random() * 1000) % (end - start);
boolean flag = true;
if (tempary < end && tempary >= start) {
for (int i = 0; i < temp.length; i++) {
if (tempary == temp[i]) {// 如果随机数不与数组中任意数相同,那么flag = true
flag = false;
break;
}
}
} else {
flag = false;
}
if (flag) {// 如果随机数不与数组中任意数相同,那么flag = true
return tempary;
} else {
return getADifferentRandomNumber(start, end, temp);
}
}
public void mySort(int[] mat) {// 升序
if (mat == null) {
whoMarked = 3;// 卫条件为3
return;
}
for (int i = 0; i < mat.length - 1; i++) {
for (int j = i + 1; j < mat.length; j++) {
if (mat[i] > mat[j]) {
mat[i] = mat[i] ^ mat[j];
mat[j] = mat[i] ^ mat[j];
mat[i] = mat[i] ^ mat[j];
}
}
}
}
}
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.Timer;
public class CleanMines extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
MinesJPanel minesjpanel;
AnalogMatrix analogMatrix;
Timer timer;
public CleanMines() {
Dimension dms = getToolkit().getScreenSize();
setBounds((int) dms.getWidth() / 4, (int) dms.getHeight() / 4, 200, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pulsMenuBar();
setResizable(false);
timer = new Timer(10, new Victory(analogMatrix, this, minesjpanel));
setVisible(true);
}
private void pulsMenuBar() {
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
String[] menu_str = { "Start", "Help" };
String[][] menuitem_str = {
{ "New", "Primary", "Middle_rank", "High_rank", "Ranking", "|",
"exit" }, { "Copyright", "^_^CheatingPrograms^_^" } };
for (int i = 0; i < menu_str.length; i++) {
JMenu menu = new JMenu(menu_str[i]);
menubar.add(menu);
for (int j = 0; j < menuitem_str[i].length; j++) {
if (menuitem_str[i][j].equals("|")) {
menu.add(new JSeparator());
continue;
}
JMenuItem menuitem = new JMenuItem(menuitem_str[i][j]);
menuitem.addActionListener(this);
menu.add(menuitem);
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("new")) {
if(minesjpanel==null){
return;
}
minesjpanel.btn.setText("Start");
minesjpanel.run();
}
if (e.getActionCommand().equalsIgnoreCase("Primary")) {
setRanking(analogMatrix = new AnalogMatrix(9, 9, 20));
}
if (e.getActionCommand().equalsIgnoreCase("Middle_rank")) {
setRanking(analogMatrix = new AnalogMatrix(15, 15, 50));
this.setLocation(200, 0);
}
if (e.getActionCommand().equalsIgnoreCase("High_rank")) {
setRanking(analogMatrix = new AnalogMatrix(20, 20, 100));
setLocation(0, 0);
}
if (e.getActionCommand().equalsIgnoreCase("Ranking")) {
}
if (e.getActionCommand().equalsIgnoreCase("exit")) {
if (JOptionPane.showConfirmDialog(this, "亲~你真的要退出吗?") == 0) {
System.exit(0);
}
}
if (e.getActionCommand().equalsIgnoreCase("Copyright")) {
JOptionPane.showMessageDialog(this,
"所有版权信息归 \"刘强\"所有,如有违权者,后果自负!!!");
}
if (e.getActionCommand().equalsIgnoreCase("^_^CheatingPrograms^_^")) {
if (JOptionPane.showConfirmDialog(this, "亲~你确定要开外挂吗?") == 0) {
if (minesjpanel == null) {
JOptionPane.showMessageDialog(this, "请选择等级!!!");
return;
}
minesjpanel.cheatingProgram(analogMatrix);
}
}
}
private void setRanking(AnalogMatrix analogMatrix) {
if (minesjpanel != null) {
this.remove(minesjpanel);
}
minesjpanel = new MinesJPanel(analogMatrix, this);
minesjpanel.setMinesNumber(analogMatrix);
this.setSize(analogMatrix.row * 30, analogMatrix.column * 30);
getContentPane().add(minesjpanel);
}
public static void main(String[] args) {
new CleanMines();
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
public class MinesJPanel extends JPanel implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
public AnalogMatrix analogMatrix;// 核心函数********
CleanMines cleanMines;
public JButton[] btns;
private JPanel northpanel, centerpanel;
public JButton btn;
public Color color;
public MinesJPanel(AnalogMatrix analogMatrix, CleanMines cleanMines) {
this.analogMatrix = analogMatrix;
this.cleanMines = cleanMines;
setBorder(new TitledBorder("扫雷啦!"));
setLayout(new BorderLayout());
setNorthpanel();
}
public void setMinesNumber(AnalogMatrix analogMatrix) {
this.analogMatrix = analogMatrix;
centerpanel = new JPanel();
add(centerpanel);
centerpanel.setLayout(new GridLayout(analogMatrix.row,
analogMatrix.column));
btns = new JButton[analogMatrix.row * analogMatrix.column];
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton("");
btns[i].setMargin(new Insets(0, 0, 0, 0));
centerpanel.add(btns[i]);
}
color = btns[0].getBackground();
}
private void setNorthpanel() {
northpanel = new JPanel();
add(northpanel, BorderLayout.NORTH);
btn = new JButton("Start");
northpanel.add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("Start")) {// 每当游戏开始时将所有的按钮清空
new Thread(this).start();
return;
}
if (e.getActionCommand().equalsIgnoreCase("End")) {// 显示地雷
new Thread(this).start();
return;
}
for (int i = 0; i < btns.length; i++) {
if (e.getSource() == btns[i]) {
new SearchMines(this.analogMatrix, this, i);
return;
}
}
}
public void cheatingProgram(AnalogMatrix analogMatrix) {
int count = 0;
for (int i = 0; i < analogMatrix.row; i++) {
for (int j = 0; j < analogMatrix.column; j++) {
if (analogMatrix.mineField[i][j] == -1) {
btns[count++].setText("@");
} else {
btns[count++].setText(" ");
}
}
}
}
public void run() {
if (btn.getText().equalsIgnoreCase("Start")) {
cleanMines.timer = new Timer(10, new Victory(analogMatrix,
cleanMines, this));
cleanMines.timer.start();
for (int i = 0; i < btns.length; i++) {
btns[i].addActionListener(this);
btns[i].setText("");
btns[i].setBackground(color);
}
this.analogMatrix.refresh();
this.btn.setText("End");
} else {
for (int i = 0; i < btns.length; i++) {
btns[i].removeActionListener(this);
}
cheatingProgram(analogMatrix);
this.btn.setText("Start");
}
}
}
import java.awt.Color;
public class SearchMines implements Runnable {
AnalogMatrix analogMatrix;
MinesJPanel minesJPanel;
int buttonNumber, text_i, text_j;
static int number = 0;
public SearchMines(AnalogMatrix analogMatrix, MinesJPanel minesJPanel,
int text_i, int text_j) {
this.analogMatrix = analogMatrix;
this.minesJPanel = minesJPanel;
this.text_i = text_i;
this.text_j = text_j;
buttonNumber = text_i * analogMatrix.row + text_j;
new Thread(this).start();
}
public SearchMines(AnalogMatrix analogMatrix, MinesJPanel minesJPanel,
int buttonNumber) {
this.analogMatrix = analogMatrix;
this.minesJPanel = minesJPanel;
this.buttonNumber = buttonNumber;
text_i = buttonNumber / analogMatrix.column;
text_j = buttonNumber % analogMatrix.column;
new Thread(this).start();
}
public boolean searchYourSelf() {
if (analogMatrix.mineField[text_i][text_j] == -1) {
minesJPanel.btns[text_i * analogMatrix.row + text_j]
.setBackground(Color.red);
minesJPanel.btns[buttonNumber].setText("@");
return true;
} else {
minesJPanel.btns[text_i * analogMatrix.row + text_j]
.setBackground(Color.blue);
return false;
}
}
public int[] rectangle(int x, int y) {
text_i = x;
text_j = y;
return rectangle();
}
public int[] rectangle() {// 获得当前点的搜索范围,与起始坐标
int[] rect = new int[4];
int[] surround = { -1, 1 };
int left_x = text_i - 1;// 初始化在左上角
int up_y = text_j - 1;
int length_x = 3;// 初始化长度为3
int length_y = 3;
for (int i = 0; i < surround.length; i++) {
int temp = text_i + surround[i];
if (temp < 0) {
left_x = text_i;
length_x -= 1;
continue;
}
if (temp == analogMatrix.row) {
length_x -= 1;
}
}
for (int i = 0; i < surround.length; i++) {
int temp = text_j + surround[i];
if (temp < 0) {
up_y = text_j;
length_y -= 1;
continue;
}
if (temp == analogMatrix.column) {
length_y -= 1;
}
}
rect[0] = left_x;
rect[1] = up_y;
rect[2] = length_x;
rect[3] = length_y;
return rect;
}
public int searchSurroundAllMines(int x, int y) {
text_i = x;
text_j = y;
return searchSurroundAllMines();
}
public int searchSurroundAllMines() {// 周围有几个地雷
int count = 0;
int[] temp1 = rectangle();
for (int i = temp1[0]; i < temp1[0] + temp1[2]; i++) {
for (int j = temp1[1]; j < temp1[1] + temp1[3]; j++) {
if (analogMatrix.mineField[i][j] == -1) {
count++;
}
}
}
return count;
}
public void hightRearch(int text_i, int text_j) {
this.text_i = text_i;
this.text_j = text_j;
int[] temp1 = rectangle();
for (int i = temp1[0]; i < temp1[0] + temp1[2]; i++) {
for (int j = temp1[1]; j < temp1[1] + temp1[3]; j++) {
if (analogMatrix.mineField[i][j] == 0) {
searchSurroundAll();
}
}
}
}
public void searchSurroundAll() {
int[] temp1 = rectangle();
for (int i = temp1[0]; i < temp1[0] + temp1[2]; i++) {
for (int j = temp1[1]; j < temp1[1] + temp1[3]; j++) {
if (analogMatrix.mineField[i][j] == 0) {
minesJPanel.btns[i * analogMatrix.row + j]
.setText(searchSurroundAllMines(i, j) == 0 ? ""
: "" + searchSurroundAllMines(i, j));
minesJPanel.btns[i * analogMatrix.row + j]
.setBackground(Color.blue);
}
}
}
}
public void run() {
if (searchYourSelf()) {// 是地雷就结束
minesJPanel.btn.setText("end");
minesJPanel.run();
} else {// 不是地雷就搜索周边的按钮
minesJPanel.btns[buttonNumber]
.setText(+searchSurroundAllMines() == 0 ? "" : ""
+ searchSurroundAllMines());
hightRearch(text_i, text_j);
}
}
}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class Victory implements ActionListener {
AnalogMatrix analogMatrix;
CleanMines cleanMines;
MinesJPanel minesJPanel;
public Victory(AnalogMatrix analogMatrix, CleanMines cleanMines,
MinesJPanel minesJPanel) {
this.analogMatrix = analogMatrix;
this.cleanMines = cleanMines;
this.minesJPanel = minesJPanel;
}
public void actionPerformed(ActionEvent e) {
if (minesJPanel.btn.getText().equalsIgnoreCase("End")) {
int count = 0;
for (int i = 0; i < minesJPanel.btns.length; i++) {
if (minesJPanel.btns[i].getBackground() == Color.blue) {
count++;
}
}
if ((analogMatrix.row * analogMatrix.column - count) == analogMatrix.minesNumber) {
JOptionPane.showConfirmDialog(null, "%恭喜你胜利了%");
minesJPanel.btn.setText("End");
minesJPanel.run();
}
}
}
}