| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1081 人关注过本帖
标题:挖地雷的小程序,reset不知怎么编写了
只看楼主 加入收藏
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
 问题点数:0 回复次数:8 
挖地雷的小程序,reset不知怎么编写了

import java.awt.*;
import java.awt.event.*;
import java.math.*;
import javax.swing.*;
import java.util.Random;

public class Main extends JFrame implements ActionListener
{
public class QI extends MouseAdapter implements ActionListener
{
public JPanel v = new JPanel();
public JButton but[] = new JButton[4];
public CardLayout card = new CardLayout();
public void QI() {}
public int x=0;
public int y=0;
public void ginit(int x,int y)
{
this.x=x; this.y=y;
but[0] = new JButton("");
but[1] = new JButton("雷");
but[2] = new JButton("旗");
but[3] = new JButton(Integer.toString(map[x][y]));
card = new CardLayout();
v = new JPanel();
v.setLayout(card);
for(int k=0;k<4;k++)
v.add(but[k],Integer.toString(k));
but[0].addActionListener(this);
but[2].addActionListener(this);
but[0].addMouseListener(this);
but[2].addMouseListener(this);
card.show(v,"0");
}
public void mouseClicked(MouseEvent evt)
{
if(evt.isMetaDown())
{
if(evt.getComponent()==but[0])
{
card.show(v,"2"); //插红旗
u[x][y]=true;
sum++;
}
else if(evt.getComponent()==but[2])
{
card.show(v,"0");
u[x][y]=false;
sum--;
return;
}
}
}
public void actionPerformed(ActionEvent e)
{

if(e.getSource()==but[0])
{
if(bflag)
{
card.show(v,"2"); //插红旗
u[x][y]=true;
sum++;
}
else if(u[x][y]) return;
else if(lei[x][y])
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
if(lei[i][j]) qi[i][j].card.show(qi[i][j].v,"1");
else qi[i][j].card.show(qi[i][j].v,"0");
u[i][j]=true;
}
showfield.setText("YOU LOST");
return;
}
else
{
if(map[x][y]!=0 )
{
card.show(v,"3");
u[x][y]=true;
sum++;
}
else bfs();
}
}
else if(e.getSource()==but[2] && bflag) //拔旗
{
card.show(v,"0");
u[x][y]=false;
sum--;
return;
}
if(sum==M*M) showfield.setText("YOU WIN");
}
public void bfs()
{
int head=0,tail=0;
que[tail][0]=x;
que[tail++][1]=y;
qi[x][y].card.show(v,"3");
u[x][y]=true;
sum++;
while(head<tail)
{
int qx=que[head][0];
int qy=que[head++][1];
for(int k=0;k<4;k++)
{
int xx=qx+dxy[k][0];
int yy=qy+dxy[k][1];
System.out.println(xx+" * "+yy+":");
if(xx>=0 && xx<M && yy>=0 && yy<M
&& !u[xx][yy] && !lei[xx][yy] )
{
if(map[xx][yy]==0)
{
que[tail][0]=xx;
que[tail++][1]=yy;
}
System.out.println(xx+" ** "+yy+":");
System.out.println(map[xx][yy]);
qi[xx][yy].card.show(qi[xx][yy].v,"3");
u[xx][yy]=true;
sum++;
}

}
}
}
}

final int dxy[][]= {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{1,1},{1,-1},{-1,-1}};
final int M = 8;
final int LeiNum = 10;

QI qi[][] = new QI[M][M];
JButton flag = new JButton("FLAG");
JTextField showfield=new JTextField(10);
boolean bflag = false;

boolean u[][]=new boolean[M][M];
int que[][]=new int[M*M][2];
int map[][]=new int[M][M];
boolean lei[][]=new boolean[M][M];
public int sum=0;

private void initcompute()
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
u[i][j] = false;
lei[i][j] = false;
map[i][j] = 0;
}

for(int i=0;i<LeiNum;i++)
{

do
{
int xx=(int)(Math.random()*M);
int yy=(int)(Math.random()*M);
// System.out.println(xx+","+yy+" ");
if(lei[xx][yy]) continue;
lei[xx][yy]=true;
break;
}while(true);
}
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
for(int k=0;k<8;k++)
{
int xx,yy;
xx=i+dxy[k][0];
yy=j+dxy[k][1];
if(xx>=0 && xx<M && yy>=0 && yy<M && lei[xx][yy])
{
map[i][j]++;
}
}
}

}

