为记忆匹配游戏洗牌2天阵列

时间:2015-11-22 23:48:26

标签: java arrays match shuffle

您好我正在尝试创建一个内存匹配游戏。我能够随机化数组状态以获得不同的值,但在我将状态数组的8个元素中的2个加载到名为tempGrid [] []的数组之后,我似乎无法对内容进行填充。例如,我得到的结果是:

AL AK AZ AR 
AL AK AZ AR 
CA CO CT DE
CA CO CT DE

但是我想要

AK AL CA CT
CT DE CA AK
DE AL AR AR
CO AZ AZ CO

我怎样才能做到这一点?谢谢你的帮助。代码

import java.util.*;
public class createGrid
{

    private static String[][] tempGrid ={{"00", "01", "02", "03"},
                                      {"10", "11", "12", "13"},
                                      {"20", "21", "22", "23"},
                                      {"30", "31", "32", "33"},
                                     };

    static String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE",
                              "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", 
                              "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS",
                              "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", 
                              "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
                              "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV",
                              "WI", "WY"};
     static String[][] themeGrid = new String[4][4];
    public static void setThemeGrid()
    {   
        //filles temp grid with 8 elements x2
        for (int counter = 0; counter < 4; counter++)
        {
            themeGrid[0][counter] = states[counter];
            themeGrid[0][counter] = states[counter];
            themeGrid[0][counter] = states[counter];
            themeGrid[0][counter] = states[counter];

            themeGrid[1][counter] = states[counter];
            themeGrid[1][counter] = states[counter];
            themeGrid[1][counter] = states[counter];
            themeGrid[1][counter] = states[counter];

            themeGrid[2][counter] = states[counter+4];
            themeGrid[2][counter] = states[counter+4];
            themeGrid[2][counter] = states[counter+4];
            themeGrid[2][counter] = states[counter+4];

            themeGrid[3][counter] = states[counter+4];
            themeGrid[3][counter] = states[counter+4];
            themeGrid[3][counter] = states[counter+4];
            themeGrid[3][counter] = states[counter+4];
        }
         //Collections.shuffle(Arrays.asList(themeGrid);
         //doessn't seem to work

        System.out.println("This is the randomized theme grid");
        for (int row = 0; row < 4; row++)
        {
            for (int column = 0; column < 4; column++)
            { 
                System.out.printf("%s ", themeGrid[row][column]);


            }System.out.println();
        }
    }



    public static void main(String[] args)
    {
        setThemeGrid();

    }     
}

2 个答案:

答案 0 :(得分:0)

随机化列表或数组的最简单方法是创建一个返回随机值的Comparator,如下所示:

public class RandomComparator<T> implements Comparator<T>{
    private Random rand = new Random();

    @Override
    public int compare(T a, T b) {
        return rand.nextInt(2) - 1;
    }   
}

您可以将该RandomComparator与Arrays.sort()Collections.sort()一起使用,以随机化任何数组或集合的顺序。

对于像你这样的多向量阵列,你需要分别对每一行进行随机播放&#39;然后随机播放列#39;可能多次让他们变得好混和。

答案 1 :(得分:0)

我不清楚你为什么要四次设置themeGrid的每个元素。但我相信这会做你想要的。

public static void setThemeGrid()
{   
    //fills temp grid with 8 elements x2
    List themeList = new ArrayList()
    for (int counter = 0; counter < 4; counter++)
    {
        themeList.add(states[counter]);
        themeList.add(states[counter]);
        themeList.add(states[counter+4]);
        themeList.add(states[counter+4]);
    }
    Collections.shuffle(themeList);

    System.out.println("This is the randomized theme grid");
    for (int row = 0; row < 4; row++)
    {
        for (int column = 0; column < 4; column++)
        {
            themeGrid[row][column] = themeList.get(row*4 + column);
            System.out.printf("%s ", themeGrid[row][column]);
        }
        System.out.println();
    }
}

如果您希望能够轻松地重新调整同一网格,只需将themeList保存为静态变量,而不是将其保存为themeGrid。