-3

I am currently working on a memory game project, where you click on two cards to check if the pictures are the same or not. The problem that I am facing is that if both cards don't have the same picture, the second picture does not appear and the first one closes as well.

Here is the code which I think has some sort of an error in.

 public void actionPerformed(ActionEvent e)         
    {                    
                        clicks +=1; 
                Card clicked = (Card)e.getSource(); 
                clicked.changeColor();

                for(int i=0;i<16;i++) 
                    if(clicked == cards.get(i)) 
                        openCardIndices.add(i); 


                 if(clicks==2 && openCardIndices.get(openCardIndices.size()-1)!=openCardIndices.get(openCardIndices.size()-2)) 
                 {
                    if(cards.get(openCardIndices.get(openCardIndices.size()-1)).equals(cards.get(openCardIndices.get(openCardIndices.size()-2))))
                    {
                        (cards.get(openCardIndices.get(openCardIndices.size()-1))).removeActionListener(this);
                        (cards.get(openCardIndices.get(openCardIndices.size()-2))).removeActionListener(this);
                    }
                    else
                    {

                        openCardIndices.remove(openCardIndices.size()-1);
                        openCardIndices.remove(openCardIndices.size()-1);
                        //lockCards();
                        unlockCards();
                    }
                    clicks = 0;
                 }   
4

1 回答 1

1

您的代码告诉它这样做。

您可能希望该else子句执行以下操作:

  • 延迟 2 秒,然后隐藏解锁的卡片。

或者可能

  • 将卡片放在一边,直到您再次单击其中一张。这将需要计数器从 1 变为 2,然后变为 3,然后将它们转回。

不会为您编写代码,但它是一个线索。

于 2013-07-23T22:17:17.817 回答