public JButton reset = new JButton("RESET");
public void setmenu()
{
MenuBar bar = new MenuBar();
Menu file = new Menu("File");
setMenuBar(bar);
MenuItem help = new MenuItem("Help");
file.add(help);
bar.add(file);
}
public Main()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initcompute();
setmenu();
Container c = this.getContentPane();
JPanel cen = new JPanel();
cen.setLayout(new GridLayout(M,M));
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
qi[i][j] = new QI();
qi[i][j].ginit(i,j);
cen.add(qi[i][j].v);
}

c.add(cen,BorderLayout.CENTER);
flag.addActionListener(this);
JPanel sou=new JPanel();
sou.setLayout(new FlowLayout());
reset.addActionListener(this);
sou.add(reset);
// sou.add(flag);
showfield.setText("挖地雷");
sou.add(showfield);
c.add(sou,BorderLayout.SOUTH);

}
public static void main(String args[])
{
Main mainFrame = new Main();
mainFrame.setSize(500,530);
mainFrame.setTitle("挖地雷");
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==flag)
{
bflag=!bflag;
if(!bflag) showfield.setText("挖地雷");
else showfield.setText("插红旗");
}
else
{

}
}
}

搜索更多相关主题的帖子: public import 地雷 reset java 
2006-12-10 13:55
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
得分:0 
如何从容器中删除按钮呢
2006-12-10 13:56
一二三四五
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:856
专家分:0
注 册:2006-11-13
收藏
得分:0 
不知道怎么写,就从简单的地方开始写
不要一开始就写自己不能完成的任务

hey,di va la
2006-12-10 14:35
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
得分:0 
晕了,就差这一点就写完了。
2006-12-10 21:43
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
得分:0 
运行一下,已经写的差不多啦,能玩的。主要问题是如何在卡片布局中把一个按钮删去,就可以重起拉。那个API是什么呢
2006-12-10 22:16
☆王者至尊☆
Rank: 1
等 级:新手上路
威 望:1
帖 子:43
专家分:0
注 册:2006-6-21
收藏
得分:0 

清空panel 或者 重新设置按钮背景


2006-12-10 23:11
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
得分:0 

//终于搞定啦,感谢purana和yqfang310提供的remove()函数,大家运行一下,看看还有什么需要改的高数我。
import java.awt.*;
import java.awt.event.*;
import java.math.*;
import javax.swing.*;
import java.util.Random;

public class Main extends JFrame implements ActionListener
{
public class QI extends MouseAdapter implements ActionListener
{
public JPanel v = new JPanel();
public JButton but[] = new JButton[4];
public CardLayout card = new CardLayout();
public void QI() {}
public int x=0;
public int y=0;
public void ginit(int x,int y)
{
this.x=x; this.y=y;
but[0] = new JButton("");
but[1] = new JButton("雷");
but[2] = new JButton("旗");
but[3] = new JButton(Integer.toString(map[x][y]));
card = new CardLayout();
v = new JPanel();
v.setLayout(card);
for(int k=0;k<4;k++)
v.add(but[k],Integer.toString(k));
but[0].addActionListener(this);
but[2].addActionListener(this);
but[0].addMouseListener(this);
but[2].addMouseListener(this);
card.show(v,"0");
}
public void mouseClicked(MouseEvent evt)
{
if(evt.isMetaDown())
{
if(evt.getComponent()==but[0])
{
card.show(v,"2"); //插红旗
u[x][y]=true;
sum++;
}
else if(evt.getComponent()==but[2])
{
card.show(v,"0");
u[x][y]=false;
sum--;
return;
}
}
}
public void actionPerformed(ActionEvent e)
{

if(e.getSource()==but[0])
{
if(bflag)
{
card.show(v,"2"); //插红旗
u[x][y]=true;
sum++;
}
else if(u[x][y]) return;
else if(lei[x][y])
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
if(lei[i][j]) qi[i][j].card.show(qi[i][j].v,"1");
else qi[i][j].card.show(qi[i][j].v,"0");
u[i][j]=true;
}
showfield.setText("YOU LOST");
return;
}
else
{
if(map[x][y]!=0 )
{
card.show(v,"3");
u[x][y]=true;
sum++;
}
else bfs();
}
}
else if(e.getSource()==but[2] && bflag) //拔旗
{
card.show(v,"0");
u[x][y]=false;
sum--;
return;
}
if(sum==M*M) showfield.setText("YOU WIN");
}
public void bfs()
{
int head=0,tail=0;
que[tail][0]=x;
que[tail++][1]=y;
qi[x][y].card.show(v,"3");
u[x][y]=true;
sum++;
while(head<tail)
{
int qx=que[head][0];
int qy=que[head++][1];
for(int k=0;k<4;k++)
{
int xx=qx+dxy[k][0];
int yy=qy+dxy[k][1];
System.out.println(xx+" * "+yy+":");
if(xx>=0 && xx<M && yy>=0 && yy<M
&& !u[xx][yy] && !lei[xx][yy] )
{
if(map[xx][yy]==0)
{
que[tail][0]=xx;
que[tail++][1]=yy;
}
System.out.println(xx+" ** "+yy+":");
System.out.println(map[xx][yy]);
qi[xx][yy].card.show(qi[xx][yy].v,"3");
u[xx][yy]=true;
sum++;
}

}
}
}
}

