3

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.

CRT TV
  • 185
  • 2
  • 8

2 Answers2

0

Check the ischanged function. You are returning false if the corresponding values are not equal. Actually that means you are returning false if the board is not changed.

Or just do this: if(copy[i][j]==orij[i][j]) //here I just replaced “!=“ with “==“ return false;

Also like @Talik said use deep copy

Ajay
  • 76
  • 1
  • 8
-1

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

Talik
  • 305
  • 1
  • 11
  • 2
    yes, but he has an array of arrays, the arrays that are inside will not be clones, but literally the same arrays – Talik Feb 25 '19 at 18:29
  • see here: https://imgur.com/a/bsNmkGO the adresses for each of the sub arrays are the same – Talik Feb 25 '19 at 18:34