package com.xz.sl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane; public class Saolei extends JFrame{ final int ROW = 20;
final int COL = 20;
final int LEI = 30;
JButton[][] buttons = new JButton[ROW][COL];
int[][] counts = new int[ROW][COL];
final int LCODE = 11; //雷的编码 Container container = new Container(); public Saolei() {
init(); } private void init() {
JButton restBtn = new JButton("重来");
restBtn.setOpaque(true);
restBtn.setBackground(Color.PINK);
restBtn.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<ROW;i++) {
for(int j=0;j<COL;j++) {
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.YELLOW);
counts[i][j] = 0; }
}
mailei();
jslsl();
}
}); setLayout(new BorderLayout());
add(restBtn,BorderLayout.NORTH);
add(container,BorderLayout.CENTER);
container.setLayout(new GridLayout(ROW,COL));
for(int i=0;i<ROW;i++) {
for(int j=0;j<COL;j++) {
JButton button = new JButton();
button.setMargin(new Insets(0, 0, 0, 0));
button.setBackground(Color.YELLOW);
button.setOpaque(true);
buttons[i][j] = button;
button.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if(button.equals(restBtn)) { }else {
int count = 0;
for(int i=0;i<ROW;i++) {
for(int j=0;j<COL;j++) {
if(button.equals(buttons[i][j])) {
count = counts[i][j];
//踩到雷了
if(count == LEI) {
clcl();
}else {
openCell(i,j);
checkWin();
}
return;
}
}
}
}
} private void checkWin() {
for(int i=0;i<ROW;i++) {
for(int j=0;j<COL;j++) {
//说明还没有赢
if(buttons[i][j].isEnabled() == true && counts[i][j] != LEI) {
return;
}
}
} JOptionPane.showMessageDialog(container, "恭喜您赢了..."); } private void openCell(int i,int j ) { //如果格子已经打开,直接返回
if(buttons[i][j].isEnabled() == false) {
return ;
} buttons[i][j].setText(counts[i][j]+"");
buttons[i][j].setEnabled(false);
buttons[i][j].setBackground(Color.CYAN); if(counts[i][j] == 0) {
//左上角
if(i > 0 && j > 0 && counts[i-1][j-1] != LEI) {
openCell(i-1, j-1);
} if(i > 0 && j > 0 && counts[i-1][j] != LEI) {
openCell(i-1, j);
} if(i > 0 && j < 19 && counts[i-1][j+1] != LEI) {
openCell(i-1, j+1);
} if(i > 0 && j > 0 && counts[i][j-1] != LEI) {
openCell(i, j-1);
} if(i > 0 && j < 19 && counts[i][j+1] != LEI) {
openCell(i, j+1);
} if(i < 19 && j > 0 && counts[i+1][j-1] != LEI) {
openCell(i+1, j-1);
} if(i < 19 && j > 0 && counts[i+1][j] != LEI) {
openCell(i+1, j);
} if(i < 19 && j < 19 && counts[i+1][j+1] != LEI) {
openCell(i+1, j+1);
}
}else {
buttons[i][j].setText(counts[i][j]+"");
}
} });
container.add(button);
}
}
mailei();
//计算周边的雷的数量
jslsl(); setVisible(true);
setTitle("扫雷游戏");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,700);
} private void jslsl() {
for(int i=0;i<ROW;i++) {
for(int j=0;j<COL;j++) {
int count = 0;
if(counts[i][j] == LEI) {
continue;
} //左上角
if(i > 0 && j > 0 && counts[i-1][j-1] == LEI) {
count++;
} if(i > 0 && j > 0 && counts[i-1][j] == LEI) {
count++;
} if(i > 0 && j < 19 && counts[i-1][j+1] == LEI) {
count++;
} if(i > 0 && j > 0 && counts[i][j-1] == LEI) {
count++;
} if(i > 0 && j < 19 && counts[i][j+1] == LEI) {
count++;
} if(i < 19 && j > 0 && counts[i+1][j-1] == LEI) {
count++;
} if(i < 19 && j > 0 && counts[i+1][j] == LEI) {
count++;
} if(i < 19 && j < 19 && counts[i+1][j+1] == LEI) {
count++;
} counts[i][j] = count;
// buttons[i][j].setText(counts[i][j]+" "); }
}
} private void clcl() {
for(int i=0;i<ROW;i++) {
for(int j=0;j<COL;j++) {
int c = counts[i][j];
if(c == LEI) {
buttons[i][j].setText("X");
buttons[i][j].setBackground(Color.RED);
buttons[i][j].setEnabled(false);
}else {
buttons[i][j].setText(c+"");
buttons[i][j].setEnabled(false);
}
}
}
} private void mailei() {
Random random = new Random();
int randRow,randCol;
for(int i=0;i<LEI;i++) {
randRow = random.nextInt(ROW);
randCol = random.nextInt(COL);
if(counts[randRow][randCol] == LEI) {
i--;
}else {
counts[randRow][randCol] = LEI;
// buttons[randRow][randCol].setText(LEI+"");
}
}
} public static void main(String[] args) {
new Saolei();
} }

