I’ve been making a game and its been going OK, but I’ve made it so random I can’t figure out how to implement a solve function.
I generate 36 buttons which are each assigned a numbered between 1-18 twice, so that there are matching pairs on the board to find. Each one is then assigned an icon of its number when clicked, and if you get the the two in the right order it reveals both. (A memory game)
I want to extract the command action from my buttons without clicking them, but I made the buttons like this:
generateArray();
String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
int count = 0;
for (int f=1; f < 7;f++){
for (int i=1; i < 7;i++){
btn[i] = new JButton(letters[f]+i);
btn[i].setName(letters[f]+i);
mainGameWindow.add(btn[i]);
btn[i].addActionListener(this);
String StringCommand = Integer.toString(randomArrayNum());
btn[i].setActionCommand(StringCommand);
count++;
if(count == 18){
generateArray();
}
}
}
I’ve tried running the button array in a loop, but it’s not providing me with what I want.
How do I get the buttons and their command actions from the button array?
The index you use for
btn[]is not correct, you only memorize 6 buttons!Instead of
btn[i]use everywherebtn[f * 6 + i], this way you will memorize ALL buttons.A word of advice, you should start your index at 0 not at 1, as Java arrays index are 0-based.