因为是我的课程设计,要是有冲突就不好了,转载注明出处!!!

程序很简单,毕竟我是搞acm的,我就只介绍一下闪光点。

中心空白搜索的时候,我用的DFS;

有一点是要注意的,就是JFrame不支持重画,还好机智我用的是JPanel,利用这个画板重画游戏区。

API文档之后补上。

package com.TreeDream.MyGame;

public class Block {

    String name;
int number;
boolean boo = false; public void setName(String name) {
this.name = name;
} public void setNumber(int n) {
number = n;
} public int getNumber() {
return number;
} public String getName() {
return name;
} public void setIsMine(boolean boo) {
this.boo = boo;
} public boolean isMine() {
return boo;
} }

Block

package com.TreeDream.MyGame;

import java.awt.CardLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel; public class BlockView extends JPanel {
JLabel blockName; //标签
JButton blockCover; //按钮
CardLayout card; public BlockView() {
card = new CardLayout();
setLayout(card); blockName = new JLabel();
blockCover = new JButton(); add("cover",blockCover);
add("name",blockName); } public void setName(String name) {
blockName.setText(name);
} public String getName() {
return blockName.getText();
} public void seeBlockName() {
card.show(this, "name");
} public void seeBlockCover() {
card.show(this, "cover");
validate();
} public JButton getBlockCover() {
return blockCover;
} public JLabel getBlockName () {
return blockName;
} }

BlockView

package com.TreeDream.MyGame;

import java.util.LinkedList; 

public class LayMines {

    public void layMinesForBlock(Block[][] block,int mineCount){

        int row=block.length;
int colum=block[0].length;
LinkedList<Block> list=new LinkedList<Block>();//创建空链表 for(int i=0;i<row;i++){
for(int j=0;j<colum;j++){
list.add(block[i][j]);
}
}
while(mineCount>0){
int size=list.size();
int randomIndex=(int)(Math.random()*size);
Block b=(Block)list.get(randomIndex);
b.setName("*");
b.setIsMine(true);
list.remove(randomIndex);
mineCount--;
} for(int i=0;i<row;i++){//非雷
for(int j=0;j<colum;j++){
if(block[i][j].isMine()==false){
int mineNumber=0;//周围雷的设置
for(int k=Math.max(i-1,0);k<=Math.min(i+1,row-1);k++){
for(int t=Math.max(j-1,0);t<=Math.min(j+1,colum-1);t++){
if(block[k][t].isMine()==true){
mineNumber++;
}
}
} if(mineNumber==0){
block[i][j].setName("");
block[i][j].setNumber(mineNumber);
} else{
block[i][j].setName(""+mineNumber);
block[i][j].setNumber(mineNumber);
} }
}
}
} }

LayMines

 package com.TreeDream.MyGame;

 import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import sun.audio.*;