以上代码纯属练习用,没有经过任何的封装,有兴趣的小伙伴可以自行封装一下哦。

Java版的扫雷游戏源码的更多相关文章

  1. 基于jQuery经典扫雷游戏源码

    分享一款基于jQuery经典扫雷游戏源码.这是一款网页版扫雷小游戏特效代码下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <center> <h1>j ...

  2. html5 canvas简易版捕鱼达人游戏源码

    插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...

  3. ios版弹珠游戏源码

    这个是我们比较喜欢玩的一直小游戏的,ios版弹珠游戏源码,该游戏源码来着IOS教程网其他网友提供上传的,大家可以了解一下吧. nore_js_op>     <ignore_js_op&g ...

  4. Android版的疯狂猜图游戏源码完整版分享

    这个游戏源码是在安装教程网那么分享过来的,Android版的疯狂猜图游戏源码完整版分享,也是本人之前很早以前发的一款游戏源码的,大家如果想了解一下,可以看看吧,不说多了,上一个图先吧.   > ...

  5. Java写的斗地主游戏源码

    源码下载在最后 我们的前年的课设要求做一个斗地主程序,当时正在愁如何做界面,当时刚好在学习C#,于是就用C#完成了这个程序.一方面,当时我C#功底还很差(其实现在也不怎么样),很多地方用了“笨办法”, ...

  6. jQuery网页版五子棋小游戏源码下载

    体验效果:http://hovertree.com/texiao/game/4/ 网页五子棋源代码: <!DOCTYPE html> <html> <head> & ...

  7. Java Swing打猎射击游戏源码

    代码如下 <font size="3">package Game; import java.awt.Graphics; import java.awt.Image; i ...

  8. HTML5小游戏源码收藏

    html5魅族创意的贪食蛇游戏源码下载 html5网页版打砖块小游戏源码下载 html5 3D立体魔方小游戏源码下载 html5网页版飞机躲避游戏源码下载 html5三国人物连连看游戏源码下载 js ...

  9. android版猜拳游戏源码分享

    android版猜拳游戏源码分享安卓版猜拳游戏源码,该文件中带有安装测试包的,这个游戏源码比较简单的,现在有两个代码,一个自定义VIEW的,一个就是普通的imageView图片,游戏非常适合一些新手的 ...

随机推荐

  1. css实现京东顶部导航条

    1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...

  2. kettle 执行 kjb 临时文件夹 /tmp permission denied 问题

    编写完的 kettle job (kjb文件) 放在服务器上执行的时候出现了奇怪的错误: # 执行 kjb ./kitchen.sh -file:/opt/code/ods/ods_inc.kjb # ...

  3. codefoces B - Phoenix and Beauty

    原题链接:https://codeforc.es/problemset/problem/1348/B 题意:告诉我们一个数组及其长度和k,判断是否可以构造一个新数组使得每K段长度和都相等. 思路:首先 ...

  4. 在 .NET Core 中使用 ViewConfig 调试配置

    介绍 .NET Core 中的配置包含了多个配置提供程序,包括了 appsettings.json,环境变量,命令行参数等,还有一些扩展的自定义提供程序,比如说 ApolloConfig,AgileC ...

  5. java进阶(41)--反射机制

    文档目录: 一.反射机制的作用 二.反射机制相关类 三.获取class的三种方式 四.通过反射实例化对象 五.通过读属性文件实例化对象 六.通过反射机制访问对象属性 七.通过反射机制调用方法 ---- ...

  6. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...

  7. Android+Java Web+MySQL实现登录注册

    1 前言&概述 这篇文章是基于此处文章的更新,更新了一些技术栈,更加贴近实际需要,以及修复了若干的错误. 这是一个前端Android+后端Java/Kotlin通过Servelt进行后台数据库 ...

  8. Spring Boot demo系列(四):Spring Web+Validation

    2021.2.24 更新 1 概述 本文主要讲述了如何使用Hibernate Validator以及@Valid/@Validate注解. 2 校验 对于一个普通的Spring Boot应用,经常可以 ...

  9. python进阶(17)协程

    协程 协程(Coroutine),又称微线程,纤程.(协程是一种用户态的轻量级线程)   作用:在执行 A 函数的时候,可以随时中断,去执行 B 函数,然后中断B函数,继续执行 A 函数 (可以自动切 ...

  10. 01-Verilog基本语法元素

    不知道能不能更新完,毕竟咱学校计院对硬件向来不太重视,现在对竞赛也不咋地重视了,也不加分,也没啥用.嘛,就随便写写玩玩吧. 一只狸无聊的时候对Verilog的业余描述笔记:以<Verilog数字 ...