所有卡配对时提醒用户

时间:2016-04-13 05:29:35

标签: java swing awt

我有关于记忆游戏的这个程序,当用户配对所有卡时,应该提醒它。用户赢了。但是我无法找到识别所有卡是否配对的方法。有人可以帮我这个。

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

1 个答案:

答案 0 :(得分:1)

为什么不简单地在图标匹配时递增计数器变量,然后检查此变量的值?

也许在这里:

IS NULL