final int dxy[][]= {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{1,1},{1,-1},{-1,-1}};
final int M = 8;
final int LeiNum = 10;

QI qi[][] = new QI[M][M];
JButton flag = new JButton("FLAG");
JTextField showfield=new JTextField(10);
boolean bflag = false;

boolean u[][]=new boolean[M][M];
int que[][]=new int[M*M][2];
int map[][]=new int[M][M];
boolean lei[][]=new boolean[M][M];
public int sum=0;

private void initcompute()
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
u[i][j] = false;
lei[i][j] = false;
map[i][j] = 0;
}

for(int i=0;i<LeiNum;i++)
{

do
{
int xx=(int)(Math.random()*M);
int yy=(int)(Math.random()*M);
// System.out.println(xx+","+yy+" ");
if(lei[xx][yy]) continue;
lei[xx][yy]=true;
break;
}while(true);
}
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
for(int k=0;k<8;k++)
{
int xx,yy;
xx=i+dxy[k][0];
yy=j+dxy[k][1];
if(xx>=0 && xx<M && yy>=0 && yy<M && lei[xx][yy])
{
map[i][j]++;
}
}
}

}

public JButton reset = new JButton("RESET");
public void setmenu()
{
MenuBar bar = new MenuBar();
Menu file = new Menu("File");
setMenuBar(bar);
MenuItem help = new MenuItem("Help");
file.add(help);
bar.add(file);
}
public Main()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initcompute();
setmenu();
Container c = this.getContentPane();
JPanel cen = new JPanel();
cen.setLayout(new GridLayout(M,M));
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
qi[i][j] = new QI();
qi[i][j].ginit(i,j);
cen.add(qi[i][j].v);
}

c.add(cen,BorderLayout.CENTER);
flag.addActionListener(this);
JPanel sou=new JPanel();
sou.setLayout(new FlowLayout());
reset.addActionListener(this);
sou.add(reset);
// sou.add(flag);
showfield.setText("挖地雷");
sou.add(showfield);
c.add(sou,BorderLayout.SOUTH);

}
public static void main(String args[])
{
Main mainFrame = new Main();
mainFrame.setSize(500,530);
mainFrame.setTitle("挖地雷");
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==flag)
{
bflag=!bflag;
if(!bflag) showfield.setText("挖地雷");
else showfield.setText("插红旗");
}
else
{
initcompute();
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
{
qi[i][j].but[3].setText(Integer.toString(map[i][j]));
qi[i][j].card.show(qi[i][j].v,"0");
}
}
}
}

2006-12-11 13:16
jefyu
Rank: 1
等 级:新手上路
威 望:1
帖 子:30
专家分:0
注 册:2006-7-26
收藏
得分:0 
我来看看,呵呵

2006-12-11 19:25
NiuQingPeng
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-10
收藏
得分:0 
好象很冷清呀
2006-12-13 16:56
快速回复:挖地雷的小程序,reset不知怎么编写了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024336 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved