2017-09-25 132 views
-3

我正在做這個記憶遊戲,我似乎無法弄清楚匹配卡的算法。基本記憶遊戲Java

就像兩張牌相同,它會被禁用,否則會再次隱藏牌。

每次我點擊一張卡時,它都會保持打開狀態,當我隨機選擇另一張卡時,出於某種原因,其他卡會再次關閉。

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

請注意,您在'dolls'中有一個拼寫錯誤:''ugly4.jpg「'而不是'」Ugly4.jpg「'。 –

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

好吧它有點工作,但它不會顯示第二張卡片,因此當我玩這個遊戲時,我會很難贏得 –

+0

我需要的是我需要揭示卡,如果他們匹配,他們得到禁用或保持透露其他,他們回去隱藏 –

+0

@IceRoxas我已經更新了我的答案。 –