yakis 3 months ago
parent e14a4c05a0
commit 316da62ca2

Binary file not shown.

@ -1,289 +1,308 @@
package com.first; package com.first;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class MineSweeper { public class MineSweeper {
static private GamePanel gPanel;/* 雷区 */ static private GamePanel gPanel;/* 雷区 */
static private int midtime = 3600, mineNum = 0;/* 倒计时时间以及可用旗子数量 */ static private int midtime = 3600, mineNum = 0;/* 倒计时时间以及可用旗子数量 */
static private JLabel label1, label2; static private JLabel label1, label2;
static CountDown cd; static CountDown cd;
/**
* public MineSweeper() {
*/ JFrame jf = new JFrame("扫雷小游戏");
public MineSweeper() { jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame jf = new JFrame("扫雷小游戏"); jf.setLayout(null);
jf.setDefaultCloseOperation(3);// EXIT_ON_CLOSE,直接关闭应用程序System.exit(0)。一个main函数对应一整个程序。 jf.setBounds(600, 200, 700, 800);
jf.setLayout(null);// 未设置Layout时java默认为flowLayout布局的设置为null即为清空布局管理器之后添加组件常常是设置组件左上角坐标相对于容器左上角00的x,y值来确定组件的位置
jf.setBounds(600, 200, 700, 800);// 设置窗口的大小和位置 label1 = new JLabel("剩余时间:" + formatTime(midtime));
label1 = new JLabel("剩余时间:" + (midtime / 60 / 60 % 60) + ":" + (midtime / 60 % 60) + ":" + (midtime % 60)); label1.setBounds(10, 20, 120, 20);
label1.setBounds(10, 20, 120, 20); jf.add(label1);
jf.add(label1);
label2 = new JLabel("剩余旗子:" + mineNum); label2 = new JLabel("剩余旗子:" + mineNum);
label2.setBounds(400, 20, 240, 20);// 设置label2的位置以及大小 label2.setBounds(400, 20, 240, 20);
jf.add(label2); jf.add(label2);
JButton resetButt = new JButton("重置");// 创建重置按钮
resetButt.setMargin(new Insets(0, 0, 0, 0));// 设置边框 JButton resetButt = new JButton("重置");
resetButt.setBounds(230, 15, 60, 30);// 设置按钮在窗口中的位置和大小 resetButt.setMargin(new Insets(0, 0, 0, 0));
resetButt.addActionListener(new ActionListener() { resetButt.setBounds(230, 15, 60, 30);
@Override resetButt.addActionListener(e -> {
public void actionPerformed(ActionEvent e) { jf.dispose();
// TODO Auto-generated method stub new MineSweeper();
jf.dispose();/* 销毁窗口 */ midtime = 3600;
new MineSweeper(); });
midtime = 3600; jf.add(resetButt);
}
}); gPanel = new GamePanel(20, 20);
jf.add(resetButt);// 把按钮添加到窗口中 gPanel.setBounds(40, 100, 600, 600);
gPanel = new GamePanel(20, 20);// 设置按钮数量为20行*20列 jf.add(gPanel);
gPanel.setBounds(40, 100, 600, 600);// 设置雷区面板的位置以及大小
jf.add(gPanel);// 雷区面板添加到窗口中 jf.setVisible(true);
jf.setVisible(true);// 设置窗口可见 }
}
static class CountDown extends Thread {
static class CountDown extends Thread { @Override
@Override public void run() {
public void run() { super.run();
super.run(); while (midtime > 0) {
while (midtime > 0) { try {
try { --midtime;
--midtime; label1.setText("剩余时间:" + formatTime(midtime));
label1.setText( sleep(1000);
"剩余时间:" + (midtime / 60 / 60 % 60) + ":" + (midtime / 60 % 60) + ":" + (midtime % 60)); } catch (InterruptedException e) {
sleep(1000); e.printStackTrace();
} catch (InterruptedException e) { }
e.printStackTrace(); }
} if (midtime == 0) {
} JOptionPane.showMessageDialog(null, "时间已到", "游戏结束", JOptionPane.PLAIN_MESSAGE);
if (midtime == 0) { }
JOptionPane.showMessageDialog(null, "时间已到", "游戏结束", JOptionPane.PLAIN_MESSAGE); }
} }
}
private static String formatTime(int seconds) {
} int hrs = seconds / 3600;
int mins = (seconds % 3600) / 60;
public static void main(String[] args) {// 程序的入口 int secs = seconds % 60;
new MineSweeper();// 进入构造函数 return String.format("%02d:%02d:%02d", hrs, mins, secs);
cd = new CountDown();// 创建计时器 }
cd.start();/* 启动计时器 */
} public static void main(String[] args) {
new MineSweeper();
public static void setMineNum(int minesCount2) { cd = new CountDown();
// TODO Auto-generated method stub cd.start();
mineNum = minesCount2; }
label2.setText("剩余旗子:" + mineNum);
} public static void setMineNum(int minesCount2) {
mineNum = minesCount2;
label2.setText("剩余旗子:" + mineNum);
}
} }
class GamePanel extends JPanel { class GamePanel extends JPanel {
private int rows, cols, minesCount;// 创建行数、列数变量,地雷的数量 private int rows, cols, minesCount;
private final int BLOCKWIDTH = 30;// 规定每个按钮的宽度 private final int BLOCKWIDTH = 30;
private final int BLOCKHEIGHT = 30;// 规定每个按钮的高度 private final int BLOCKHEIGHT = 30;
private Buts[][] buts;// 雷区按钮 private Buts[][] buts;
private JLabel[][] jlabel;// 每个按钮下对应标签 private JLabel[][] jlabel;
private boolean[][] state;// true:有雷false:没有雷 private boolean[][] state;
private int[][] click;// 0:未点击1:已点击2未点击但周围有雷3插旗 private int[][] click;
public GamePanel(int row, int col) { private ImageIcon mineIcon;
rows = row;// 行数
cols = col;// 列数 public GamePanel(int row, int col) {
minesCount = rows * cols / 10;// 地雷的数量 rows = row;
MineSweeper.setMineNum(minesCount); cols = col;
buts = new Buts[rows][cols];// 创建雷区按钮 minesCount = rows * cols / 10;
jlabel = new JLabel[rows][cols];// 创建标签 MineSweeper.setMineNum(minesCount);
state = new boolean[rows][cols];// true:有雷false:没有雷
click = new int[rows][cols];// 0:未点击1:已点击2未点击但周围有雷3插旗
setLayout(null);// 设置为null即为清空布局管理器之后添加组件常常是设置组件左上角坐标相对于容器左上角00的x,y值来确定组件的位置 try {
initButtons();// 初始化按钮,设置每个按钮的基本参数 mineIcon = new ImageIcon(ImageIO.read(new File("mine.png")));
initLable();// 初始化标签方格,设置每个标签的基本参数 } catch (IOException e) {
buryMines();// 埋雷,随机生成地雷 e.printStackTrace();
writerNumber();// 计算每个方格周围8个方格中有几个地雷 }
}
// 缩放图标到合适大小
/* 用于统计每个方格周围有多少地雷 */ Image scaledImage = mineIcon.getImage().getScaledInstance(
private void writerNumber() { BLOCKWIDTH, BLOCKHEIGHT, Image.SCALE_SMOOTH);
for (int i = 0; i < rows; i++) { mineIcon = new ImageIcon(scaledImage);
for (int j = 0; j < cols; j++) {
if (!state[i][j]) {// 没有雷的地方才探测周围的情况 buts = new Buts[rows][cols];
int numbers = 0;// 用于记录每个方格四周有多少地雷 jlabel = new JLabel[rows][cols];
for (int r = -1; r < 2; r++) {// 探测每个方格行坐标-10+1后的情况 state = new boolean[rows][cols];
for (int c = -1; c < 2; c++) {// 探测每个方格列坐标-10+1后的情况 click = new int[rows][cols];
if ((i + r > -1) && (i + r < cols) && (j + c > -1) && (j + c < rows)) {// 判断有没有超过数组的边界
if (state[i + r][j + c] == true) {// 探测的位置上有地雷则numbers加1 setLayout(null);
numbers++; initButtons();
} initLable();
} buryMines();
} writerNumber();
} }
if (numbers > 0) {
click[i][j] = 2;// 设置为:未点击但周围有雷 private void writerNumber() {
} for (int i = 0; i < rows; i++) {
jlabel[i][j].setText(String.valueOf(numbers));// 给每个单元赋值周围8个方格中有几个地雷 for (int j = 0; j < cols; j++) {
// buts[i][j].setText(String.valueOf(numbers));// 调试代码用 if (!state[i][j]) {
} int numbers = 0;
} for (int r = -1; r < 2; r++) {
} for (int c = -1; c < 2; c++) {
} if ((i + r > -1) && (i + r < cols) && (j + c > -1) && (j + c < rows)) {
if (state[i + r][j + c]) numbers++;
private void initButtons() { }
for (int i = 0; i < rows; i++) { }
for (int j = 0; j < cols; j++) { }
Buts but = new Buts(); if (numbers > 0) {
but.i = i;// 给按钮的行信息赋值 click[i][j] = 2;
but.j = j;// 给按钮的列信息赋值 }
but.setBounds(BLOCKWIDTH * j, BLOCKHEIGHT * i, BLOCKWIDTH, BLOCKHEIGHT);// 设置每一个按钮的位置和大小 jlabel[i][j].setText(String.valueOf(numbers));
but.setMargin(new Insets(0, 0, 0, 0));// 设置按钮边框 jlabel[i][j].setIcon(null); // 确保数字位置不显示图标
/* 给按钮添加鼠标监听器,检测是否点击 */ }
but.addMouseListener(new MouseAdapter() { }
@Override }
public void mouseClicked(MouseEvent e) { }
/* 左击 */
if (e.getButton() == MouseEvent.BUTTON1) { private void initButtons() {
// but.setVisible(false);//调试代码用 for (int i = 0; i < rows; i++) {
// jlabel[but.i][but.j].setVisible(true);//调试代码用 for (int j = 0; j < cols; j++) {
openButtons(but); Buts but = new Buts();
} but.i = i;
/* 右击 */ but.j = j;
if (e.getButton() == MouseEvent.BUTTON3) { but.setBounds(BLOCKWIDTH * j, BLOCKHEIGHT * i, BLOCKWIDTH, BLOCKHEIGHT);
// but.setText("旗");// 调试代码用 but.setMargin(new Insets(0, 0, 0, 0));
placeFlag(but);
} but.addMouseListener(new MouseAdapter() {
} @Override
}); public void mouseClicked(MouseEvent e) {
buts[i][j] = but;// 赋值给公共变量,方便调用 if (e.getButton() == MouseEvent.BUTTON1) {
this.add(buts[i][j]);// 添加到面板中jpanel openButtons(but);
} }
} if (e.getButton() == MouseEvent.BUTTON3) {
} placeFlag(but);
}
protected void placeFlag(Buts but) { }
if (click[but.i][but.j] != 3) { });
buts[but.i][but.j].setText("旗"); buts[i][j] = but;
// System.out.println("click[but.i][but.j] =" + click[but.i][but.j]);// 调试代码用 this.add(buts[i][j]);
click[but.i][but.j] = 3;// 0:未点击1:已点击2未点击但周围有雷3插旗 }
minesCount--; }
MineSweeper.setMineNum(minesCount); }
} else {
buts[but.i][but.j].setText(""); protected void placeFlag(Buts but) {
if (!state[but.i][but.j]) { if (click[but.i][but.j] != 3) {
if (Integer.valueOf(jlabel[but.i][but.j].getText()) > 0) { buts[but.i][but.j].setText("旗");
click[but.i][but.j] = 2;// 周围有地雷 click[but.i][but.j] = 3;
// System.out.println("click[but.i][but.j] =" + click[but.i][but.j]);// 调试代码用 minesCount--;
} else { MineSweeper.setMineNum(minesCount);
click[but.i][but.j] = 0;// 未点击 } else {
// System.out.println("click[but.i][but.j] =" + click[but.i][but.j]);// 调试代码用 buts[but.i][but.j].setText("");
} if (!state[but.i][but.j]) {
minesCount++; if (Integer.valueOf(jlabel[but.i][but.j].getText()) > 0) {
MineSweeper.setMineNum(minesCount); click[but.i][but.j] = 2;
} else { } else {
click[but.i][but.j] = 0;// 未点击 click[but.i][but.j] = 0;
} }
minesCount++;
} MineSweeper.setMineNum(minesCount);
if (minesCount == 0) {// 当旗子用完后,判断是否全部标记正确 } else {
boolean flag = true; click[but.i][but.j] = 0;
for (int i = 0; i < rows; i++) { }
for (int j = 0; j < cols; j++) { }
if ((click[i][j] != 3) && (state[i][j])) {// 对所有没有插旗的方格进行判断如果没有插旗但是state的状态又未true则令flag未false if (minesCount == 0) {
flag = false; boolean flag = true;
} for (int i = 0; i < rows; i++) {
} for (int j = 0; j < cols; j++) {
} if ((click[i][j] != 3) && state[i][j]) {
if (flag) { flag = false;
MineSweeper.cd.stop();// 停止计时 }
JOptionPane.showMessageDialog(null, "您成功了", "游戏结束", JOptionPane.PLAIN_MESSAGE); }
} else { }
MineSweeper.cd.stop();// 停止计时 MineSweeper.cd.stop();
JOptionPane.showMessageDialog(null, "旗子已经用完", "游戏失败", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(null, flag ? "您成功了" : "旗子已经用完", "游戏结束", JOptionPane.PLAIN_MESSAGE);
} }
} }
}
protected void openButtons(Buts but) {
protected void openButtons(Buts but) { int i = but.i;
int i = but.i; int j = but.j;
int j = but.j;
if (state[i][j] && (click[i][j] != 3)) { if (state[i][j] && (click[i][j] != 3)) {
openAllCell(); openAllCell();
MineSweeper.cd.stop(); MineSweeper.cd.stop();
JOptionPane.showMessageDialog(null, "您踩到地雷了", "游戏结束", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(null, "您踩到地雷了", "游戏结束", JOptionPane.PLAIN_MESSAGE);
} }
if (click[i][j] == 2) {
buts[i][j].setVisible(false); if (click[i][j] == 2) {
jlabel[i][j].setVisible(true); buts[i][j].setVisible(false);
} jlabel[i][j].setVisible(true);
if ((click[i][j] == 0) || (click[i][j] == 1)) {// 0的周围是要判断的1的周围也有可能所以也要判断 }
for (int r = -1; r < 2; r++) {
for (int c = -1; c < 2; c++) { if ((click[i][j] == 0) || (click[i][j] == 1)) {
if ((i + r > -1) && (i + r < cols) && (j + c > -1) && (j + c < rows) && (!state[i + r][j + c])) {// 判断有没有超过数组的边界 for (int r = -1; r < 2; r++) {
if (click[i + r][j + c] == 0) {// 0:未点击1:已点击2未点击但周围有雷3插旗 for (int c = -1; c < 2; c++) {
buts[i + r][j + c].setVisible(false); if ((i + r > -1) && (i + r < cols) && (j + c > -1) && (j + c < rows)
jlabel[i + r][j + c].setVisible(true); && (!state[i + r][j + c])) {
click[i + r][j + c] = 1;
openButtons(buts[i + r][j + c]); if (click[i + r][j + c] == 0) {
} buts[i + r][j + c].setVisible(false);
if (click[i + r][j + c] == 2) { jlabel[i + r][j + c].setVisible(true);
buts[i + r][j + c].setVisible(false); click[i + r][j + c] = 1;
jlabel[i + r][j + c].setVisible(true); openButtons(buts[i + r][j + c]);
} }
}
} if (click[i + r][j + c] == 2) {
} buts[i + r][j + c].setVisible(false);
} jlabel[i + r][j + c].setVisible(true);
} }
}
private void openAllCell() { }
// TODO Auto-generated method stub }
for (int i = 0; i < rows; i++) { }
for (int j = 0; j < cols; j++) { }
buts[i][j].setVisible(false);
jlabel[i][j].setVisible(true); private void openAllCell() {
click[i][j] = 1; for (int i = 0; i < rows; i++) {
} for (int j = 0; j < cols; j++) {
} buts[i][j].setVisible(false);
} jlabel[i][j].setVisible(true);
if (state[i][j]) {
private void initLable() { jlabel[i][j].setIcon(mineIcon);
for (int i = 0; i < rows; i++) { }
for (int j = 0; j < cols; j++) { click[i][j] = 1;
JLabel l = new JLabel("", JLabel.CENTER); }
l.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);// 设置每个小方格的位置大小 }
l.setBorder(BorderFactory.createLineBorder(Color.gray));// 绘制方格边框为灰色 }
l.setOpaque(true);// 设置方格为透明,方便我们填充
l.setBackground(Color.lightGray);// 背景填充为浅灰色 private void initLable() {
this.add(l);// 添加到面板中 for (int i = 0; i < rows; i++) {
jlabel[i][j] = l;// 将方格加到类变量中,方便公用 for (int j = 0; j < cols; j++) {
l.setVisible(false);// 设置不可见 JLabel l = new JLabel("", JLabel.CENTER);
} l.setBounds(j * BLOCKWIDTH, i * BLOCKHEIGHT, BLOCKWIDTH, BLOCKHEIGHT);
} l.setBorder(BorderFactory.createLineBorder(Color.gray));
} l.setOpaque(true);
l.setBackground(Color.lightGray);
private void buryMines() {// 生成雷区 l.setHorizontalTextPosition(SwingConstants.CENTER);
for (int i = 0; i < minesCount; i++) { l.setVerticalTextPosition(SwingConstants.CENTER);
int x = (int) (Math.random() * rows);// 生成随机行数 this.add(l);
int y = (int) (Math.random() * cols);// 生成随机列数 jlabel[i][j] = l;
/* 判断随机生成的位置是不是有地雷 */ l.setVisible(false);
if (state[x][y] == true) {// 如果随机生成的位置上已经有雷了则i-- }
i--; }
} else {// 位置上没有雷则state为true标签设置为雷 }
state[x][y] = true;
jlabel[x][y].setText("雷"); private void buryMines() {
// buts[x][y].setText("雷");// 调式代码用 for (int i = 0; i < minesCount; i++) {
// System.out.println(x + "," + y + ":位置有地雷");//调式代码用 int x = (int) (Math.random() * rows);
} int y = (int) (Math.random() * cols);
} if (!state[x][y]) {
} state[x][y] = true;
jlabel[x][y].setIcon(mineIcon);
jlabel[x][y].setText("");
} else {
i--;
}
}
}
} }
class Buts extends JButton { class Buts extends JButton {
public int i, j; public int i, j;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Loading…
Cancel
Save