2013-07-23 109 views
-3

我目前正在研究一個記憶遊戲項目,點擊兩張卡片來檢查圖片是否相同。我面臨的問題是,如果兩張卡沒有相同的照片,則第二張照片不會出現,第一張照片也會關閉。Java:記憶遊戲

這是我認爲有某種錯誤的代碼。

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; 
       } 
+1

因此,您只需將所有代碼轉儲到我們並說「修復它」?這不是Stack Overflow的工作原理。我們不是你的個人代碼調試器 - 你實際上必須嘗試一些東西! – Doorknob

+2

發佈所有的代碼我猜是好的,但給我們一個詳細的解釋,特別是你認爲問題可能來自哪裏,並且一般來說可能是相關的任何信息。 – scottyseus

+0

我很抱歉,我不是那個意思,而且我在2-3小時之內就犯了這個錯誤,並嘗試過各種方法,但仍然無法克服它,所以我認爲如果有人想要發佈我的整個代碼,嘗試運行它以查看錯誤本身:s我只是嘗試對unlockCards()進行註釋並顯示第二張卡片,但不應該按順序執行程序,爲什麼調用此方法會阻止圖像顯示,即使我在它之前使用Thread.sleep .. – HishamGarout

回答

1

你的代碼告訴它這樣做。

你可能想的是else條款做這樣的事情:隱藏卡解鎖之前

  • 延時2秒。

也許

  • 將卡單獨直到你再次打開其中的一個點擊。這將需要櫃檯從1到2,然後到3,在那裏你把它們翻過來。

不會爲你寫代碼,但它的線索。