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.