网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  用java编写扫雷游戏--代码思想     
  文章作者:未知  文章来源:水木森林  
  查看:123次  录入:管理员--2007-11-17  
 
  import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*按扭类*/

class bomb extends jbutton
{

public int num_x,num_y; //第几号方块
public int bombroundcount; //周围雷数
public boolean isbomb; //是否为雷
public boolean isclicked; //是否被点击
public int bombflag; //探雷标记
public boolean isright; //是否点击右键

public bomb(int x,int y)
{
bombflag = 0;
num_x = x;
num_y = y;
bombroundcount = 0;
isbomb = false;
isclicked = false;
isright = false;
}
}
/*窗口及算法实现类*/

class mainbomb extends jframe implements actionlistener,mouselistener
{

public jtextfield text;
public label nowbomb,setbomb;
public int blocknum,bombnum; //当前方块数当前雷数
public icon icon_bomb = new imageicon("bomb.gif"); //踩雷
public icon icon_bomb_big = new imageicon("bomb_big.gif"); //踩雷标记
public icon icon_flag = new imageicon("flag.gif"); //雷标记
public icon icon_question = new imageicon("question.gif"); //疑惑是否有雷
public jbutton start = new jbutton(" 开始 ");
public panel menupamel = new panel();
public panel mainpanel = new panel();
public bomb[][] bombbutton;

/*界面设计*/

public mainbomb()
{
super("扫雷 aaron2004制作 2004.8 ");
blocknum = 64;
bombnum = 10;
container c=getcontentpane();
c.setbackground(color.gray);
c.setlayout(new borderlayout());
text=new jtextfield("10 ",3);
nowbomb = new label("当前雷数"+" "+bombnum+"");
setbomb= new label("设置地雷数");
start.addactionlistener(new actionlistener(){
public void actionperformed(actionevent e)
{
bombnum = integer.parseint(text.gettext().trim());
if(bombnum >= 10 && bombnum < 50 )
replay();
else
{
joptionpane msg = new joptionpane();
joptionpane.showmessagedialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}

}
} );
menupamel.add(setbomb);
menupamel.add(text);
menupamel.add(start);
menupamel.add(nowbomb);
c.add(menupamel,"north");

mainpanel.setlayout(new gridlayout( (int)math.sqrt(blocknum) , (int)math.sqrt(blocknum)) );
bombbutton=new bomb[ (int)math.sqrt(blocknum) ][];
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++)
{
bombbutton[ i ]=new bomb[ (int)math.sqrt(blocknum) ];
}
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++ )
for(int j = 0 ; j < (int)math.sqrt(blocknum) ; j++ )
{
bombbutton[ i ][ j ]=new bomb(i,j);
bombbutton[ i ][ j ].setforeground( color.gray);
bombbutton[ i ][ j ].addactionlistener(this);
bombbutton[ i ][ j ].addmouselistener(this);
}
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++ )
for(int j = 0 ; j < (int)math.sqrt(blocknum) ; j++ )
mainpanel.add(bombbutton[ i ][ j ]);
c.add(mainpanel,"center");
startbomb();
setsize(400,400);
setlocation(350,200);
setresizable(false);
}

/*布雷*/

public void startbomb()
{

for(int i=0;i<bombnum;i++)
{
int x =(int)(math.random()*(int)(math.sqrt(blocknum)-1));
int y =(int)(math.random()*(int)(math.sqrt(blocknum)-1));

if(bombbutton[ x ][ y ].isbomb==true)
i--;
else
bombbutton[ x ][ y ].isbomb=true ;
}
}

/*重新开始*/

public void replay()
{
nowbomb.settext("当前雷数"+" "+bombnum+"");
for(int i = 0 ; i < (int)math.sqrt(blocknum) ; i++)
for(int j = 0 ; j < (int)math.sqrt(blocknum) ; j++)
{
bombbutton[ i ][ j ].isbomb=false;
bombbutton[ i ][ j ].isclicked=false;
bombbutton[ i ][ j ].setenabled(true);
bombbutton[ i ][ j ].settext("");
bombbutton[ i ][ j ].seticon(null);
}
startbomb();
}

