2016-04-13 24 views
-3

Bu oyunda, hafıza kartları hakkında bir program var. Kullanıcı kazandı. Ancak, tüm kartların eşleştirilip eşleşmediğini belirleyemiyorum. Birisi bana bu konuda yardımcı olabilir.Tüm kullanıcılar eşleştirildiğinde uyarı kullanıcısı

public class MemoryGame extends JFrame { 

static String files[] = { 
    "japan.jpg", 
    "greece.jpg", 
    "canada.jpg", 
    "brazil.jpg", 
    "jamaica.jpg"}; 
static JButton buttons[]; 
ImageIcon closedIcon; 
int numButtons; 
ImageIcon icons[]; 
Timer myTimer; 

int numClicks = 0; 
int oddClickIndex = 0; 
int currentIndex = 0; 

public MemoryGame() { 
    // Set the title. 

    setTitle("Memory Game"); 

    // Specify an action for the close button. 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // Create a BorderLayout manager. 
    setLayout(new GridLayout(2, files.length)); 

    closedIcon = new ImageIcon("closed.JPG"); 
    numButtons = files.length * 2; 
    buttons = new JButton[numButtons]; 
    icons = new ImageIcon[numButtons]; 
    for (int i = 0, j = 0; i < files.length; i++) { 
     icons[j] = new ImageIcon(getClass().getResource(files[i])); 
     buttons[j] = new JButton(""); 
     buttons[j].addActionListener(new ImageButtonListener()); 
     buttons[j].setIcon(closedIcon); 
     add(buttons[j++]); 

     icons[j] = icons[j - 1]; 
     buttons[j] = new JButton(""); 
     buttons[j].addActionListener(new ImageButtonListener()); 
     buttons[j].setIcon(closedIcon); 
     add(buttons[j++]); 
    } 

    // randomize icons array 
    Random gen = new Random(); 
    for (int i = 0; i < numButtons; i++) { 
     int rand = gen.nextInt(numButtons); 
     ImageIcon temp = icons[i]; 
     icons[i] = icons[rand]; 
     icons[rand] = temp; 
    } 


    setSize(300,200); 
    setVisible(true); 

    myTimer = new Timer(1000, new TimerListener()); 
    // myTimer.start(); 
} 

private class TimerListener implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
     buttons[currentIndex].setIcon(closedIcon); 
     buttons[oddClickIndex].setIcon(closedIcon); 
     myTimer.stop(); 
    } 
} 

private class ImageButtonListener implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 

     // we are waiting for timer to pop - no user clicks accepted 
     if (myTimer.isRunning()) 
      return; 

     numClicks++; 
     System.out.println(numClicks); 

     // which button was clicked? 
     for (int i = 0; i < numButtons; i++) { 
      if (e.getSource() == buttons[i]) { 
       buttons[i].setIcon(icons[i]); 
       currentIndex = i; 
      } 
     } 

     // check for even click 
     if (numClicks % 2 == 0) { 
      // check whether same position is clicked twice! 
      if (currentIndex == oddClickIndex) { 
       numClicks--; 
       return; 
      } 
      // are two images matching? 
      if (icons[currentIndex] != icons[oddClickIndex]) { 
       // show images for 1 sec, before flipping back 
       myTimer.start(); 
      } 
     } else { 
      // we just record index for odd clicks 
      oddClickIndex = currentIndex; 
     } 
    } 
} 

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

cevap

1

simgeler eşleştiğinde Neden sadece bir karşı değişkeni arttırın ve sonra bu değişkenin değerini kontrol edecek Belki burada

:

if (icons[currentIndex] != icons[oddClickIndex]) { 
    // show images for 1 sec, before flipping back 
    myTimer.start(); 
} else { 
    pairCounter++; 
    if (pairCounter >= MAX_PAIRS) { 
     // NOTIFY USER 
     // end game 
     // reset pairCounter 
    } 
} 
+0

Onun çalışmıyor görünüyor. Ben Eklendi pairCounter = 1; ve int MAX_PAIRS = 10; –

İlgili konular