扫雷游戏vb代码:模拟实现扫雷游戏代码



//模拟实现扫雷游戏自学编程网整理

importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;

importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.border.LineBorder;

publicMySwingTest05{

JFramejf;

Containercon;

JPaneljp;

//按钮
JButtonjb;

//标签
JLabeljl;

//中间容器
JPaneljpt;

//产生标签内容
charch;

//定义地雷个数和位置
num;

//计算地雷个数并写到标签中
temp;

//产生图盘行数
01=9;

//地雷个数为02
02=10;

//标签和按钮大小
xy=15;

publicMySwingTest05{

//TODO自动生成构造存根

//生成随机

ch=char[01*01];
num=[02];
temp=0;

for(i=0;i<num.length;){
temp=()(Math.random*(01*01));
for(j=0;j<i;j){
(tempnum[j]&&i!=0){
temp=0;
;
}
}
(temp!=0){
num[i]=temp;
//.out.prln(num[i]);
i;
}

}

/*
*//保证每行都有地雷for(i=0;i<num.length;i){temp=01*i;
*num[i]=()(Math.random*01+temp);// [Page]
*.out.prln(num[i]);}
*/

for(i=0;i<num.length;i){

ch[num[i]]=’⊙’;

}
for(i=0;i<ch.length;i){

(ch[i]!=’⊙’){
(i0){
temp=0;
tempch[i+1]’⊙’?1:0;//右边
tempch[i+01]’⊙’?1:0;//下边
tempch[i+01+1]’⊙’?1:0;//右下边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);

}
}(i(01-1)){
temp=0;
tempch[i-1]’⊙’?1:0;//左边
tempch[i+01]’⊙’?1:0;//下边
tempch[i+01-1]’⊙’?1:0;//左下边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}

(i01*(01-1)){


temp=0;
tempch[i+1]’⊙’?1:0;//右边
tempch[i-01]’⊙’?1:0;//上边
tempch[i-01+1]’⊙’?1:0;//右上边 [Page]
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}(i(01*01-1)){
temp=0;
tempch[i-1]’⊙’?1:0;//左边
tempch[i-01]’⊙’?1:0;//上边
tempch[i-01-1]’⊙’?1:0;//左上边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}(i>0&&i<01-1){
temp=0;
tempch[i+1]’⊙’?1:0;//右边
tempch[i-1]’⊙’?1:0;//左边
tempch[i+01]’⊙’?1:0;//下边
tempch[i+01-1]’⊙’?1:0;//左下边
tempch[i+01+1]’⊙’?1:0;//右下边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}(i>01*(01-1)&&i<01*01-1){
temp=0; [Page]
tempch[i+1]’⊙’?1:0;//右边
tempch[i-1]’⊙’?1:0;//左边
tempch[i-01]’⊙’?1:0;//上边
tempch[i-01-1]’⊙’?1:0;//左上边
tempch[i-01+1]’⊙’?1:0;//右上边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}(i%010){
temp=0;
tempch[i+1]’⊙’?1:0;//右边
tempch[i-01]’⊙’?1:0;//上边
tempch[i+01]’⊙’?1:0;//下边
tempch[i-01+1]’⊙’?1:0;//右上边
tempch[i+01+1]’⊙’?1:0;//右下边
(temp0){
ch[i]=’’;


}{
ch[i]=getchar(temp);
}
}(i%0101-1){
temp=0;
tempch[i-1]’⊙’?1:0;//左边
tempch[i-01]’⊙’?1:0;//上边 [Page]
tempch[i+01]’⊙’?1:0;//下边
tempch[i-01-1]’⊙’?1:0;//左上边
tempch[i+01-1]’⊙’?1:0;//左下边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}{
temp=0;
tempch[i+1]’⊙’?1:0;//右边
tempch[i-1]’⊙’?1:0;//左边
tempch[i-01]’⊙’?1:0;//上边
tempch[i+01]’⊙’?1:0;//下边
tempch[i-01-1]’⊙’?1:0;//左上边
tempch[i-01+1]’⊙’?1:0;//右上边
tempch[i+01-1]’⊙’?1:0;//左下边
tempch[i+01+1]’⊙’?1:0;//右下边
(temp0){
ch[i]=’’;
}{
ch[i]=getchar(temp);
}
}
}
//.out.prln(ch[i]);

}//生成图形完毕

//生成图形
jf=JFrame(\"扫雷\");
con=jf.getContentPane;

jp=JPanel(GridLayout(01,01,0,0)); [Page]

jb=JButton[01*01];
jl=JLabel[01*01];
jpt=JPanel[01*01];

for(i=0;i<jpt.length;i){
jpt[i]=JPanel;
jpt[i].Layout(BorderLayout);
//getchar;
jl[i]=JLabel(\"\"+ch[i]+\"\");
jl[i].PreferredSize(Dimension(xy,xy));//设置图形大小
jl[i].HorizontalAlignment(JLabel.CENTER);//中间显示
jb[i]=JButton;
jb[i].PreferredSize(Dimension(xy,xy));//设置图形大小
jb[i].addActionListener(actionL01(this));
jb[i].addMouseListener(mouseL01(this));
jb[i].ActionCommand(\"\"+i);

//jpt[i].add(jl[i]);
jpt[i].Border(LineBorder(Color.GRAY,1));
jpt[i].add(jb[i]);
jp.add(jpt[i]);
}

con.add(jp);

Dimensiondim=Toolkit.getDefaultToolkit.getScreenSize;
jf.DefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.Location(()((dim.getWidth-jf.getWidth)/2),
()((dim.getHeight-jf.getHeight)/2));
jf.pack;


jf.Resizable(false);
jf.Visible(true);

}

publicvoid(Stringargs){

MySwingTest05;

}

//根据生成char
publicchargetchar(i){
switch(i){
1:
’1’;
2:
’2’;
3:
’3’;
4:
’4’;
5:
’5’;
6:
’6’;
7:
’7’;
8:
’8’;
default: [Page]
’’;
}
}



//实现接口ActionListener

actionL01implementsActionListener{
MySwingTest05mm;

//JButtonjbtemp;

publicactionL01{

}

publicactionL01(MySwingTest05mm){

this.mm=mm;
}

publicvoidactionPerformed(ActionEvente){

//this.jbtemp=(JButton)e.getSource;

this.action1(Integer.parseInt((e.getActionCommand)));
this.action5;

}

//根据区别按钮对象打开相应标签
publicvoidaction1(i){

//地雷是被数字包围着所以不用担心打开地雷
//如果打开是地雷就直接报告
//如果打开是数字就只打开自己本身
//如果打开空就递归循环打开直到数字包围着地雷

//首先判断按钮颜色pink则不打开
(mm.jb[i].getBackground!=Color.PINK
&&mm.jpt[i].getComponent(0).getClass.toString.equals(
\"javax.swing.JButton\")){
(mm.ch[i]’⊙’){//判断是地雷
action3(i);
mm.jf.Title(\"中雷!\");
action4;
}(Character.isDigit(mm.ch[i])){//判断是数字
action3(i);
}//判断是空
{
action2(i);
}

}
}

//踩雷时触发
publicvoidaction4{
for(i=0;i<mm.jb.length;i){
//判断标示地方是否为地雷

//如果就用表示
(mm.ch[i]!=’⊙’&&(mm.jb[i].getBackgroundColor.PINK)){
mm.jl[i].Text(\"※\");
}
//如果没有表示就设置为空
(mm.ch[i]!=’⊙’
&&mm.jpt[i].getComponent(0).getClass.toString.equals(
\"javax.swing.JButton\")){ [Page]
mm.jl[i].Text(\"\");//将没有打开且不是地雷按钮设置为空
}
//如果正确就用表示
(mm.ch[i]’⊙’
&&(mm.jb[i].getBackgroundColor.PINK)){
mm.jl[i].Text(\"√\");
}
action3(i);

}
}

//根据相应按钮对象打开相应标签
publicvoidaction3(i){

mm.jpt[i].remove(mm.jb[i]);
//根据区别数字设置颜色
(Character.isDigit(mm.ch[i])){
switch(mm.ch[i]){

’1’:
mm.jl[i].Foreground(Color.BLUE);
;
’2’:
mm.jl[i].Foreground(Color.GREEN);
;
’3’:
mm.jl[i].Foreground(Color.RED);
;
’4’:
mm.jl[i].Foreground(Color.MAGENTA);


;
’5’:
mm.jl[i].Foreground(Color.CYAN);
;
’6’:
mm.jl[i].Foreground(Color.ORANGE);
;
’7’:
mm.jl[i].Foreground(Color.PINK);
;
’8’:
mm.jl[i].Foreground(Color.DARK_GRAY);
;

}
}
mm.jpt[i].add(mm.jl[i]);
mm.jf.validate;

}

//判断是否胜利
publicvoidaction5{
sum=0;
for(i=0;i<mm.jb.length;i){

(mm.ch[i]’⊙’&&(mm.jb[i].getBackgroundColor.PINK)){
//sum;
//.out.prln(sum);
(summm.num.length){
mm.jf.Title(\"胜利!\");
for(j=0;j<mm.jb.length;j){ [Page]
action3(j);
}
;

}
}
}
}

//只判断上下左右采用递归
publicvoidaction2(ind){
01=mm.01;

action3(ind);

//不选择action2而选择action1需要判断按钮颜色
//(Character.isDigit(mm.ch[ind])false){
(ind0){
/*
*(mm.ch[ind+1]’’)//右边{
*/
action1(ind+1);//右边
//选择action1action1能够判断按钮颜色
//action2(ind+1);//不选择action2action2不能判断按钮颜色以下同理
/*
*}{action1(ind+1);}(mm.ch[ind+01]’’)//下边{
*/
action1(ind+01);//下边
/*
*}{action1(ind+01);}(mm.ch[ind+01+1]’’&&
*mm.jpt[ind+01+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右下边{
*/
action1(ind+01+1);//右下边
/*
*}{action1(ind+01+1);}
*/

}(ind01-1){
/*
*(mm.ch[ind-1]’’)//左边{
*/
action1(ind-1);//左边
/*
*}{action1(ind-1);}(mm.ch[ind+01]’’)//下边{
*/ [Page]
action1(ind+01);//下边
/*
*}{action1(ind+01);}(mm.ch[ind+01-1]’’&&
*mm.jpt[ind+01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左下边{
*/
action1(ind+01-1);//左下边
/*
*}{action1(ind+01-1);}
*/

}(ind01*(01-1)){
/*
*(mm.ch[ind+1]’’)//右边{


*/
action1(ind+1);//右边
/*
*}{action1(ind+1);}(mm.ch[ind-01]’’)//上边{
*/
action1(ind-01);//上边
/*
*}{action1(ind-01);}
*
*(mm.ch[ind-01+1]’’&&mm.jpt[ind-01+
*1].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//右上边{
*/
action1(ind-01+1);//右上边
/*
*}{action1(ind-01+1);}
*/

}(ind01*01-1){
/*
*(mm.ch[ind-1]’’)//左边{
*/
action1(ind-1);//左边
/*
*}{action1(ind-1);}(mm.ch[ind-01]’’)//上边{ [Page]
*/
action1(ind-01);//上边
/*
*}{action1(ind-01);}(mm.ch[ind-01-1]’’&&
*mm.jpt[ind-01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左上边{
*/
action1(ind-01-1);//左上边
/*
*}{action1(ind-01-1);}
*/

}(ind>0&&ind<01-1){
/*
*(mm.ch[ind+01]’’&&mm.jpt[ind+
*01].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//下边{
*/
action1(ind+01);//下边
/*
*}{action1(ind+01);}(mm.ch[ind-1]’’&&
*mm.jpt[ind-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左边{
*/
action1(ind-1);//左边
/*
*}{action1(ind-1);}(mm.ch[ind+1]’’&&
*mm.jpt[ind+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右边{
*/
action1(ind+1);//右边
/*
*}{action1(ind+1);}(mm.ch[ind+01+1]’’&&
*mm.jpt[ind+01+1].getComponent(0).getClass [Page]
*.toString.equals(\"javax.swing.JButton\"))//右下边{
*/
action1(ind+01+1);//右下边
/*
*}{action1(ind+01+1);}(mm.ch[ind+01-1]’’&&
*mm.jpt[ind+01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左下边{
*/


action1(ind+01-1);//左下边
/*
*}{action1(ind+01-1);}
*/
}(ind>01*(01-1)&&ind<01*01-1){
/*
*(mm.ch[ind-01]’’&&mm.jpt[ind-
*01].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//上边{
*/
action1(ind-01);//上边
/*
*}{action1(ind-01);}(mm.ch[ind-1]’’&&
*mm.jpt[ind-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左边{
*/
action1(ind-1);//左边
/*
*}{action1(ind-1);}(mm.ch[ind+1]’’&&
*mm.jpt[ind+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右边{
*/
action1(ind+1);//右边
/*
*}{action1(ind+1);}(mm.ch[ind-01-1]’’&& [Page]
*mm.jpt[ind-01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左上边{
*/
action1(ind-01-1);//左上边
/*
*}{action1(ind-01-1);}(mm.ch[ind-01+1]’’&&
*mm.jpt[ind-01+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右上边{
*/
action1(ind-01+1);//右上边
/*
*}{action1(ind-01+1);}
*/
}(ind%010){
/*
*(mm.ch[ind-01]’’&&mm.jpt[ind-
*01].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//上边{
*/
action1(ind-01);//上边
/*
*}{action1(ind-01);}(mm.ch[ind+01]’’&&
*mm.jpt[ind+01].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//下边{
*/
action1(ind+01);//下边
/*
*}{action1(ind+01);}(mm.ch[ind+1]’’&&
*mm.jpt[ind+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右边{
*/
action1(ind+1);//右边
/*
*}{action1(ind+1);}(mm.ch[ind+01+1]’’&& [Page]
*mm.jpt[ind+01+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右下边{
*/


action1(ind+01+1);//右下边
/*
*}{action1(ind+01+1);}(mm.ch[ind-01+1]’’&&
*mm.jpt[ind-01+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右上边{
*/
action1(ind-01+1);//右上边
/*
*}{action1(ind-01+1);}
*/
}(ind%0101-1){
/*
*(mm.ch[ind-01]’’&&mm.jpt[ind-
*01].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//上边{
*/
action1(ind-01);//上边
/*
*}{action1(ind-01);}(mm.ch[ind+01]’’&&
*mm.jpt[ind+01].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//下边{
*/
action1(ind+01);//下边
/*
*}{action1(ind+01);}(mm.ch[ind-1]’’&&
*mm.jpt[ind-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左边{
*/
action1(ind-1);//左边
/*
*}{action1(ind-1);} [Page]
*
*(mm.ch[ind+01-1]’’&&mm.jpt[ind+01-
*1].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//左下边{
*/
action1(ind+01-1);//左下边
/*
*}{action1(ind+01-1);}(mm.ch[ind-01-1]’’&&
*mm.jpt[ind-01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左上边{
*/
action1(ind-01-1);//左上边
/*
*}{action1(ind-01-1);}
*/

}{
/*
*(mm.ch[ind-01]’’&&mm.jpt[ind-
*01].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//上边{
*/
action1(ind-01);//上边
/*
*}{action1(ind-01);}(mm.ch[ind+01]’’&&
*mm.jpt[ind+01].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//下边{
*/
action1(ind+01);//下边
/*
*}{action1(ind+01);}(mm.ch[ind-1]’’&&
*mm.jpt[ind-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左边{
*/
action1(ind-1);//左边 [Page]


/*
*}{action1(ind-1);}(mm.ch[ind+1]’’&&
*mm.jpt[ind+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右边{
*/
action1(ind+1);//右边
/*
*}{action1(ind+1);}
*
*(mm.ch[ind+01+1]’’&&mm.jpt[ind+01+
*1].getComponent(0).getClass.toString.equals(\"
*javax.swing.JButton\"))//右下边{
*/
action1(ind+01+1);//右下边
/*
*}{action1(ind+01+1);}(mm.ch[ind+01-1]’’&&
*mm.jpt[ind+01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左下边{
*/
action1(ind+01-1);//左下边
/*
*}{action1(ind+01-1);}(mm.ch[ind-01-1]’’&&
*mm.jpt[ind-01-1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//左上边{
*/
action1(ind-01-1);//左上边
/*
*}{action1(ind-01-1);}(mm.ch[ind-01+1]’’&&
*mm.jpt[ind-01+1].getComponent(0).getClass
*.toString.equals(\"javax.swing.JButton\"))//右上边{ [Page]
*/
action1(ind-01+1);//右上边
/*
*}{action1(ind-01+1);}
*/

}
}
}

//实现接口MouseListenner
//继承类MouseAdapter不用实现接口MouseListener中所有思路方法只要实现需要实现思路方法默认实现为null
mouseL01extendsMouseAdapter{
MySwingTest05mm;

actionL01aa;

publicmouseL01{
aa=actionL01;
}

publicmouseL01(MySwingTest05mm){

this.mm=mm;
aa=actionL01(mm);//注意要传递对象mm保证对象为同对象
}

//鼠标右键标示地雷yellow表示提示但不标示
publicvoidmouseClicked(MouseEvente){

(e.getButtonMouseEvent.BUTTON3){
JButtonjb=(JButton)e.getSource;
//jb.Text(\"⊙\");
(jb.getBackgroundColor.PINK){
jb.Background(Color.YELLOW);
}(jb.getBackgroundColor.YELLOW){
jb.Background(null);
}{
jb.Background(Color.PINK);
}
aa.action5;
}
}
}

}

Tags: 

延伸阅读

最新评论

发表评论