Android 在collections.shuffle之后获取已洗牌项的索引

Android 在collections.shuffle之后获取已洗牌项的索引,android,collections,indexing,shuffle,Android,Collections,Indexing,Shuffle,我正在尝试用android做一个拼图游戏应用程序。在本文中,我将一个位图分割成许多小块。这些块然后显示在GridView中,现在我需要将它们洗牌。然后,我需要知道每个图像块的实际位置(工件应该在哪里,它在图像中的实际位置)和当前位置(工件当前所在的位置)实际位置和当前位置是两个整数数组。所以,有没有一种方法可以让我在洗牌后获得每个图像块的currentPosition和actualPosition,这样在用户每次移动之后,我就可以检查每个图像块的actualPosition是否等于它的curre

我正在尝试用android做一个拼图游戏应用程序。在本文中,我将一个
位图
分割成许多小块。这些块然后显示在
GridView
中,现在我需要将它们洗牌。然后,我需要知道每个图像块的实际位置(工件应该在哪里,它在图像中的实际位置)和当前位置(工件当前所在的位置)<代码>实际位置和当前位置是两个整数数组。所以,有没有一种方法可以让我在洗牌后获得每个图像块的
currentPosition
actualPosition
,这样在用户每次移动之后,我就可以检查每个图像块的
actualPosition
是否等于它的
currentPosition
。如果是这样,用户将赢得游戏。谁能帮帮我吗

下面是纯Java的数字益智游戏。可以从命令行运行。 它会在每次移动后重新打印整个矩阵(不好看)。它演示了基本游戏。 我希望大部分代码都是不言自明的。这显示了游戏的基本二维映射、位置跟踪和基于数字的验证。玩得开心

package madhav.turangi.basic.game;

import java.util.Random;
import java.util.Scanner;

public class NumberPuzzle {

    int size;
    int[][] arr;
    int spaceRow;
    int spaceCol;
    int turnsTook;

    public NumberPuzzle(int size) {
        this.size = size;
        arr =  new int[size][size];
    }

    void init()
    {
        for(int r=0; r<size; r++)
        {
            for(int c=0; c<arr[r].length; c++)
            {
                arr[r][c] = r*size + c + 1; // row-column of cell to its value equation
            }
        }
        spaceRow = spaceCol = size - 1; // bottom-right cell index
    }

    int readUserInput()
    {
        int value = -1;
        boolean valid = false;
        do {
            System.out.printf("To move space [0 - Up, 1 - Down, 2 - Left, 3 - Right] : ? ");
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            try 
            {
                value = Integer.parseInt(line);
                valid = (value>=0 && value<=3);
            }
            catch(NumberFormatException ne)
            {
            }
            if(! valid) System.out.println("== Invalid ==");
        } while (! valid);
        return value;
    }

    void swap(int aRow, int aCol, int withRow, int withCol)
    {
        int temp = arr[aRow][aCol];
        arr[aRow][aCol] = arr[withRow][withCol];
        arr[withRow][withCol] = temp;
    }

    boolean moveUp()
    {
        if(spaceRow != 0)
        {
            int newSpaceRow = spaceRow - 1;
            swap(spaceRow, spaceCol, newSpaceRow, spaceCol);
            spaceRow--;
            return true;
        }
        else
        {
            return false;
        }
    }

    boolean moveDown()
    {
        if(spaceRow != size-1)
        {
            int newSpaceRow = spaceRow + 1;
            swap(spaceRow, spaceCol, newSpaceRow, spaceCol);
            spaceRow++;
            return true;
        }
        else
        {
            return false;
        }
    }

    boolean moveRight()
    {
        if(spaceCol != size-1)
        {
            int newSpaceCol = spaceCol + 1;
            swap(spaceRow, spaceCol, spaceRow, newSpaceCol);
            spaceCol++;
            return true;
        }
        else
        {
            return false;
        }
    }

    boolean moveLeft()
    {
        if(spaceCol != 0)
        {
            int newSpaceCol = spaceCol - 1;
            swap(spaceRow, spaceCol, spaceRow, newSpaceCol);
            spaceCol--;
            return true;
        }
        else
        {
            return false;
        }
    }

    void shuffle()
    {
        Random rnd = new Random(System.currentTimeMillis());
        boolean moved = false;
        int attemptCount = 1;
        int maxMoves = 20;
        for(int moveCount=0; moveCount<maxMoves; moveCount++, attemptCount++)
        {
            int randomMoveDir = rnd.nextInt(4);
            moved = move(randomMoveDir);
            if(! moved) moveCount--; //ensure maxMoves number of moves
        }
        System.out.printf("Shuffle attempts %d\n",attemptCount);
    }

    boolean move(int dir)
    {
        boolean moved = false;
        switch(dir)
        {
            case 0 : // up
                moved = moveUp();
                break;
            case 1 : // down
                moved = moveDown();
                break;
            case 2 : // left
                moved = moveLeft();
                break;
            case 3 : // right
                moved = moveRight();
                break;
        }
        return moved;
    }

    void prnArray()
    {
        System.out.println("-- --  --  --  --");
        for(int[] row : arr)
        {
            for(int cellValue : row)
            {
                String v = (cellValue == 16 ? "" : String.valueOf(cellValue));
                System.out.printf("%4s", v);
            }
            System.out.println();
        }
        System.out.println("-- --  --  --  --");
    }

    boolean validate()
    {
        for(int r=0; r<size; r++)
        {
            for(int c=0; c<arr[r].length; c++)
            {
                if(arr[r][c] != (r*size + c + 1))
                {
                    return false;
                }
            }
        }
        return true;
    }

    boolean oneTurn()
    {
        int dir = readUserInput();
        boolean moved = move(dir);
        boolean won = false;
        if(moved)
        {
            turnsTook++;
            prnArray();
            won = validate();
        }
        else
        {
            System.out.println("= Invalid =");
        }
        return won;
    }

    void play()
    {
        init();

        System.out.println("Before shuffle");
        prnArray();

        shuffle();
        prnArray();

        boolean won = false;
        while(! won)
        {
            won = oneTurn();
        }
        System.out.printf("Won in %d\n", turnsTook);
    }


    public static void main(String[] args) 
    {
        NumberPuzzle puzzle = new NumberPuzzle(4);
        puzzle.play(); 
    }

}
包madhav.turangi.basic.game;
导入java.util.Random;
导入java.util.Scanner;
公共类编号模糊{
整数大小;
int[]arr;
int斯派洛;
int spaceCol;
内turnsTook;
公用号码模糊(整数大小){
这个。大小=大小;
arr=新整数[大小][大小];
}
void init()
{

for(int r=0;rI假设将有一个空块(chunk)如果只有这样,就没有几个重要的事情要考虑。如果使用一维数组很难跟踪,一个完整的随机洗牌有时会使谜题无法解决。请考虑将你的2个模糊视图映射到2个模糊数组。我在大学时做过类似的事,但它是数字P。在C++中使用CUI。核心逻辑和设计可以应用于这里。从2维数组开始,将位图块映射到该数组。