java实现简单扫雷小游戏

 更新时间:2020年04月22日 16:25:20   作者:tf1997  
这篇文章主要为大家详细介绍了java实现简单扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现扫雷游戏的具体代码,供大家参考,具体内容如下

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
 
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
public class SaoLei implements MouseListener,ActionListener{
 JPanel p=new JPanel();
 JFrame frame = new JFrame("扫雷");
 @SuppressWarnings("rawtypes")
 JComboBox combobox = new JComboBox();
 JButton reset = new JButton("重新开始");
 Container container = new Container();
 
 //游戏数据结构
 SaoLeiConstant constant = new SaoLeiConstant();
 JButton[][] buttons = new JButton[constant.row][constant.col];//定义按钮
 int[][] counts = new int [constant.row][constant.col];//定义整型数组保存按钮下方雷数
 
 //创建构造方法
 public SaoLei() {
 //显示窗口
 frame.setSize(600,700);//600*700
 frame.setResizable(false);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setLayout(new BorderLayout());
 
 //添加重来、选择难度按钮
 addtopButton();
 
 //添加雷区按钮
 addButtons();
 
 //埋雷
 addLei();
 
 //添加雷的计数
 calcNeiboLei();
 
 frame.setVisible(true);
 }
 
 void addtopButton() {
 p.removeAll();
 p.add(reset);
 reset.setBackground(Color.green);
 reset.setOpaque(true);
 reset.addActionListener(this);
 //combobox.addItem("选择难度");
 combobox.addItem("新手难度");
 combobox.addItem("初级难度");
 combobox.addItem("中级难度");
 combobox.addItem("高级难度");
 combobox.addItem("大师难度");
 combobox.setBackground(Color.GREEN);
 combobox.setOpaque(true);
 combobox.addItemListener(new ItemListener(){
 
 @Override
 public void itemStateChanged(ItemEvent e) {
 String item = e.getItem().toString(); 
 if(item == "新手难度") {
 constant.leiCount = 20;
 ResetGame();
 } else if(item == "初级难度") {
 constant.leiCount = 43;
 ResetGame();
 } else if(item == "中级难度"){
 constant.leiCount = 63;
 ResetGame();
 } else if(item == "高级难度"){
 constant.leiCount = 99;
 ResetGame();
 } else if(item == "大师难度") {
 constant.leiCount = 119;
 ResetGame();
 }
 
 }
 
 });
 p.add(combobox);
 frame.add(p,BorderLayout.NORTH);
 //p.add(new Label("总雷数:"+constant.leiCount,Label.CENTER));
 //p.add(new Label("总雷数:"+constant.leiCount,Label.RIGHT));
 
 }
 
 
 /*
 void addnanduButton() {
 nandu.setBackground(Color.green);
 nandu.setOpaque(true);
 nandu.addActionListener(this);
 frame.add(nandu,BorderLayout.WEST);
 }
 
 void addResetButton() {
 reset.setBackground(Color.green);
 reset.setOpaque(true);
 reset.addActionListener(this);
 //reset.addMouseListener(this);
 frame.add(reset,BorderLayout.NORTH); 
 }
 */
 
 void addLei() {
 Random rand = new Random();
 int randRow,randCol;
 for(int i=0; i<constant.leiCount; i++) {
 randRow = rand.nextInt(constant.row);
 randCol = rand.nextInt(constant.col);
 if(counts[randRow][randCol] == constant.LEICODE) {
 i--;
 } else {
 counts[randRow][randCol] = constant.LEICODE;
 //buttons[randRow][randCol].setText("X");
 }
 }
 }
 
