-2

I am trying to code a memory matching game - the standard type of concentration game where the player is shown picture cards, they're flipped over, and they have to match the corresponding cards.

There's a few things that have me completely at a loss as to where I should even begin, and I would really appreciate it if I could get some advice. I'm not sure how I would shuffle the images in an array of Buttons each time I restarted the game. I considered making an integer matrix and shuffling the numbers and images separately, but 1) I'm not sure how to shuffle ImageIcons on a button, and 2) the 2 numbers that are supposed to match up would have different images.

I also considered making a String array to shuffle the filenames of the ImageIcons, but I think that would require reassigning each individual image icon (there are 48 cards and 24 pairs so that would take up a lot of time). Could I get some ideas on how to approach this problem? Is there an easier/more efficient solution than the ones I've thought up? I know there's a Fisher-Yates shuffle algorithm used for cards but I can't quite understand it.

mk8139
  • 57
  • 6

1 Answers1

0

Use Collection class to shuffle your data. For example, you can shuffle an array of dice, numbered from 1-6 as follow

public ImageIcon [] shuffle() {
        String [] dicesName = {"dice1.png", "dice2.png", "dice3.png", "dice4.png", "dice5.png", "dice6.png"};
        List<String> dices = Arrays.asList(dicesName);
        Collections.shuffle(dices);

        ImageIcon[] dicesShuffled = new ImageIcon[6];
        for (int i = 0; i < dices.size(); i++) {
            dicesShuffled[i] = new ImageIcon(getClass().getResource(dices.get(i))); 
        }
        return dicesShuffled;
    }

You can call this method every time you want to shuffle the dices. It returns an imageIcon shuffled array.

blueFalcon
  • 111
  • 9
  • So, just to clarify: for example, I could shuffle a String array of filenames and then, using a for-loop, instantiate the new shuffled filenames as ImageIcons in the JButton array? – mk8139 May 17 '16 at 00:04
  • Yes. Note that the for-loop method to create ImageIcons only works if the filenames are located in same directory as your main class. – blueFalcon May 17 '16 at 15:29
  • how would I ensure that the ImageIcons would work, if the path of the filenames changes? It would be in the same directory as my main class, but hypothetically, if I moved the whole directory to a different computer, the path of the images would be different, it wouldn't be /Desktop/icons/bob.jpg anymore, for example. Would I have to edit the path? – mk8139 May 17 '16 at 22:45
  • No. Just make sure your images are in the same folder as your main class, getClass().getResource("filename"); will ensure that the path is correct. If you how ever store all of your images in a sub-folder within the folder where your main class is located this won't work. Take a look at the getResource() method, http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29. – blueFalcon May 18 '16 at 00:33