欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现简单扫雷小游戏

程序员文章站 2023-12-20 11:08:34
本文实例为大家分享了java实现扫雷游戏的具体代码,供大家参考,具体内容如下 import java.awt.borderlayout; import 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经典小游戏》

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

上一篇:

下一篇: