import java.awt.*;
import java.awt.event.*;
public class lianliankan implements actionlistener
{
static string s=”no”; //用来纪录点击按钮的信息
int x0=0,y0=0,x=0,y=0,n1=0,n2=0; //用来纪录按钮的位置信息
frame f,f1;
button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10; //用比较笨的方法添加了
button b11,b12,b13,b14,b15,b16,b17,b18; //30个按钮来实现游戏界面
button b19,b20,b21,b22,b23,b24,b25; //可以用数组实现,这是本人
button b26,b27,b28,b29,b30,bc; //学java时,入门的联系,所以
button b,ba,br,bt1,bt2; //有些东西很业余!!嘻嘻
panel p1,p2,p3;
textfield t; //用来显示一些随机信息,方法是下面的guli().
label l;
int d[][]={ //用来和界面的按钮建立映射关系
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
public static void main(string[] args)
{
lianliankan t=new lianliankan();
t.suiji();
t.go();
}
public void actionperformed(actionevent e) //再来一次按钮的响应事件。
{
int d[][]={
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
this.d=d;
suiji();
f.setvisible(false);
f1.setvisible(false);
s=”no”;
go();
}
public void go()//初始化界面
{
l=new label(“亲爱的玩家,”);
f=new frame(“连连看”);
t=new textfield();
p2=new panel();
p1=new panel();
p3=new panel();
bc=new button(“退出”);
br=new button(“重列”);
b=new button();
b1=new button(string.valueof(d[1][1]));
b2=new button(string.valueof(d[1][2]));
b3=new button(string.valueof(d[1][3]));
b4=new button(string.valueof(d[1][4]));
b5=new button(string.valueof(d[1][5]));
b6=new button(string.valueof(d[2][1]));
b7=new button(string.valueof(d[2][2]));
b8=new button(string.valueof(d[2][3]));
b9=new button(string.valueof(d[2][4]));
b10=new button(string.valueof(d[2][5]));
b11=new button(string.valueof(d[3][1]));
b12=new button(string.valueof(d[3][2]));
b13=new button(string.valueof(d[3][3]));
b14=new button(string.valueof(d[3][4]));
b15=new button(string.valueof(d[3][5]));
b16=new button(string.valueof(d[4][1]));
b17=new button(string.valueof(d[4][2]));
b18=new button(string.valueof(d[4][3]));
b19=new button(string.valueof(d[4][4]));
b20=new button(string.valueof(d[4][5]));
b21=new button(string.valueof(d[5][1]));
b22=new button(string.valueof(d[5][2]));
b23=new button(string.valueof(d[5][3]));
b24=new button(string.valueof(d[5][4]));
b25=new button(string.valueof(d[5][5]));
b26=new button(string.valueof(d[6][1]));
b27=new button(string.valueof(d[6][2]));
b28=new button(string.valueof(d[6][3]));
b29=new button(string.valueof(d[6][4]));
b30=new button(string.valueof(d[6][5]));
p3.setlayout(null);
p1.setsize(250,300);
p2.setsize(100,40);
p3.setsize(300,30);
t.setsize(60,30);
l.setsize(70,30);
p1.setlayout(new gridlayout(6,5));
p1.setbackground(color.pink);
p1.setlocation(100,100);
p2.setlocation(0,400);
p3.setlocation(50,50);
t.setlocation(230,2);
l.setlocation(150,2);
bc.setlocation(0,40);
br.setlocation(0,100);
f.add(p1);
f.add(p2);
f.add(p3);
p3.add(l);
p3.add(t);
p2.add(bc);
p2.add(br);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
p1.add(b17);
p1.add(b18);
p1.add(b19);
p1.add(b20);
p1.add(b21);
p1.add(b22);
p1.add(b23);
p1.add(b24);
p1.add(b25);
p1.add(b26);
p1.add(b27);
p1.add(b28);
p1.add(b29);
p1.add(b30);
f.pack();
f.setbounds(280,100,500,450);
f.setresizable(false);
f.setvisible(true);
bc.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
ex();
}
});
br.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
chonglie();
}
});
b1.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(1,1,b1);
}
});
b2.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(1,2,b2);
}
});
b3.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(1,3,b3);
}
});
b4.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(1,4,b4);
}
});
b5.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(1,5,b5);
}
});
b6.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(2,1,b6);
}
});
b7.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(2,2,b7);
}
});
b8.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(2,3,b8);
}
});
b9.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(2,4,b9);
}
});
b10.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(2,5,b10);
}
});
b11.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(3,1,b11);
}
});
b12.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(3,2,b12);
}
});
b13.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(3,3,b13);
}
});
b14.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(3,4,b14);
}
});
b15.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(3,5,b15);
}
});
b16.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(4,1,b16);
}
});
b17.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(4,2,b17);
}
});
b18.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(4,3,b18);
}
});
b19.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(4,4,b19);
}
});
b20.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(4,5,b20);
}
});
b21.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(5,1,b21);
}
});
b22.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(5,2,b22);
}
});
b23.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(5,3,b23);
}
});
b24.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(5,4,b24);
}
});
b25.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(5,5,b25);
}
});
b26.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(6,1,b26);
}
});
b27.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(6,2,b27);
}
});
b28.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(6,3,b28);
}
});
b29.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(6,4,b29);
}
});
b30.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
wei(6,5,b30);
}
});
}
public void ex() //退出界面,可用diolog来实现有模式的类型,更加符合
{
f1=new frame(“游戏作业”);
f1.setlayout(new gridlayout(1,1));
bt1=new button(“确定退出”);
bt2=new button(“再来一局”);
f1.add(bt1);
f1.add(bt2);
f1.pack();
f1.setbounds(400,250,90,60);
f1.setresizable(false);
f1.show();
f1.setvisible(true);
bt1.addmouselistener(new mouseadapter(){
public void mouseclicked(mouseevent e)
{
system.exit(0);
}
});
bt2.addactionlistener(this);
}
public void suiji() //产生随机数,来填充游戏界面对应的数组的各个位置
{
int m,n,k=0,k1,k2,k3;
for(m=1;m<=15;m++)
{
k1=(int)(math.random()*25+1);
for(n=1;n<=2;n++)
{
k2=(int)(math.random()*6+1);
k3=(int)(math.random()*5+1);
while(d[k2][k3]!=0 && k!=30)
{
k2=(int)(math.random()*6+1);
k3=(int)(math.random()*5+1);
}
this.d[k2][k3]=k1;
k++;
}
}
}
public void guli() //随机信息
{
int l=0;
t.settext(“”);
l=(int)(math.random()*10);
system.out.println(l);
switch(l)
{
case 1:
t.settext(“好!加油!”);
break;
case 3:
t.settext(“你真棒!”);
break;
case 5:
t.settext(“加快速度!”);
break;
case 6:
t.settext(“不错啊!”);
break;
case 8:
t.settext(“加油吧!”);
break;
case 9:
t.settext(“够聪明!”);
break;
default:
break;
}
}
public void chonglie() //重列方法
{
int save[],i,j,n=0,k2,k3,k;
int d[][]={
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
save=new int[30];
for(n=0;n<30;n++)
save[n]=0; //定义一个数组来保存当前的每个按钮位置上的信息
n=0;
for(i=0;i<=6;i++)
for(j=0;j<=5;j++)
{
if(this.d[i][j]!=0)
{
save[n]=this.d[i][j];
n++;
}
}
n=n-1;
this.d=d;
while(n>=0) //产生随机位置,放置按钮
{
k2=(int)(math.random()*6+1);
k3=(int)(math.random()*5+1);
while(d[k2][k3]!=0)
{
k2=(int)(math.random()*6+1);
k3=(int)(math.random()*5+1);
}
this.d[k2][k3]=save[n];
n–;
}
f.setvisible(false);
s=”no”; //这里一定要将按钮点击信息归为初始
go();
ling();
}
public void ling() //将数组中为零的成员对应的按钮消去
{ //用按钮类型的数组实现会简化得多,
if(d[1][1]==0)
b1.setvisible(false);
if(d[1][2]==0)
b2.setvisible(false);
if(d[1][3]==0)
b3.setvisible(false);
if(d[1][4]==0)
b4.setvisible(false);
if(d[1][5]==0)
b5.setvisible(false);
if(d[2][1]==0)
b6.setvisible(false);
if(d[2][2]==0)
b7.setvisible(false);
if(d[2][3]==0)
b8.setvisible(false);
if(d[2][4]==0)
b9.setvisible(false);
if(d[2][5]==0)
b10.setvisible(false);
if(d[3][1]==0)
b11.setvisible(false);
if(d[3][2]==0)
b12.setvisible(false);
if(d[3][3]==0)
b13.setvisible(false);
if(d[3][4]==0)
b14.setvisible(false);
if(d[3][5]==0)
b15.setvisible(false);
if(d[4][1]==0)
b16.setvisible(false);
if(d[4][2]==0)
b17.setvisible(false);
if(d[4][3]==0)
b18.setvisible(false);
if(d[4][4]==0)
b19.setvisible(false);
if(d[4][5]==0)
b20.setvisible(false);
if(d[5][1]==0)
b21.setvisible(false);
if(d[5][2]==0)
b22.setvisible(false);
if(d[5][3]==0)
b23.setvisible(false);
if(d[5][4]==0)
b24.setvisible(false);
if(d[5][5]==0)
b25.setvisible(false);
if(d[6][1]==0)
b26.setvisible(false);
if(d[6][2]==0)
b27.setvisible(false);
if(d[6][3]==0)
b28.setvisible(false);
if(d[6][4]==0)
b29.setvisible(false);
if(d[6][5]==0)
b30.setvisible(false);
}
public void wei(int w1,int w2,button bz) //判断并纪录每次点击按钮的信息
{ //当两次的按钮相同才能消去
if((s.trim()).equals(“no”))
{
s=b1.getlabel();
x0=w1;
y0=w2;
n1=d[x0][y0];
b=bz;
x=w1;
y=w2;
n2=d[x][y];
ba=bz;
}
else
{
x0=x;
y0=y;
n1=d[x0][y0];
b=ba;
x=w1;
y=w2;
n2=d[x][y];
ba=bz;
if(n1==n2 && ba!=b)
{
xiao();
}
}
}
public void xiao() //这里是整个游戏最重要的部分,就是判断两个按钮在信息
{ //相同的情况下能不能消去。仔细分析,不一条条注释
int i=0, j=0,n=0,k=0;
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))) //相邻的情况
{
ba.setvisible(false);
b.setvisible(false);
guli();
s=”no”;
d[x0][y0]=0;
d[x][y]=0;
}
else
{
for (j=0;j<7;j++ ) //两个按钮按行分析,看能否消去
{
if (d[x0][j]==0)
{
if (y>j)
{
for (i=y-1;i>=j;i– )
{
if (d[x][i]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (y0>j)
{
for (i=y0-1;i>=j ;i– )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0<j)
{
for (i=y0+1;i<=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (y<j)
{
for (i=y+1;i<=j ;i++ )
{
if (d[x][i]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (y0>j)
{
for (i=y0-1;i>=j ;i– )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0<j)
{
for (i=y0+1;i<=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (y==j )
{
if (y0>j)
{
for (i=y0-1;i>=j ;i– )
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (y0<j)
{
for (i=y0+1;i<=j ;i++)
{
if (d[x0][i]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (k==2)
{ if (x0==x)
{
b.setvisible(false);
ba.setvisible(false);
guli();
s=”no”;
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
if (x0<x)
{
for (n=x0;n<=x-1;n++ )
{
if (d[n][j]!=0)
{
k=0;
break;
}
if(d[n][j]==0 && n==x-1)
{
b.setvisible(false);
ba.setvisible(false);
guli();
s=”no”;
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
if (x0>x)
{
for (n=x0;n>=x+1 ;n– )
{
if (d[n][j]!=0)
{
k=0;
break;
}
if(d[n][j]==0 && n==x+1)
{
b.setvisible(false);
ba.setvisible(false);
guli();
s=”no”;
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
}
}
for (i=0;i<8;i++ ) //按列分析,看能不能消去
{
if (d[i][y0]==0)
{
if (x>i)
{
for (j=x-1;j>=i ;j– )
{
if (d[j][y]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (x0>i)
{
for (j=x0-1;j>=i ;j– )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0<i)
{
for (j=x0+1;j<=i;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (x<i)
{
for (j=x+1;j<=i;j++ )
{
if (d[j][y]!=0)
{
k=0;
break;
}
else
{
k=1;
}
}
if (k==1)
{
if (x0>i)
{
for (j=x0-1;j>=i ;j– )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0<i)
{
for (j=x0+1;j<=i ;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (x==i)
{
if (x0>i)
{
for (j=x0-1;j>=i ;j– )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
if (x0<i)
{
for (j=x0+1;j<=i ;j++ )
{
if (d[j][y0]!=0)
{
k=0;
break;
}
else
{
k=2;
}
}
}
}
}
if (k==2)
{
if (y0==y)
{
b.setvisible(false);
ba.setvisible(false);
guli();
s=”no”;
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
if (y0<y)
{
for (n=y0;n<=y-1 ;n++ )
{
if (d[i][n]!=0)
{
k=0;
break;
}
if(d[i][n]==0 && n==y-1)
{
b.setvisible(false);
ba.setvisible(false);
guli();
s=”no”;
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
if (y0>y)
{
for (n=y0;n>=y+1 ;n–)
{
if (d[i][n]!=0)
{
k=0;
break;
}
if(d[i][n]==0 && n==y+1)
{
b.setvisible(false);
ba.setvisible(false);
guli();
s=”no”;
k=0;
d[x0][y0]=0;
d[x][y]=0;
}
}
}
}
}
}
}
}