2048是我好早以前就想做的游戏,在2014年8月19日终于大功告成啦!!!特别开心,当然这里要感谢的人特别特别的多,没有熊哥、我徒儿、小李子他们帮助我排除一个个疑难杂症,我不可能在这短短几天就将它做成这个,所以灰常感谢蓝杰的小伙伴们,小艳子爱你们,么么哒……突然想到我的老爸老妈,希望我所做到的这些没有让他们失望,他们一直以来都特别支持我的想法,最最想感谢的还是我亲亲爱爱滴父母……矫情的话不说啦,真的好爱你们……下面是我的2048游戏运行界面……以及玩家须知的图片……还是一如既往的自恋地加上自己的照片,把自己录的歌作为背景音乐……
也不多说啦,开始整理咯……
public class Zodiac extends JFrame{
MyListener my;
int x,y;
JTextField jt;
JLabel jl6;
JButton j11,j12,j13,j14,j15;
private JPanel jp;
Random ran=new Random();
public static JButton array[][]=new JButton [4][4];
//首先我的主函数是这样的,只调用了一个方法,部分内容是写在surface里面
public static void main(String[] args) {
Zodiac zo=new Zodiac();
zo.surface();
}
public void surface(){
//在我的这些年系列的第六集中,已经大部分介绍了一些方法的调用和里面的参数问题,这里就不赘述了
this.setTitle(" 2 0 4 8 ");
this.setFont(new Font("楷体",1,23));
this.setForeground(new Color(124,166,216));
this.setSize(980, 650);
this.setResizable(false);
this.setLocation(350, 20);
this.setLayout(null);//我在这里设置了空布局,主要是为了方便设置各个容器的位置
this.setDefaultCloseOperation(3);
//这里就是设置默认图标,之前应该有提到过,这样使得界面美观些,为了P这张2048的默认图标的图,俺花了1个多小时……泪奔
ImageIcon im=new ImageIcon(this.getClass().getResource("123.PNG"));
this.setIconImage(im.getImage());
//设置鼠标,我们可以通过调用Toolkit类里面的方法来改变鼠标的形状,这也是美化界面的一部分
Toolkit kit =Toolkit.getDefaultToolkit();
Image sb=kit.getImage(this.getClass().getResource("12345.PNG"));
Cursor cu=kit.createCustomCursor(sb,new Point(0,0),"asfas");
this.setCursor(cu);
// 这部分的代码很长很啰嗦,无非就是加上“十二生肖”这四个字,但是一定要记得每一个在JFrame上添加的容器都要加上一句: 容器名.setFocusable(false);因为玩2048要加键盘监听器,如果不将容器都设为不可聚焦的话,会使得窗体无法监听到键盘的动作
ImageIcon im1=new ImageIcon(this.getClass().getResource("1.PNG"));
ImageIcon im2=new ImageIcon(this.getClass().getResource("2.PNG"));
ImageIcon im3=new ImageIcon(this.getClass().getResource("3.PNG"));
ImageIcon im4=new ImageIcon(this.getClass().getResource("4.PNG"));
JLabel jl1=new JLabel(im1);
JLabel jl2=new JLabel(im2);
JLabel jl3=new JLabel(im3);
JLabel jl4=new JLabel(im4);
jl1.setBounds(615, 150,im1.getIconWidth(),im1.getIconHeight());
jl2.setBounds(730, 260,im2.getIconWidth(),im2.getIconHeight());
jl3.setBounds(730, 40,im3.getIconWidth(),im3.getIconHeight());
jl4.setBounds(839,155,im4.getIconWidth(),im4.getIconHeight());
jl1.setFocusable(false);
jl2.setFocusable(false);
jl3.setFocusable(false);
jl4.setFocusable(false);
this.add(jl1);
this.add(jl2);
this.add(jl3);
this.add(jl4);
//很明显这个就是在做得分那一栏的界面
JLabel jl=new JLabel("S c o r e");
jl.setBounds(730, 495,160, 40);
jl.setFont(new Font("楷体",1,22));
jl.setForeground(new Color(124,166,216));
jl.setFocusable(false);
this.add(jl);
//我这里可以直接写 jl6=new JLabel("0");是因为我在最最开始写了JLabel jl6;如果没有jl6=new JLabel("0");就会报空指针异常
jl6=new JLabel("0");
// jl6.setName("0");
jl6.setFocusable(false);
jl6.setBounds(718,540,120, 40);
jl6.setBackground(new Color(238,238,238));
jl6.setFont(new Font("楷体",1,23));
jl6.setForeground(new Color(124,166,216));
jl6.setHorizontalAlignment(JLabel.CENTER);
this.add(jl6);
//这里和上面的一样也是在做界面,如上图中的疯狂版和经典版,注意位置都是相对窗体而言的
j12=new JButton("疯 狂 版");
j12.setBounds(620, 380,140, 40);
j12.setFont(new Font("楷体",1,22));
j12.setForeground(new Color(124,166,216));
j12.setBackground(new Color(238,238,238));
j12.setFocusable(false);
this.add(j12);
// my=new MyListener(j12);
j13=new JButton("经 典 版");
j13.setBounds(810, 380,140, 40);
j13.setFont(new Font("楷体",1,22));
j13.setForeground(new Color(124,166,216));
j13.setBackground(new Color(238,238,238));
j13.setFocusable(false);
this.add(j13);
// j12.addMouseMotionListener(my);
// my=new MyListener(j13);
j14=new JButton("玩 家 须 知");
j14.setBounds(700,440,175, 40);
j14.setFont(new Font("楷体",1,22));
j14.setForeground(new Color(124,166,216));
j14.setBackground(new Color(238,238,238));
j14.setFocusable(false);
this.add(j14);
//这里就是在做再来一句按钮啦,因为添加的是图片,所以大家可能不知道这里到底是加了什么
j15=new JButton(" ");
j15.setBounds(740,163,80, 80);
j15.setBackground(new Color(238,238,238));
ImageIcon zl=new ImageIcon(this.getClass().getResource("zl.png"));
j15.setIcon(zl);
j15.setFocusable(false);
this.add(j15);
//这段代码就是画4乘4的格子,因为线条要加粗,所以用了Graphics2D类,但又不能直接用,所以得先实例化Graphics类,再强制转化
jp=new JPanel(){
public void paint(Graphics g){
Graphics2D gg=(Graphics2D)g;
super.paint(gg);
for(int i=0;i<5;i++){
gg.setColor(new Color(255,255,255));
gg.setStroke(new BasicStroke(8.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
gg.drawLine(5+i*146, 0,5+i*146, 600);
}
for(int j=0;j<5;j++){
gg.setColor(new Color(255,255,255));
gg.setStroke(new BasicStroke(8.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
gg.drawLine(0, 5+j*146,593, 5+j*146);
}
}
};
jp.setFocusable(false);
jp.setLocation(10, 10);
jp.setSize(593,593);
jp.setBackground(new Color(124,166,216));
this.setVisible(true);//这个绝对不能忘啦,不然整个窗体都看不到哟~~~
<span style="font-size:32px;">//以上都是界面的代码,接下来就是功能的实现啦……</span>
<pre class="java" name="code">public void initarray(){
//当我们画好4乘4的格子之后,就可以在这个格子里面添加16个button啦,当然这里要精确的计算每个格子的大小以及起点的横纵坐标
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
//两个for循环遍历一遍,将位置和颜色设置好之后,用之前定义过的JButton的二维数组,将初始的ActionCommand设置为空,这个很重要,因为之后辨认到底能不能撞在一起翻倍就是靠这个来识别
JButton button=new JButton("");
button.setBounds(19+i*146, 19+j*146, 138,138);
button.setBackground(new Color(124,166,216));
array[i][j]=button;
array[i][j].setActionCommand("");
this.add(array[i][j]); }
}
}
<pre class="java" name="code">public void addran(){
//这个方法是在任意位置随机添加一个2,如果是图片的话,就是代表2的那张图片
int x1=ran.nextInt(4);
int y1=ran.nextInt(4);//括号里面代表从0到3随机选择一个数
if(array[x1][y1].getActionCommand().equals("")){
//上面一行代码是在判断随机取的那个位置是否为空,如果是的话,就加一个2,或者说代表2的图片,如果不是的话,那就继续调用这个方法,属于一种递归的思想
ImageIcon i2=new ImageIcon(this.getClass().getResource("02.png"));
array[x1][y1].setIcon(i2);//这里就是实例化一个图片对象,将那个JButton加上这个图片
array[x1][y1].setActionCommand("2");//加完了之后别忘了将内容改为2,如果不这么做的话,就会出现重复添加的问题
}
else{
addran();
}
}
//接下来就是特别关键的算法了,我的这种是先走再碰撞相加,2048的关键就是在于在向左向右或向上向下的过程中,将所有相同数字的方块叠加,处于方向后面的那个方块会消失,而向着方向的方块会翻倍,如果没有相同的,就往方向指示的位置移动到不能再移了为之
public void moveright(){
//注意,这个算法名字是表示经典版,不能连加的那种玩法,怎么能让他不连加呢,就设置两个标记变量用来表示二维数组的量
int flag=4;
int flag2=4;//初始设为除了0、1、2、3的数都行,因为之后要根据需要将他们设为0、1、2、3中的任意一个
for(int j=0;j<4;j++){
//注意这个是向右的方向,所以外层的大循环应该是j=0,之后再让i=2,因为向右的话,要和后面的那个数比,可是下面为什么要再加个k=0,然后循环三次呢?这里我们要注意的问题就是只有i=3的位置那个的数字不用移动,其他三个位置都需要移动,所以,我们要有三个循环,以防万一
for(int k=0;k<3;k++){
for(int i=2;i>=0;i--){
String str=array[i][j].getActionCommand();
String str1=array[i+1][j].getActionCommand();
//上面两行代码主要为了简化之后的判断语句
if(!(str.equals(""))){
//判断的最大的前提是它有数字或者说有代表数字的图片,如果没有的话,就不需要判断是否一样了,没有的话就写在else if那里
if(str.equals(str1)&&(flag!=i)&&(flag2!=j)){
//上面一行的代码就是判断两个按钮上的数字是否一直,并且两个标记不属于本身,因为防止连加的最重要的部分就是要标记已经相加过的位置
int num=Integer.parseInt(str1);//这里是要获取强制将字符串转化为整型后的数,用于之后设置新的加倍后的数字的内容
setImage(i+1, j,(2*num));//这个方法很重要,在博客后面会附上,用途就是实例化图片,然后将那个图片添加在array[i+1][j]位置上
array[i+1][j].setActionCommand(2*num+"");//这句和上面一句都是在设置新生成的翻倍的JButton的图片和数字
setImage(i, j, 0);
array[i][j].setActionCommand("");//这上面两行是在设置本该消失的那个JButton的图片和数字,当然数字是为空,而图片其实就是将背景色截个图,然后附上去的
flag=i+1;
flag2=j;//这两行代码非常的重要,因为这样将标记变量设置为下一个,这样的话按照循环的顺序下一个如果还碰到可以碰撞相加的时候,就不满足前面if的条件,这样就不会连加了,是不是很机智!
jl6.setText((Integer.parseInt(jl6.getText())+2*num)+"");//这行代码呢就是在将分数增加,在原有的分数上加上碰撞后获取的数字翻倍的分数,再强制转化为字符型
}else if(str1.equals("")){//这里就是在处理没有数字或者说没有代表数字的图片的情况,让它往前走就可以了……
int num=Integer.parseInt(str);
setImage(i+1, j,num);
array[i+1][j].setActionCommand(num+"");
setImage(i, j,0);
array[i][j].setActionCommand("");//处理的方式是和前面差不多的逻辑
}
}
}
}
flag=4;
flag2=4;//这个很重要,因为这一部分的三个循环都执行完毕后,图片都会跑到窗体的右边,如果用户还有继续的操作的话,就得把数据归为初始化的值……等待她进入下一个操作
}
}
//接下来向左向上向下都是一样的算法啦……类似滴……大家可以自己推倒
public void moveleft(){
int flag=4;
int flag2=4;
for(int j=0;j<4;j++){
for(int k=0;k<3;k++){
for(int i=1;i<4;i++){
String str=array[i][j].getActionCommand();
String str1=array[i-1][j].getActionCommand();
if(!(str.equals(""))){
if(str.equals(str1)&&(flag!=i)&&(flag2!=j)){
int num=Integer.parseInt(str1);
setImage(i-1, j,(2*num));
array[i-1][j].setActionCommand(2*num+"");
setImage(i, j, 0);
array[i][j].setActionCommand("");
flag=i-1;
flag2=j;
jl6.setText((Integer.parseInt(jl6.getText())+2*num)+"");
}else if(str1.equals("")){
int num=Integer.parseInt(str);
setImage(i-1, j,num);
array[i-1][j].setActionCommand(num+"");
setImage(i, j,0);
array[i][j].setActionCommand("");
}
}
}
}
flag=5;
flag2=5;
}
}
public void moveup(){
int flag=4;
int flag2=4;
for(int i=0;i<4;i++){
for(int k=0;k<3;k++){
for(int j=1;j<4;j++){
String str=array[i][j].getActionCommand();
String str1=array[i][j-1].getActionCommand();
if(!(str.equals(""))){
if(str.equals(str1)&&(flag!=i)&&(flag2!=j)){
int num=Integer.parseInt(str1);
setImage(i, j-1,(2*num));
array[i][j-1].setActionCommand(2*num+"");
setImage(i, j, 0);
array[i][j].setActionCommand("");
flag=i;
flag2=j-1;
jl6.setText((Integer.parseInt(jl6.getText())+2*num)+"");
}else if(str1.equals("")){
int num=Integer.parseInt(str);
setImage(i, j-1,num);
array[i][j-1].setActionCommand(num+"");
setImage(i, j,0);
array[i][j].setActionCommand("");
}
}
}
}
flag=5;
flag2=5;
}
}
public void movedown(){
int flag=4;
int flag2=4;
for(int i=0;i<4;i++){
for(int k=0;k<3;k++){
for(int j=2;j>=0;j--){
String str=array[i][j].getActionCommand();
String str1=array[i][j+1].getActionCommand();
if(!(str.equals(""))){
if(str.equals(str1)&&(flag!=i)&&(flag2!=j)){
int num=Integer.parseInt(str1);
setImage(i, j+1,(2*num));
array[i][j+1].setActionCommand(2*num+"");
setImage(i, j, 0);
array[i][j].setActionCommand("");
flag=i;
flag2=j+1;
jl6.setText((Integer.parseInt(jl6.getText())+2*num)+"");
}else if(str1.equals("")){
int num=Integer.parseInt(str);
setImage(i,j+1,num);
array[i][j+1].setActionCommand(num+"");
setImage(i, j,0);
array[i][j].setActionCommand("");
}
}
}
}
flag=5;
flag2=5;
}
}
//这里就是之前一直说要调用的setImage的方法,顾名思义啦就是加图片,传的参数是三个整型数,前两个是传数组的位置,最后一个是数字,就是2、4、8……
public void setImage(int x,int y,int t){
switch(t){
case 0:
ImageIcon i0=new ImageIcon(this.getClass().getResource("0.PNG"));
array[x][y].setIcon(i0);
break;
case 2:
ImageIcon i2=new ImageIcon(this.getClass().getResource("02.png"));
array[x][y].setIcon(i2);
break;
case 4:
ImageIcon i4=new ImageIcon(this.getClass().getResource("04.png"));
array[x][y].setIcon(i4);
break;
case 8:
ImageIcon i8=new ImageIcon(this.getClass().getResource("08.png"));
array[x][y].setIcon(i8);
break;
case 16:
ImageIcon i16=new ImageIcon(this.getClass().getResource("16.png"));
array[x][y].setIcon(i16);
break;
case 32:
ImageIcon i32=new ImageIcon(this.getClass().getResource("32.png"));
array[x][y].setIcon(i32);
break;
case 64:
ImageIcon i64=new ImageIcon(this.getClass().getResource("64.png"));
array[x][y].setIcon(i64);
break;
case 128:
ImageIcon i128=new ImageIcon(this.getClass().getResource("128.png"));
array[x][y].setIcon(i128);
break;
case 512:
ImageIcon i512=new ImageIcon(this.getClass().getResource("512.png"));
array[x][y].setIcon(i512);
break;
case 1024:
ImageIcon i1028=new ImageIcon(this.getClass().getResource("1024.png"));
array[x][y].setIcon(i1028);
break;
case 256:
ImageIcon i256=new ImageIcon(this.getClass().getResource("256.png"));
array[x][y].setIcon(i256);
break;
case 2048:
ImageIcon i2048=new ImageIcon(this.getClass().getResource("2048.png"));
array[x][y].setIcon(i2048);
break;
case 1111:
ImageIcon i1111=new ImageIcon(this.getClass().getResource("1111.png"));
array[x][y].setIcon(i1111);
}
}
//接下来就是监听器的事儿了,定义一个监听器的类然后继承JFrame再实现ActionListener,MouseListener,KeyListener,MouseMotionListener的接口,记得实现接口的话必须得写上接口里面的抽象方法,否则会报错
public class MyListener extends JFrame implements ActionListener,MouseListener,KeyListener,MouseMotionListener{
private JButton jbu;
int x,y;
Zodiac zo;
public Zodiac jf;//先在全局定义了这个,方便把Zodiac类里面的方法和可调用的属性传递过来
AudioClip audioarray[]=new AudioClip[6];//这里是定义一个数组,用来存放音乐文件
String s[]={"shier.wav","pig.wav","dog.wav","02.wav","03.wav","04.wav"};//这是数组的静态初始化
String str="经 典 版";//这里是定义一个字符串,之后大有用处,当然先赋值为"经典版",是因为我把游戏设置默认为经典版的
public MyListener(Zodiac jf){
//这里的构造器主要是为了传参
this.jf= jf;
//这里就是用一个for循环将音乐文件赋给这个数组,try catch语句用来捕捉异常情况跳出的问题
try {
for(int i=0;i<audioarray.length;i++){
audioarray[i]=Applet.newAudioClip(this.getClass().getResource(s[i]));
}
} catch (Exception e) {
}
audioarray[0].loop();//默认的背景音乐是十二生肖,所以,我把他写在了这里,loop顾名思义就是循环的意思……也可以用play(),但是play方法播了一遍之后就没有再播了,所以,这里用loop更合适些
}
public void mousePressed(MouseEvent e) {
x=e.getX();
y=e.getY();
System.out.println(x + " "+y);
//这个对最终的结果没有实质性的作用,但是在确定每个容器的位置的时候需要这个帮忙找坐标
}
public void keyPressed(KeyEvent e) {
//这个方法就是很关键的部分,键盘监听器可以检测到用户用了哪个键
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
if(str.equals("经 典 版"))
jf.moveright();
else if(str.equals("疯狂版"))
jf.moveright2();//这里的意思就是说如果获取到的字符串是"经典版",就调用不能连加的那个方法,如果不是,那么就调用那个没有加两个标记变量的方法
if(!judge1()){//这个judge1的方法是判断4乘4的格子里的图片或者谁有没有占满,如果占满就要判断是否输了,如果没有占满,就继续调用addran方法
jf.addran();
}
if(judge2()){//这里judge2的方法就是出现了占满,且前后左右都没有相同的可以碰撞的数字或代表数字的图片
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
jf.setImage(i, j, 1111);
//这里就是因为挑战失败,就出现格子里全是猪的图片
}
}
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[1].play();
//然后所有其他的音乐都停止,只播放游戏结束的音乐:猪叫
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(jf.array[i][j].getActionCommand().equals("2048")){
//这里遍历一遍,若发现出现了2048则表示赢了,于是就将其他音乐通通停止,只播放赢的时候的音乐:欢快的狗叫
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[2].play();
for(int k=0;k<4;k++){
for(int l=0;l<4;l++){
jf.setImage(k, l, 2048);
//跟前面输的情况一样,将整个格子填满狗狗……
}
}
}
}
}
}//其他向左向上向下的情况是一样的,就不详细的赘述
else if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(str.equals("经 典 版"))
jf.moveleft();
else
jf.moveleft2();
if(!judge1()){
jf.addran();
}
if(judge2()){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
jf.setImage(i, j, 1111);
}
}
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[1].play();
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(jf.array[i][j].getActionCommand().equals("2048")){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[2].play();
for(int k=0;k<4;k++){
for(int l=0;l<4;l++){
jf.setImage(k, l, 2048);
}
}
}
}
}
}else if(e.getKeyCode()==KeyEvent.VK_UP){
if(str.equals("经 典 版"))
jf.moveup();
else
jf.moveup2();
if(!judge1()){
jf.addran();
}
if(judge2()){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[1].play();
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
jf.setImage(i, j, 1111);
}
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(jf.array[i][j].getActionCommand().equals("2048")){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[2].play();
for(int k=0;k<4;k++){
for(int l=0;l<4;l++){
jf.setImage(k, l, 2048);
}
}
}
}
}
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN){
if(str.equals("经 典 版"))
jf.movedown();
else
jf.movedown2();
if(!judge1()){
jf.addran();
}
if(judge2()){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[1].play();
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
jf.setImage(i, j, 1111);
}
}
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(jf.array[i][j].getActionCommand().equals("2048")){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[2].play();
for(int k=0;k<4;k++){
for(int l=0;l<4;l++){
jf.setImage(k, l, 2048);
}
}
}
}
}
}
//这里就是添加的点歌机制,按某某键就出现某某歌…………
else if(e.getKeyCode()==KeyEvent.VK_0){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
}
else if(e.getKeyCode()==KeyEvent.VK_1){
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[0].loop();
}else if(e.getKeyCode()==KeyEvent.VK_2){
audioarray[0].stop();
audioarray[4].stop();
audioarray[5].stop();
audioarray[3].loop();
}else if(e.getKeyCode()==KeyEvent.VK_3){
audioarray[0].stop();
audioarray[3].stop();
audioarray[5].stop();
audioarray[4].loop();
}else if(e.getKeyCode()==KeyEvent.VK_4){
audioarray[0].stop();
audioarray[3].stop();
audioarray[4].stop();
audioarray[5].loop();}
}
public boolean judge1(){//这里就是前面调用的判断是否占满格子的方法,很容易理解的,注意这里输出的返回值是布尔型的
int count=0;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(!(jf.array[i][j].getActionCommand().equals(""))){
count++;
}
}
}
if(count==16){
return true;
}else {
return false;
}
}
public boolean judge2(){//然后这个方法就是判断前后左右是否有相同的数字或者说代表数字的图片
String str5=jf.array[0][0].getActionCommand();
String str6=jf.array[0][3].getActionCommand();
String str7=jf.array[3][0].getActionCommand();
String str8=jf.array[3][3].getActionCommand();
String str9=jf.array[0][1].getActionCommand();
String str10=jf.array[1][0].getActionCommand();
String str11=jf.array[0][2].getActionCommand();
String str12=jf.array[1][3].getActionCommand();
String str13=jf.array[2][0].getActionCommand();
String str14=jf.array[3][1].getActionCommand();
String str15=jf.array[3][2].getActionCommand();
String str16=jf.array[2][3].getActionCommand();
int k=0;
if(judge1()){//首先肯定要先满足16个格子都满,因为只有这样才能进行判断到底输了没有
for(int i=1;i<3;i++){
for(int j=1;j<3;j++){//这里只能判断最最中间的那四个按钮的前后左右有没有相同的数字,那肯定不够,因为边角的那四个没有顾及到
String str=jf.array[i][j].getActionCommand();
String str1=jf.array[i+1][j].getActionCommand();
String str2=jf.array[i-1][j].getActionCommand();
String str3=jf.array[i][j-1].getActionCommand();
String str4=jf.array[i][j+1].getActionCommand();
if(!str.equals(str1)&&!str.equals(str2)&&!str.equals(str3)&&!str.equals(str4)&&!str5.equals(str9)&&!str5.equals(str10)&&!str6.equals(str11)&&!str6.equals(str12)&&!str7.equals(str13)&&!str7.equals(str14)&&!str8.equals(str15)&&!str8.equals(str16)){
k++;//因此这行代码如此的长……
}
}
}
}if(k>3){//括号里写k==4,也是可以的
return true;
}else
return false;
}
public void actionPerformed(ActionEvent e) {//这里的就是通过ActionListener监听器,将自己PS的照片添加进去即可
str=e.getActionCommand();
if(str.equals("玩 家 须 知")){
JFrame jf2=new JFrame("玩 家 须 知");
jf2.setSize(358, 610);
jf2.setUndecorated(true);
jf2.setFocusable(false);
jf2.setLocation(10, 35);;
// jf2.setLayout(null);
ImageIcon mr=new ImageIcon(this.getClass().getResource("123.PNG"));
jf2.setIconImage(mr.getImage());
JLabel jlxz=new JLabel();
ImageIcon wjxz=new ImageIcon(this.getClass().getResource("wjxz2.png"));
jlxz.setIcon(wjxz);
jf2.add(jlxz);
jf2.setVisible(true);
}
if(str.equals(" ")){//这里大家可能没有看出来,因为再来一局那个按钮上面是一张照片,当时就是初始化的时候设了一个空格字符,其实有更简单的方法就是直接setActionCommand方法设置……
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
jf.array[i][j].setActionCommand("");
jf.setImage(i, j, 0);
//因为要再来一局嘛,所以肯定要将内容清空,然后图片或者数字都设为空
}
}
jf.addran();
jf.addran();//然后循环结束之后当然要两次调用addran方法,因为初始的时候是有两个数字滴
jf.jl6.setText("0");//自然分数要记得清零
}
}
public void mouseMoved(MouseEvent e) {//这个调用mousemove的方法就是实现鼠标滑到那个按钮上的时候会变色……实际上就是用个if判断语句实现的背景颜色改变……
int x=e.getX();
int y=e.getY();
if(x>610&&x<770&&y>380&&y<450){
jf.j12.setBackground(new Color(174,191,212));
}else{
jf.j12.setBackground(new Color(238,238,238));
jf.j12.setForeground(new Color(124,166,216));}
if(x>810&&x<955&&y>380&&y<444){
jf.j13.setBackground(new Color(174,191,212));
}else{
jf.j13.setBackground(new Color(238,238,238));
jf.j13.setForeground(new Color(124,166,216));
}
if(x>700&&x<880&&y>440&&y<505){
jf.j14.setBackground(new Color(174,191,212));
}else{
jf.j14.setBackground(new Color(238,238,238));
jf.j14.setForeground(new Color(124,166,216));}
}