2016-04-13 16 views
-3

Ich habe dieses Programm über Memory-Spiel, dass, wenn der Benutzer alle Karten pairs sollte es warnt, dass. Der Benutzer hat gewonnen. Ich finde jedoch keine Möglichkeit, festzustellen, ob alle Karten gepaart sind. Kann mir jemand dabei helfen?Alert Benutzer, wenn alle Karten gepaart sind

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

Antwort

1

Warum nicht einfach eine Zählervariable erhöhen, wenn die Symbole übereinstimmen, und dann den Wert dieser Variablen überprüfen?

Vielleicht hier:

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

Sein scheint nicht funktioniert. I Hinzugefügt pairCounter = 1; und int MAX_PAIRS = 10; –

Verwandte Themen