1

I'm not certain if the title is the right way to word what I'm asking, sorry if it's not, but what I'm trying to do is create a memory match game using GUI. I have an array, and I've got the button printing an element from the array at random, but, the issue is, that I can have the same element printing multiple times. Is there a way to remove that element from being selected once it's used? If there isn't a way to do that, any ideas on how I could go about getting it to use each element only once? This is my current code:

    package MemoryMatching;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MemoryGUI extends JFrame implements MemoryMatch, ActionListener{
    JPanel mainPanel, boardPanel;
    JButton [][] gridButtons = new JButton[3][4];
    char cardArray[] = new char[12];
    int numInPlay;

    public MemoryGUI(){
        cardArray[0] = 'A';
        cardArray[1] = 'A';
        cardArray[2] = 'B';
        cardArray[3] = 'B';
        cardArray[4] = 'C';
        cardArray[5] = 'C';
        cardArray[6] = 'D';
        cardArray[7] = 'D';
        cardArray[8] = 'E';
        cardArray[9] = 'E';
        cardArray[10] = 'F';
        cardArray[11] = 'F';
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        boardPanel = new JPanel();
        boardPanel.setLayout(new GridLayout(4,3));
        setBoard();
        mainPanel.add(boardPanel, BorderLayout.CENTER);
        add(mainPanel);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btnClicked = (JButton) e.getSource();
        btnClicked.setEnabled(false);
        char randomChar = cardArray[(int)new Random().nextInt(cardArray.length)];

        btnClicked.setText(""+ randomChar);
        faceUp();

    }

    @Override
    public void setBoard() {
        for(int x = 0; x < cardArray.length; x++) {

        }
        for(int row=0; row<gridButtons.length; row++){
            for(int col=0; col<gridButtons[row].length;col++){
                gridButtons[row][col] = new JButton();
                gridButtons[row][col].addActionListener(this);
                gridButtons[row][col].setText("No peeking");
                boardPanel.add(gridButtons[row][col] );
                faceDown();
            }
        }
    }

    @Override
    public void isWinner() {
        // TODO Auto-generated method stub

    }
    @Override
    public void isMatch() {


    }
    @Override
    public void faceUp() {
        for(int x = 0; x < cardArray.length; x++) {
            for(int y = 0; y < cardArray[x]; y++) {

            }
        }
    }
    @Override
    public void faceDown() {


    }
}

what I'm currently getting is something like A B A A F B F D D E F C rather than: B A C D E F A B C F E D The first example having three A and one C, rather than two of each as in the second example. If possible I'd like to not be given the code outright, but a push towards the right direction.

2 Answers2

0

A simple solution: don't use an array.

Instead, use a List<Integer> and shuffle that list. Then simply use the last element of the list for your purpose - and then remove the last element from the list; and shuffle again.

Of course, you could also write a bit of code to keep track of all indexes that you already used, and keep asking for random indexes until you get one that wasn't used before.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I haven't been able to get the text to display properly, but using List instead of Array makes me feel much closer to the desired result. – HoneybunHero Oct 13 '17 at 17:15
0

For your code, I think the following code would do the trick.

array = ArrayUtils.removeElement(array, element)

If you really want to use an array, look into Fisher–Yates shuffle for random shuffling of an array.

However, my recommendation would be to not use an array. Use a list as the other answer suggested.

LostCause
  • 151
  • 1
  • 11