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经典小游戏》

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

(0)

相关推荐

  • Java版AI五子棋游戏

    本文实例为大家分享了java五子棋游戏的具体代码,供大家参考,具体内容如下 AI思路:通过判断棋盘上每个空位的分数,去分数最高的几个点,随机下棋 分数计算思路:能成5个说明能赢了,给最高分 不能成5个,对方能成5个,说明对方要赢了,给第二高分 能成活4,给第三高分 能成活3,给第四高分 能成冲4,给第五高分 能成冲3,给第六高分 能成活2,给第七高分 能成冲2,给第八高分 其他,给最低分 分数设定可自己定义. 因为是去年写的了,思路记得大概就是这样.最近根据书上写了个棋类游戏的设计框架,待完善后

  • java实现flappy Bird小游戏

    本文实例为大家分享了java实现flappy Bird游戏的具体代码,供大家参考,具体内容如下 整个游戏由3个类构成.Bird类,Pipe类,stage类 第一步:首先写一个Bird类 //鸟类 public class Bird { private int flyHeight;//飞行高度 private int xpos;//距离y轴(窗口左边缘)的位置, public static int Up=1;//向上飞 public static int Down=-1;//向下飞 public

  • Java实现的打地鼠小游戏完整示例【附源码下载】

    本文实例讲述了Java实现的打地鼠小游戏.分享给大家供大家参考,具体如下: 这里涉及到java线程和GUI的相关知识,一个简单的java小游戏打地鼠,有兴趣的朋友可以优化一下.先来看看运行效果: 具体代码: Mouse.java: import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt

  • java连连看游戏菜单设计

    本文实例为大家分享了java连连看游戏菜单的具体实现代码,供大家参考,具体内容如下 先写GUI. 首先初始化框架,菜单,按钮,需要把菜单和按钮都添加在框架中.注意添加的顺序,首先要设置菜单,再设置框架,再设置按钮,如果交换了设置菜单和框架的顺序,会导致菜单显示不出,被框架挡住.对菜单设置了三个选项,第一个选项有五个下拉按键,用循环添加,第二个和第三个选项的下拉按键直接添加. GUI代码如下: package gui; import java.awt.Font; import javax.swin

  • java实现24点游戏

    游戏规则 从扑克中每次取出4张牌.使用加减乘除,第一个能得出24者为赢.(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏. 基本要求 随机生成4个代表扑克牌牌面的数字字母,程序自动列出所有可能算出24的表达式 列出表达式无重复 用户初始生命值为一给定值(比如3),初始分数为0.随机生成4个代表扑克牌牌面的数字或字母,由用户输入包含这4个数字或字母的运算表达式(可包含括号),如果表达式计算结果为24则代表用户赢了此局. 使用计时器要求用户在规定时间内输入表达式,如

  • java实现猜拳游戏

    本文实例为大家分享了java实现猜拳游戏的具体代码,供大家参考,具体内容如下 package com.farsight.session7; import java.util.Scanner; /** * 根据输入的数字,判断数组strs的值 然后进行逻辑判断 */ public class 猜拳 { public static void main(String[] args) { String[] strs = { "石头", "剪刀", "布"

  • 小伙熬夜用Java重现经典超级马里奥代码实例

    说起Java,很多人都想问,Java这么火但是Java能干什么呢?Java主要是做企业级的应用开发,其实Java还可以做游戏开发,Java不仅开发过电脑端的游戏,手游也有开发过,经典的游戏有很多,比如<我的世界>.<吃豆>等等,今天小编来分享一个用Java开发超级马里奥. 游戏界面展示: 游戏代码如下: 以上所述是小编给大家介绍的Java重现经典超级马里奥详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的.在此也非常感谢大家对我们网站的支持!

  • java实现斗地主游戏

    感想: 第一次写博客,感觉编辑器挺复杂厉害的,感觉自己的内容挺简单的.有什么问题请多多指教! 思路: 1.创建一个扑克牌的实体类Poker,设置了四个参数:花色.数字.牌值(判断大小).是否地主牌,实现getset方法和构造方法: 2.创建一个玩家的实体类Player,设置了四个参数: 初始牌集合,排序后牌集合,牌值集合,是否地主,实现getset方法和构造方法: 3.洗牌:循环嵌套花色数组跟数字数组生成52个Poker,手动加入大小王Poker,放进map(int,Poker)里面,利用Col

  • Java Swing实现扫雷小游戏

    swing设计扫雷心得,供大家参考,具体内容如下 最近学习swing学习之余做了一个小游戏:扫雷 1.前期设计 2.实现 其实完成这个游戏的核心就在于对数组的操纵,下面贴主要代码Main.java: package first; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.Image; import java.

  • Java实现Flappy Bird游戏源码

    本文实例为大家分享了Java实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下 1.首先在mainActivity.xml中放置一个View,ID为viewDraw 2.开始编程,程序中自定义一个View类的子类,与viewDraw关联,程序除了放置如一个view控件,没有其他控件,程序上面的所有图片都是通过控制canvas画图实现 3.游戏是依据flappybird的游戏过程重新写的算法,功能与原版游戏相似,可能有些地方不足,完全是学习练习编程而已,做的不好见谅 4.原图片大小

随机推荐