I am working on a small game where the player clicks a button whichs value is from a 2-dimensional Array.This is how the game looks In case the player wants to revert his action I want to save the steps the player took in a List, and when he clicks the "revert"-button the last item of the list gets reverted. Problem is I don't know how to the save the index of a 2-dimensional Array to a list.
My code is far for the array is following
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
counter++;
buttons[i][j] = new JButton("" + counter);
buttons[i][j].setBackground(Color.white);
buttons[i][j].addActionListener(new ButtonPress());
fenster.add(buttons[i][j]);
And the code for the ActionListener is here:
public void actionPerformed(ActionEvent e) {
amountMoves++;
moves.setText("Moves: " + amountMoves);
int x = 0;
int y = 0;
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons[i].length; j++) {
if (((JButton) e.getSource()).equals(buttons[i][j])) {
x = i;
y = j;
break;
}
}
}
}
Lists aren't my strongpoint so so far I only have this:
ArrayList<Integer> steps = new ArrayList<Integer>();
I know I can add a element with steps.add() and delete the last element with steps.remove(steps.size() - 1);, but that's all so far
Any help is greatly appreciated
EDIT: With the help of @Johnny Mopp and @SSP I figured out what to do. First I created a List of the type String ArrayList<String> steps = new ArrayList<String>(); (you can alternativly use a List of the type JButton) and add it in the Actionlister with steps.add(buttons[i][j].getText());
ArrayList<Button>)? Or use an array ofPair.....