【问题标题】:Basic Memory Game Java基本记忆游戏 Java
【发布时间】:2017-09-25 08:10:35
【问题描述】:

我正在做这个记忆游戏,但我似乎无法弄清楚匹配卡片的算法。

就像如果两张卡相同,它会被禁用,否则再次隐藏卡。

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

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();
    }
}

【问题讨论】:

  • 请注意您在dolls 中有错字:"ugly4.jpg" 而不是"Ugly4.jpg"
  • 是的,我得到了那个,嘿嘿抱歉:)

标签: java arrays memory


【解决方案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();
}

【讨论】:

  • 好吧,它有点工作,但它不会显示我点击的第二张卡片,因此当我玩游戏时,我很难赢
  • 我需要向卡片显示,如果它们匹配,它们将被禁用或保持显示,否则它们会返回隐藏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多