简体   繁体   English

2048 更改检查方法java

[英]2048 change check method java

I am trying to write a 2048 game in java.我正在尝试用 Java 编写一个 2048 游戏。 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.我遇到了一个错误,其中检查它是否已更改的方法每次都返回 true,我似乎无法弄清楚原因。

This is my isChecked method which should return true if the board has been changed and false otherwise.这是我的 isChecked 方法,如果电路板已更改,它应该返回 true,否则返回 false。

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. addNum() 只是一个将数字添加到板上随机空位置的函数。 board is the class variable(these are all in the same class) which is a 2d int array which represents the game board. board 是类变量(这些都在同一个类中),它是一个 2d int 数组,表示游戏板。

Check the ischanged function.检查 ischanged 函数。 You are returning false if the corresponding values are not equal.如果相应的值不相等,您将返回 false。 Actually that means you are returning false if the board is not changed.实际上,这意味着如果板未更改,您将返回 false。

Or just do this: if(copy[i][j]==orij[i][j]) //here I just replaced “!=“ with “==“ return false;或者就这样做: if(copy[i][j]==orij[i][j]) //这里我只是用“==”替换了“!=” return false;

Also like @Talik said use deep copy也像@Talik 说使用深拷贝

try using:尝试使用:

Arrays.copyOf(..) Arrays.copyOf(..)

I think clone just copies the reference on the arrays of the board into a new array.我认为 clone 只是将电路板数组上的引用复制到一个新数组中。 So every time you change board, you change the clone所以每次你换板子,你就改变了克隆

other options are as seen here: How to clone a multidimensional array in java?其他选项如下所示: 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并手动克隆阵列中的每一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM