基本记忆游戏Java

时间:2017-09-25 08:10:35

标签: java arrays memory

我正在做这个记忆游戏,我似乎无法找出匹配卡片的算法。

如果两张牌相同,则会被禁用,否则会再次隐藏牌。

每次点击一张卡片时,它都会保持打开状态,当我随机选择另一张卡片时,由于某种原因,其他卡片会再次关闭。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MemGame extends JFrame implements ActionListener
{
GridLayout three = new GridLayout(4,4);

String dolls[]= {"Ugly1.jpg","Ugly1.jpg","Ugly2.jpg","Ugly2.jpg","Ugly3.jpg","Ugly3.jpg","ugly4.jpg","Ugly4.jpg","Ugly5.jpg","Ugly5.jpg","Ugly6.jpg","Ugly6.jpg","Ugly7.jpg","Ugly7.jpg","Ugly8.jpg","Ugly8.jpg"};
JButton button[]= (new JButton[dolls.length]);
int current,shuffle,trans;
int check=0;
int holder[]=new int [2];

String container[]={"",""};

public MemGame()
{       
    Container c = getContentPane();
    c.setLayout(three);

        for(current=0;current<dolls.length;current++)
        {

            int shuffle=(int)(Math.random()*dolls.length);
            String hold=dolls[current];
            dolls[current]=dolls[shuffle];
            dolls[shuffle]=hold;

        }

        for(int x=0;x<dolls.length;x++)
        {   
            button[x]=new JButton();
            c.add(button[x]);
            button[x].addActionListener(this);
        }

    setVisible(true);
    setSize(500,500);
}
public void actionPerformed(ActionEvent e)
{
        for(int x=0;x<dolls.length;x++)
        {   
            if(e.getSource()==button[x])
            {
                button[x].setText("");
                button[x].setIcon(new ImageIcon(dolls[x]));
                button[x].setEnabled(false);

            //This is where my problem starts.... I think

                check++;

                if(check==1)
                {
                    container[0]=dolls[x];
                    holder[0]=x;


                }
                if(check==2)
                {

                    container[1]=dolls[x];
                    holder[1]=x;

                }
                if(check==3)
                {
                    if(container[0].equals(container[1]))
                    {
                        button[holder[0]].setEnabled(false);
                        button[holder[1]].setEnabled(false);
                    }   
                    else
                    {
                        button[holder[0]].setEnabled(true);
                        button[holder[0]].setIcon(new ImageIcon());

                        button[holder[1]].setEnabled(true);
                        button[holder[1]].setIcon(new ImageIcon());

                    }
                    check=1;


                }
            }
        }
    }   

    public static void main (String args[])
    {
        new MemGame();
    }
}

1 个答案:

答案 0 :(得分:1)

check等于2时,在第二次点击时检查有效组合,如果按钮不匹配则重新启用按钮。

要在处理事件时重绘屏幕,此逻辑需要在新线程中运行,因为负责更新UI的人已经忙于执行事件处理程序代码。

@Override
public void actionPerformed(ActionEvent e) {
    Thread t = new Thread(() -> {
        for (int x = 0; x < dolls.length; x++) {
            if (e.getSource() == button[x]) {
                button[x].setText("");
                button[x].setIcon(new ImageIcon(dolls[x]));
                button[x].setEnabled(false);

                check++;
                if (check == 1) {
                    container[0] = dolls[x];
                    holder[0] = x;
                }
                if (check == 2) {
                    container[1] = dolls[x];
                    holder[1] = x;

                    try {
                        Thread.sleep(500L);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }

                    if (!container[0].equals(container[1])) {
                        button[holder[0]].setEnabled(true);
                        button[holder[0]].setIcon(new ImageIcon());
                        button[holder[1]].setEnabled(true);
                        button[holder[1]].setIcon(new ImageIcon());
                    }
                    check = 0;
                }
            }
        }
    });
    t.start();
}