import java.io.*;
import java.net.MalformedURLException;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.MenuBar;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.security.cert.TrustAnchor; import javax.sound.midi.MidiChannel;
import javax.sound.sampled.AudioInputStream;
import javax.swing.BorderFactory;
import javax.swing.BoundedRangeModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.border.Border; /**
* 游戏布局
* @author YinJian
*
*/ public class MineMainFrame extends JFrame implements ActionListener,MouseListener { /**
* 菜单条
*/
JMenuBar menuBar = new JMenuBar();
/**
* 菜单
*/
JMenu about = new JMenu("关于");
/**
* 子菜单
*/
JMenuItem exItem,aboutItem; /**
* 行数输入文本框
*/
JTextField inputRow = new JTextField(5); /**
* 列数输入文本框
*/
JTextField inputColum = new JTextField(5); /**
* 雷数输入文本
*/
JTextField inputMineCount = new JTextField(5); /**
* 游戏对象
*/
MineMainFrame mainFrame = this; /**
* 开始按钮
*/
JButton start; /**
* 方块的二维数组
*/
Block block[][];
/**
* 方块的视图数组
*/
BlockView blockView[][]; /**
* 深搜标记
*/
boolean vis[][]; /**
* 雷是否被插旗
*/
boolean isFlag[][]; /**
* 当前插旗的数目
*/
int flagNum; /**
* 游戏行数
*/
int row; /**
* 游戏列数
*/
int colum; /**
* 游戏雷数
*/
int mineCount; /**
* 游戏中心区
*/
JPanel pCenter; /**
* 游戏输入区
*/
JPanel pNorth; /**
* 游戏雷的布局对象
*/
LayMines lay; Icon icon1 = new ImageIcon("img/1.jpg");
Icon icon2 = new ImageIcon("img/2.jpg");
Icon icon3 = new ImageIcon("img/3.jpg");
Icon icon4 = new ImageIcon("img/4.jpg");
Icon icon5 = new ImageIcon("img/5.jpg");
Icon icon6 = new ImageIcon("img/6.jpg");
Icon icon7 = new ImageIcon("img/7.jpg");
Icon icon8 = new ImageIcon("img/8.jpg");
Icon icon9 = new ImageIcon("img/9.jpg");
Icon iconflag = new ImageIcon("img/红旗.jpg");
Icon iconmine = new ImageIcon("img/雷.jpg"); /**
* 音乐
*/
AudioClip music; /**
* 游戏初始化,构造函数
* @param title 游戏标题
*/
public MineMainFrame(String title) {
setTitle(title); //插入音乐
File file = new File("musics/Lone Ranger.wav"); try {
music = Applet.newAudioClip(file.toURI().toURL());
music.play();
} catch (MalformedURLException e) {
e.printStackTrace();
} //菜单制作
exItem = new JMenuItem("退出");
aboutItem = new JMenuItem("介绍"); aboutItem.addActionListener(new AboutListen());
exItem.addActionListener(new exitActionListen()); about.add(aboutItem);
about.add(exItem); menuBar.add(about); setJMenuBar(menuBar); //画游戏区域
start = new JButton("开始");
pCenter = new JPanel();
pNorth = new JPanel();
pNorth.setBackground(Color.red); start.addActionListener(this);
inputColum.addActionListener(this);
inputMineCount.addActionListener(this);
inputRow.addActionListener(this); pNorth.add(new JLabel("row"));
pNorth.add(inputRow);
pNorth.add(new JLabel("col"));
pNorth.add(inputColum);
pNorth.add(new JLabel("count"));
pNorth.add(inputMineCount);
pNorth.add(start);
add(pNorth, BorderLayout.NORTH);
setBounds(300, 100, 550, 550);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} /**
* 监听文本框,开始按钮,和游戏区的动作
*/
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (e.getSource() == inputRow) {
row = Integer.parseInt(str);
System.out.println(row);
}
if (e.getSource() == inputColum) {
colum = Integer.parseInt(str);
System.out.println(colum);
}
if (e.getSource() == inputMineCount) {
mineCount = Integer.parseInt(str);
System.out.println(mineCount);
} else {
JButton source = (JButton) e.getSource();
if (source != start) {
int m = -1, n = -1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
if (source == blockView[i][j].getBlockCover()) {
m = i;
n = j;
break;
}
}
}
if (block[m][n].isMine()) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
blockView[i][j].getBlockCover().removeActionListener(this);
if (block[i][j].isMine()) {
blockView[i][j].seeBlockName();
}
}
}
JOptionPane.showMessageDialog(this, "小朋友你挂了哦!快看看你踩到哪个雷了啊"); } else {
if (block[m][n].getNumber() > 0) {
vis[m][n] = true;
blockView[m][n].seeBlockName();
} else if (block[m][n].getNumber() == 0) {
dfs(m, n);
}
}
}
if (source == start) {
remove(this.pCenter);
pCenter = new JPanel();
flagNum = 0;
System.out.println("hello");
block = new Block[row][colum];
vis = new boolean[row][colum];
isFlag = new boolean[row][colum]; for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
block[i][j] = new Block();
vis[i][j] = false; }
} lay = new LayMines();
lay.layMinesForBlock(block, mineCount); blockView = new BlockView[row][colum]; pCenter.setLayout(new GridLayout(row, colum)); for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
blockView[i][j] = new BlockView();
// blockView[i][j].setName(block[i][j].getName());
// 给blockView视图添加上图片
if (block[i][j].getNumber() > 0) { if (block[i][j].getNumber() == 1)
blockView[i][j].getBlockName().setIcon(icon1);
if (block[i][j].getNumber() == 2)
blockView[i][j].getBlockName().setIcon(icon2);
if (block[i][j].getNumber() == 3)
blockView[i][j].getBlockName().setIcon(icon3);
if (block[i][j].getNumber() == 4)
blockView[i][j].getBlockName().setIcon(icon4);
if (block[i][j].getNumber() == 5)
blockView[i][j].getBlockName().setIcon(icon5);
if (block[i][j].getNumber() == 6)
blockView[i][j].getBlockName().setIcon(icon6);
if (block[i][j].getNumber() == 7)
blockView[i][j].getBlockName().setIcon(icon7);
if (block[i][j].getNumber() == 8)
blockView[i][j].getBlockName().setIcon(icon8);
if (block[i][j].getNumber() == 9)
blockView[i][j].getBlockName().setIcon(icon9);
} if (block[i][j].isMine()) {
blockView[i][j].getBlockName().setIcon(iconmine);
isFlag[i][j] = false;
} pCenter.add(blockView[i][j]);
blockView[i][j].getBlockCover().addActionListener(this);
blockView[i][j].getBlockCover().addMouseListener(this);
}
} add(pCenter, BorderLayout.CENTER);
setVisible(true);
validate();
}
} } /**
* dfs的方向
*/
int dr[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
/**
* dfs的方向
*/
int dc[] = { -1, 0, 1, -1, 1, -1, 0, 1 }; /**
* 深度优先搜索
* @param x 行坐标
* @param y 列坐标
*/
public void dfs(int x, int y) { vis[x][y] = true;
blockView[x][y].seeBlockName(); for (int i = 0; i < 8; i++) {
int dx = x + dr[i];
int dy = y + dc[i]; if (dx >= 0 && dx < row && dy >= 0 && dy < colum && !block[dx][dy].isMine() && !vis[dx][dy]) {
if (block[dx][dy].getNumber() == 0) {
dfs(dx, dy);
} else {
vis[dx][dy] = true;
blockView[dx][dy].seeBlockName();
}
}
} } /**
* 判断是否胜利
*/
void win() {
int flag = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
if (block[i][j].isMine()==true&&isFlag[i][j] == true)
flag ++;
}
}
if (flag == mineCount) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
blockView[i][j].getBlockCover().removeActionListener(this);
blockView[i][j].getBlockCover().removeMouseListener(this);
}
}
JOptionPane.showMessageDialog(this, "小朋友,恭喜你胜利了啊"); } if (flagNum>mineCount) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
blockView[i][j].getBlockCover().removeActionListener(this);
blockView[i][j].getBlockCover().removeMouseListener(this);
}
}
JOptionPane.showMessageDialog(this, "小朋友,你违规了,你的小红旗数目太多了!");
} } /**
* 鼠标右击插上红旗
*/
@Override
public void mouseClicked(MouseEvent e) { // 鼠标单击
JButton button = (JButton) e.getSource(); if (e.getButton() == MouseEvent.BUTTON3) { // 右击鼠标插上红旗在JButton上,不能是JLabel上,不然就给出答案了;
button.setIcon(iconflag);
flagNum ++;
// isFlag[][]
int m=-1, n=-1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < colum; j++) {
if (button == blockView[i][j].getBlockCover()) {
m = i;
n = j;
break;
}
}
}
isFlag[m][n] = true;
button.removeMouseListener(this);
win();
} } @Override
public void mouseEntered(MouseEvent e) { // 鼠标进入 } @Override
public void mouseExited(MouseEvent e) { // 鼠标离开组件 } @Override
public void mousePressed(MouseEvent e) { // 鼠标按下 } @Override
public void mouseReleased(MouseEvent e) { // 鼠标释放 } /**
* 菜单退出的动作事件
* @author YinJian
*
*/
public class exitActionListen implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
} /**
* 菜单关于的动作时间
* @author YinJian
*
*/
public class AboutListen implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(mainFrame, "尹建制作", "关于扫雷", JOptionPane.PLAIN_MESSAGE);
}
} }

