samedi 27 juin 2015

Deep copies of 2d object array

how can I make on a button press a new deep copy of a 2 dimensional array?

Basically I created a game field with buttons. The game is called sokoban and it's a puzzle. The player is moving from one button to the other with arrow keys on a fixed map (8x8 buttons). I want to implement an undo function. So I thought that I just create a deep copy of the JButton array before each move and save it into a stack. So when I press the undo button it calls the pop function of my stack. The problem is that I need to declare and initialize another JButton[][] where I can save the game field to before each move. Since I want infinite possible moves and also undos it seems impossible to me. I can't declare and initalize infite diffrent JButton[][] arrays. Any idea on how I can solve that?

That's how I copy a 2d object array:

    JButton[][] tempArray = new JButton[jbArray.length][jbArray[0].length];

    for (int i = 0; i < getJbArray().length; i++) {
        for (int j=0;j<getJbArray()[0].length;j++) {
            tempArray[i][j]=jbArray[i][j];
        }
    } 

    movesStack.push(tempArray);

Aucun commentaire:

Enregistrer un commentaire