/*是否挖完了所有的雷*/

public void iswin()
{
int findbomb=0; //找到的地雷数

for(int i = 0;i < (int)math.sqrt(blocknum) ; i++)
for(int j = 0;j < (int)math.sqrt(blocknum ); j++)
{
if(bombbutton[ i ][ j ].isbomb == true && bombbutton[ i ][ j ].isright == true)
findbomb++;
}
if( findbomb == integer.parseint(text.gettext().trim()) )
{
joptionpane msg = new joptionpane();
joptionpane.showmessagedialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);
}
}

/*计算方块周围雷数 */

public void countroundbomb()
{
for (int i = 0; i < (int)math.sqrt(blocknum); i++) {
for (int j = 0; j < (int)math.sqrt(blocknum); j++) {
int count = 0;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombbutton[ i ][ j ].isbomb != true) {
if ( (i - 1 >= 0) && (j - 1 >= 0)) {
if (bombbutton[i - 1][j - 1].isbomb == true) {
count += 1; //检测左上方空格是否是地雷
}
}
if ( (i - 1 >= 0)) {
if (bombbutton[i - 1][ j ].isbomb == true) {
count += 1; //检测上方空格是否为地雷
}
}
if ( (i - 1 >= 0) && (j + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[i - 1][j + 1] .isbomb == true) {
count += 1; //检测右上方是否为地雷
}
}
if ( (j - 1 >= 0)) {
if (bombbutton[ i ][j - 1] .isbomb == true) {
count += 1; //检测左边是否为地雷
}
}
if ( (i >= 0) && (j + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[ i ][j + 1].isbomb == true) {
count += 1; //右边
}
}
if ( (j - 1 >= 0) && (i + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[i + 1][j - 1].isbomb == true) {
count += 1; //左下
}
}
if ( (i + 1 <= (int)math.sqrt(blocknum)-1)) {
if (bombbutton[i + 1][ j ].isbomb == true) {
count += 1; //下
}
}
if ( (j + 1 <= (int)math.sqrt(blocknum)-1) && (i + 1 <= math.sqrt(blocknum)-1)) {
if (bombbutton[i + 1][j + 1].isbomb == true) {
count += 1; //右下
}
}
bombbutton[ i ][ j ].bombroundcount = count;
}
}
}
}

/**当选中的位置为空,则翻开周围的地图**/

public void isnull(bomb[][] bombbutton,bomb clickecbutton)
{
int i,j;
i=clickecbutton.num_x;
j=clickecbutton.num_y;

if (clickecbutton.isbomb==true) {

}
else {

if ( (i - 1 >= 0) && (j - 1 >= 0)) { //检测左上方空格是否是空
if (bombbutton[i - 1][j - 1].isbomb == false && bombbutton[i - 1][j - 1].isclicked == false && bombbutton[i - 1][j - 1].isright == false) {
bombbutton[i - 1][j - 1].settext((bombbutton
 
 
上一篇: 用java编写的记事本程序(3)    下一篇: 用java插件在浏览器中运行java2 applet
  相关文档
j2se综合--浅析实现排列组合查询算法 01-30
用struts框架开发国际化程序的设计思路 11-16
自动化weblogic平台应用程序供应 11-17
netbeans rcp - 添加/修改netbeans的jvm启动参数 11-17
java的网络功能与编程4 11-17
servlet技术及其与cgi的比较 11-17
区分过载方法 11-17
探索string 类 11-17
基础教程:java代码编写程序的基本规范 11-16
javaapplet编程响应鼠标键盘 11-17
精通hibernate之映射继承关系三 11-17
j2ee综合--关于权限设计的详细探讨 02-02
技术分享——开发eclipse自定义控件 11-17
用javabean数据组件集成jsf应用程序与ejb(3) 11-17
分析并修补 resin 的中文处理 11-17
java新手入门:使用eclipse的插件开发 11-16
浅析java多线程程序设计机制 11-16
在java中实现c++代码的复用 11-17
java servlet api说明文档(2.1a版)(二) 11-17
技术前沿:网格帮助soa扩大规模 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息