MineMainFrame

 package com.TreeDream.MyGame;

 /**
* 游戏创建
* @author YinJian
*
*/ public class Main { /**
* 主函数创建游戏
* @param args
*/
public static void main(String[] args) {
new MineMainFrame("winmine");
} }

Main

Java课程设计——扫雷(winmine)的更多相关文章

  1. Java课程设计(2019版)

    参考资料 Java课程设计参考资料(2018-12-26更新) Java课程设计常见问题(程序部署.数据库.JSP) 项目开发参考-阿里巴巴Java开发手册(正式版) 更多参考资料请查看QQ群文件中的 ...

  2. java(课程设计之记事本界面部分代码公布)

    代码:涉及记事本的一些界面......!! /* *java课程设计之记事本(coder @Gxjun) * 编写一个记事本程序 * 要求: * 用图形用户界面实现. * 能实现编辑.保存.另存为.查 ...

  3. java课程设计(计算器)

    JAVA课程 设 计 报 告 1206401-18   瞿杰 一.设计时间 2013年6月 24日-----6月28日 二.设计地点 湖南城市学院实验楼计算机506机房 三.设计目的 1.巩固学习VB ...

  4. Java课程设计——博客作业教学数据分析系统(201521123084 林正晟)

    #课程设计--博客作业教学数据分析系统(201521123084 林正晟) 1.团队课程设计博客链接 博客作业教学数据分析系统 2.个人负责模块或任务说明 学生登陆界面的前端实现和与数据库的连接 学生 ...

  5. Java课程设计——博客作业教学数据分析系统(201521123082 黄华林)

    Java课程设计--博客作业教学数据分析系统(201521123082 黄华林) 一.团队课程设计博客链接 博客作业教学数据分析系统(From:网络五条狗) 二.个人负责模块或任务说明 1.网络爬虫 ...

  6. java课程设计--We Talk(201521123061)

    java课程设计--We Talk(201521123061) 团队博客链接:http://www.cnblogs.com/slickghost/ 数据库 一.通过Dao模式建立与数据库的连接 1.数 ...

  7. Java 课程设计 "Give it up"小游戏(团队)

    JAVA课程设计 "永不言弃"小游戏(From :Niverse) 通过Swing技术创建游戏的登陆注册界面,使用mySQL数据库技术完成用户的各项信息保存和游戏完成后的成绩保存. ...

  8. Java课程设计----仿Windows标准型计算器

    JAVA课程设计 仿Windows标准型计算器(By Yanboooooooo) 一.团队介绍: 连燕波[组长]:网络1513学生. 张文博[组员]:网络1513学生. 二.项目git地址 码云项目地 ...

  9. Java课程设计—象棋

    1. 团队名称.团队成员介绍 团队名称:WY 团队成员: 吴慧婷[组长] 201521123094 网络1514 姚佳希 201521123042 网络1512 2 项目git地址 Java课程设计 ...

随机推荐

  1. angularJS实现二级联动查询以及自定义过滤器的使用

    <!DOCTYPE html><html lang="en"><head>    <meta http-equiv="Conte ...

  2. spring boot + gradle[草稿]

    入门文档:https://github.com/qibaoguang/Spring-Boot-Reference-Guide 安装gradle 官方下载 https://gradle.org/grad ...

  3. Android Studio: Failed to sync Gradle project 'xxx' Error:Unable to start the daemon process: could not reserve enough space for object heap.

    创建项目的时候报错: Failed to sync Gradle project 'xxx' Error:Unable to start the daemon process: could not r ...

  4. jQuery工具函数(转)

    原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/27/2835561.html 作者:逆心 ------------------------ ...

  5. ios 证书申请和发布流程

    证书是什么? 上面这个就是我们申请好证书后,下载到本地的.cer文件,也就是常说的开发证书与发布证书的样式.这.cer文件格式的证书是让开发者使用的设备(也就是你的Mac)有真机调试,发布APP的权限 ...

  6. kali driftnet

    语法 : driftnet   [options]   [filter code] 主要参数: -b               捕获到新的图片时发出嘟嘟声 -i  interface     选择监 ...

  7. PIXHAWK DIY LED扩展板

    板载的状态LED灯,因为各种灰机的外壳有可能会被挡住看不到状态.那么我们也是可以用arduino板子来扩展实现外置,其实就是用328P芯片来实现. 这程序支持WS2812B的全彩LED灯. 默认的信号 ...

  8. HDU-1257 导弹拦截系统 http://acm.hdu.edu.cn/showproblem.php?pid=1257

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  9. 第一部分:连接MYSQL数据库代码

    <?php $connec=mysql_connect("localhost","root","root") or die(" ...

  10. 夺命雷公狗-----React---17--事件常用的属性

    我们可以通过打印的方式将他打印出来看看,如下所示: <!DOCTYPE> <html> <head> <meta charset="utf-8&qu ...