在collections.shuffle之后获取混洗项目的索引

时间:2014-06-26 05:44:51

标签: android collections indexing shuffle

我正在尝试在Android中做一个拼图游戏应用程序。在这里,我将Bitmap拆分成许多小块。然后这些块显示在GridView现在我需要将它们洗牌。然后,我需要知道每个图像块的actualPosition(应该是该片段的位置,它在图像中的实际位置)及其currentPosition(片段当前所在的位置)。 actualPositioncurrentPosition是2个整数数组。那么有一种方法可以让我在混洗后获得每个图像组块currentPositionactualPosition,这样在用户进行的每次移动之后我都可以检查每个图像组块。 actualPosition等于其currentPosition。如果是这样,则用户赢得游戏。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

以下是纯Java中的数字拼图游戏。可以从命令行运行。 它在每次移动后重新打印整个矩阵(不漂亮)。它演示了基本游戏。 我希望大多数代码都是自我解释的。这显示了游戏的基本2-dim映射,位置跟踪,基于数字的验证。玩得开心。

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(); 
    }

}