2048 change check method java

Joe Butler :

I am trying to write a 2048 game in java. I am trying to make it so it checks if the board has been changed, and if it was changed it will add to the move counter and add a number to the board. Otherwise it should not do anything. I am running into a bug where the method that checks if it was changed returns true every time and I can't seem to figure out why.

This is my isChecked method which should return true if the board has been changed and false otherwise.

public boolean isChanged(int [][]copy,int [][]orig){
        if(copy.length!=orig.length){
            System.out.print("INVALID MOVE");
            return false;
        }
        for(int i=0;i<copy.length;i++){
            for(int j=0;j<copy[i].length;j++){
                if(copy[i][j]!=orig[i][j]) {
                    System.out.print("INVLAID MOVE");
                    return false;
                }
            }
        }
        System.out.println("VALID MOVE");
        moves++;
        return true;
    }

Below are the method that handle left movement, combination, etc. the ones for up down and right are basically the same just with minor changes to change the direction so I decied not to include them in this post as I did not feel they were necessary

public void shiftLeft() {
        for (int x = 0; x < board.length; x++) {
            for (int y = board[x].length-1; y>0; y--) {
                if (board[x][y -1] == 0 && board[x][y] != 0) {
                    board[x][y - 1] = board[x][y];
                    board[x][y] = 0;
                    if(y!=board[x].length-1)
                        y+=1;
                }
            }

        }
    }
    public void combineLeft() {
        for (int x = 0; x < board.length; x++) {
            for (int y =board[x].length-2; y >=0; y--) {
                if(board[x][y]==board[x][y+1]){
                    board[x][y]*=2;
                    board[x][y+1]=0;
                }
            }
        }
    }
 public void left(){
    int [][] copy=board.clone();
    shiftLeft();
    shiftLeft();
    combineLeft();
    shiftLeft();
    if(isChanged(copy,board)==true)
        addNum();
}

addNum() is simply a function that adds a number to a random empty position on the board. board is the class variable(these are all in the same class) which is a 2d int array which represents the game board.

Talik :

try using:

Arrays.copyOf(..)

I think clone just copies the reference on the arrays of the board into a new array. So every time you change board, you change the clone

other options are as seen here: How to clone a multidimensional array in java?

a deep copy method

public static int[][] deepCopyIntMatrix(int[][] input) {
if (input == null)
    return null;
int[][] result = new int[input.length][];
for (int r = 0; r < input.length; r++) {
    result[r] = input[r].clone();
}
return result;

}

and cloning each row in the array manually

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=110464&siteId=1