 void addButtons() {
 frame.add(container,BorderLayout.CENTER);
 container.setLayout(new GridLayout(constant.row,constant.col));
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 JButton button = new JButton();
 button.setBackground(Color.white);
 button.setOpaque(true);
 button.addActionListener(this);
 button.addMouseListener((MouseListener) this);
 buttons[i][j] = button;
 container.add(button);
 }
 }
 }
 
 void calcNeiboLei() {
 int count;
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 count =0;
 if(counts[i][j] == constant.LEICODE) continue;
 if(i>0 && j>0 && counts[i-1][j-1] == constant.LEICODE) count++;
 if(i>0 && counts[i-1][j] == constant.LEICODE) count++;
 if(i>0 && j<19 &&counts[i-1][j+1] == constant.LEICODE) count++; 
 if(j>0 && counts[i][j-1] == constant.LEICODE) count++;
 if(j<19 && counts[i][j+1] == constant.LEICODE) count++;
 if(i<19 && j>0 && counts[i+1][j-1] == constant.LEICODE) count++;
 if(i<19 && counts[i+1][j] == constant.LEICODE) count++;
 if(i<19 && j<19 && counts[i+1][j+1] == constant.LEICODE) count++;
 counts[i][j] = count;
 buttons[i][j].setMargin(new Insets(0,0,0,0));//让按钮随按钮上的图案变化
 //buttons[i][j].setText(counts[i][j] + "");
 }
 }
 }
 
 @Override
 public void actionPerformed(ActionEvent e) {
 
 JButton button = (JButton)e.getSource();
 if(button.equals(reset)) {
 ResetGame();//重新开始游戏
 } else {
 int count = 0;
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(button.equals(buttons[i][j])) {
 count = counts[i][j];
 if(count == constant.LEICODE) {
 loseGame();
 } else {
 openCell(i,j); 
 checkWin();
 }
 return;
 } 
 }
 }
 }
 }
 
 public void mouseClicked(MouseEvent e) {
 JButton button = (JButton)e.getSource();
 if (e.getButton() == MouseEvent.BUTTON3) {//判断鼠标右击动作
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(button.equals(buttons[i][j])) {
 if((buttons[i][j].isEnabled() == true)) {
 //buttons[i][j].setEnabled(false);
 buttons[i][j].setMargin(new Insets(0,0,0,0));//让按钮随按钮上的图案变化
 buttons[i][j].setText("?");
 return;
 } 
 }
 }
 } 
 }
 }
 
 void ResetGame() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 buttons[i][j].setText("");
 buttons[i][j].setEnabled(true);
 buttons[i][j].setBackground(Color.white);
 counts[i][j] = 0; 
 } 
 }
 addLei();
 calcNeiboLei(); 
 }
 
 void checkWin() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(buttons[i][j].isEnabled() == true && counts[i][j] != constant.LEICODE ) return; 
 }
 }
 JOptionPane.showMessageDialog(frame,"Yeah,你赢了!"); 
 }
 
 //使用递归方法打开格子
 void openCell(int i, int j) {
 if(buttons[i][j].isEnabled() == false) return;
 buttons[i][j].setBackground(Color.yellow);
 buttons[i][j].setOpaque(true);
 buttons[i][j].setEnabled(false);
 if(counts[i][j] == 0) {
 if(i>0 && j>0 && counts[i-1][j-1] != constant.LEICODE) openCell(i-1,j-1);
 if(i>0 && j<19 && counts[i-1][j] != constant.LEICODE) openCell(i-1,j);
 if(i>0 && j<19 &&counts[i-1][j+1] != constant.LEICODE) openCell(i-1,j+1); 
 if(j>0 && counts[i][j-1] != constant.LEICODE) openCell(i,j-1);
 if(j<19 && counts[i][j+1] != constant.LEICODE) openCell(i,j+1);
 if(i<19 && j>0 && counts[i+1][j-1] != constant.LEICODE) openCell(i+1,j-1);
 if(i<19 && counts[i+1][j] != constant.LEICODE) openCell(i+1,j);
 if(i<19 && j<19 && counts[i+1][j+1] != constant.LEICODE) openCell(i+1,j+1);
 } else {
 buttons[i][j].setMargin(new Insets(0,0,0,0));
 buttons[i][j].setText(counts[i][j] + "");
 }
 }
 
 void loseGame() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 int count = counts[i][j];
 if(count == constant.LEICODE) {
 buttons[i][j].setMargin(new Insets(0,0,0,0));
 buttons[i][j].setText("雷");
 buttons[i][j].setBackground(Color.red);
 buttons[i][j].setEnabled(false);
 } else {
 buttons[i][j].setMargin(new Insets(0,0,0,0));
 buttons[i][j].setText(count + "");
 buttons[i][j].setEnabled(false);
 
 }
 }
 }
 JOptionPane.showMessageDialog(frame,"error,你输了!");
 }
 
 public static void main(String[] args) {
 new SaoLei();
 }
 
 @Override
 public void mousePressed(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseReleased(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseEntered(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseExited(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
}

常量类

public class SaoLeiConstant {
 final int row = 20;//行数30
 final int col = 20;//列数30
 final int LEICODE = 10;//定义雷下方的数字
 protected int temp = 20;
 protected int leiCount = temp;//雷数30
}

效果图

更多精彩游戏,请参考专题《java经典小游戏》

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java SpringBoot自动装配原理详解

    Java SpringBoot自动装配原理详解

    这篇文章主要介绍了详解Spring Boot自动装配的原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-09-09
  • 手把手教你设置IntelliJ IDEA 的彩色代码主题的图文教程

    手把手教你设置IntelliJ IDEA 的彩色代码主题的图文教程

    本文给出一系列 IntelliJ IDEA 代码的彩色主题,感兴趣的朋友一起看看吧
    2018-01-01
  • Java SPI简单应用案例详解

    Java SPI简单应用案例详解

    这篇文章主要介绍了Java SPI简单应用案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java实现贪吃蛇大作战小游戏的示例代码

    Java实现贪吃蛇大作战小游戏的示例代码

    本文主要介绍了Java实现贪吃蛇大作战小游戏的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • java解析xml之sax解析xml示例分享

    java解析xml之sax解析xml示例分享

    SAX基于事件的解析,解析器在一次读取XML文件中根据读取的数据产生相应的事件,由应用程序实现相应的事件处理逻辑,即它是一种“推”的解析方式;这种解析方法速度快、占用内存少,但是它需要应用程序自己处理解析器的状态,实现起来会比较麻烦
    2014-01-01
  • Java Lambda表达式和函数式接口实例分析

    Java Lambda表达式和函数式接口实例分析

    这篇文章主要介绍了Java Lambda表达式和函数式接口,结合实例形式分析了Java8 Lambda表达式和函数式接口相关原理、用法及操作注意事项,需要的朋友可以参考下
    2019-09-09
  • Java使用Ehcache缓存框架的技术指南

    Java使用Ehcache缓存框架的技术指南

    Ehcache 是 Java 平台下一个开源、高性能的分布式缓存框架,常用于提高系统性能和可扩展性,它能够帮助开发者缓存频繁访问的数据,从而减少对数据库和其他持久化存储的访问压力,本文给大家介绍了Java使用Ehcache缓存框架的技术指南,需要的朋友可以参考下
    2025-03-03
  • Java数据结构之二叉排序树的实现

    Java数据结构之二叉排序树的实现

    二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),亦称二叉搜索树。本文详细介绍了二叉排序树的原理,并且提供了Java代码的完全实现。需要的可以参考一下
    2022-01-01
  • Spring MVC学习之DispatcherServlet请求处理详析

    Spring MVC学习之DispatcherServlet请求处理详析

    这篇文章主要给大家介绍了关于Spring MVC学习教程之DispatcherServlet请求处理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2018-11-11
  • springboot获取URL请求参数的多种方式

    springboot获取URL请求参数的多种方式

    这篇文章主要介绍了springboot获取URL请求参数的多种方式,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-01-01

最新评论