Java 为什么赢了';我的图像按钮不能为我的滑动益智游戏洗牌吗?

Java 为什么赢了';我的图像按钮不能为我的滑动益智游戏洗牌吗?,java,android,multidimensional-array,Java,Android,Multidimensional Array,我正在尝试使用Android Studio创建一个超级简单的2x2滑动拼图。尽管2x2并不被推荐,但我想让这个游戏尽可能简单 在content_game.xml文件中,我使用ImageButtons创建了一个2x2表。在我的Game.java文件中,在OnCreate()函数中,我引用了每个小部件,创建并调用了shuffle()函数来混合它们。在同一个活动中,我创建了一个“新拼图”按钮来再次洗牌图像 完整Game.java文件: gameGrid[0][0] = (ImageBu

我正在尝试使用Android Studio创建一个超级简单的2x2滑动拼图。尽管2x2并不被推荐,但我想让这个游戏尽可能简单

在content_game.xml文件中,我使用ImageButtons创建了一个2x2表。在我的Game.java文件中,在
OnCreate()
函数中,我引用了每个小部件,创建并调用了
shuffle()
函数来混合它们。在同一个活动中,我创建了一个“新拼图”按钮来再次洗牌图像

完整Game.java文件:

        gameGrid[0][0] = (ImageButton) findViewById(R.id.square1);
        gameGrid[0][1] = (ImageButton) findViewById(R.id.square2);
        gameGrid[1][0] = (ImageButton) findViewById(R.id.square3);
        gameGrid[1][1] = (ImageButton) findViewById(R.id.square4);
        newPuzzleButton = (Button) findViewById(R.id.newPuzzleBtn);
        movesTextView = (TextView) findViewById(R.id.movesTextView);

 //event handlers
        for (int x = 0; x < gameGrid.length; x++) {
            for (int y = 0; y < gameGrid[x].length; y++) {
                gameGrid[x][y].setOnClickListener(this);
            }
        }

        newPuzzleButton.setOnClickListener(this);
        //scramble grid for new puzzle
        scramble();

        //starting values
        moves = 0;
        gameOver = false;
        imgName = "shoes"; //default puzzle img
        movesTextView.setText(String.valueOf(moves));
        gameString = "    "; //4 spaces (1 for each square)
    }

   private void scramble() {
        Random random = new Random();
        int x_index;
        int y_index;
        ImageButton temp[][] = new ImageButton[2][2];

        for (int x = gameGrid.length - 1; x > 0; x--) {
            for (int y = gameGrid[x].length - 1; y > 0; y--) {
                x_index = random.nextInt(x + 1);
                y_index = random.nextInt(y + 1);
                temp[x][y] = gameGrid[x_index][y_index];
                gameGrid[x_index][y_index] = gameGrid[x][y];
                gameGrid[x][y] = temp[x][y];
            }
        }


    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.newPuzzleBtn:
                scramble();

                //set starting values
                moves = 0;
                gameOver = false;
                imgName = "shoes"; //default puzzle img
                movesTextView.setText(String.valueOf(moves));
                gameString = "    "; //4 spaces (1 for each square)
                break;
            default:
            ImageButton b = (ImageButton) v;
            if (b.getId() == R.id.square4) {
                moves = 4;
            }
            else if (b.getId() == R.id.square3){
                moves = 3;
            }
            else if (b.getId() == R.id.square2){
                moves = 2;
            }
            else {
                moves = 1;
            }
            movesTextView.setText(String.valueOf(moves));
        }
    }


您的代码不会移动
ImageButton
s的位置

类似的情况:如果你把四个人的电话号码写在一张纸上,将他们出现在纸上的顺序改变不会给这四个人分配新的号码

我想您为每个
ImageButton
s分配了一个固定的可绘制资源。您可以使用可绘制资源的二维数组

private int[][] images = {R.drawable.top_left, R.drawable.top_right, R.drawable.bottom_left, R.drawable.bottom_right};
其中top_left.png、top_right.png、bottom_left.png和bottom_right.png是拼图的四个部分

如果您洗牌,然后将可绘制资源分配给
ImageButton
s,您应该会得到所需的效果

请注意,scramble()中的循环应该运行到“>=0”而不是“>0”

另一种洗牌图像资源的方法是使用
SparseIntArray
,它类似于
列表
,但可用于基本数据类型
int
,而不是
整数
。这有助于提高性能

private SparseIntArray images = new SparseIntArray();
让我们介绍两种新方法

private void initializeImages() {
    images.clear();
    images.append(0,R.drawable.ic_phone_24dp);
    images.append(1,  R.drawable.ic_face_24dp);
    images.append(2, R.drawable.ic_favorite_24dp);
    images.append(3, R.drawable.ic_star_24dp);
}

onCreate()中使用它们

onClick()中


我试过了!我在嵌套for循环中使用
setImageResource(images[x][y])
时,创建了这个私有int,然后将它们洗牌,这样它就可以运行四个部分。不过,在测试时,它只显示右下角的图片已被洗牌。我已取消分配固定资源,因此应用程序将前3个框显示为空白,而第4个框显示为随机块。您能进一步帮助我吗?@SyzzleFizz-请分享全部相关代码(不仅仅是片段,还有xml文件),然后我将用完整的.java和.xml代码研究iTunes更新的问题。再次感谢您抽出时间。
private SparseIntArray images = new SparseIntArray();
private void initializeImages() {
    images.clear();
    images.append(0,R.drawable.ic_phone_24dp);
    images.append(1,  R.drawable.ic_face_24dp);
    images.append(2, R.drawable.ic_favorite_24dp);
    images.append(3, R.drawable.ic_star_24dp);
}
private void shuffleImageResourceIds() {
    Random random = new Random();
    for (int x = gameGrid.length - 1; x >= 0; x--) {
        for (int y = gameGrid[x].length - 1; y >= 0; y--) {
            int size = images.size();
            if(size > 1){
                int index = random.nextInt(size);
                int imageResourceId = images.valueAt(index);
                images.removeAt(index);
                gameGrid[x][y].setImageResource(imageResourceId);
            }
            else {
                gameGrid[x][y].setImageResource(images.valueAt(0));
            }
        }
    }
}
newPuzzleButton.setOnClickListener(this);
//scramble grid for new puzzle
//scramble();
initializeImages();
shuffleImageResourceIds();
@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.newPuzzleBtn:
            // scramble();
            initializeImages();
            shuffleImageResourceIds();

            //set starting values like before ...

            break;
        default:
           // handle moves like before ...
    }
}