为记忆匹配游戏洗牌 2 天数组
Shuffling 2-Day array for memory match game
您好,我正在尝试创建一个记忆配对游戏。我能够随机化数组状态以获得不同的值,但是在我将 states 数组的 8 个元素中的 2x 加载到名为 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();
}
}
随机化列表或数组的最简单方法是创建一个 returns 随机值的比较器,如下所示:
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()
一起使用,以随机化任何数组或集合的顺序。
对于像您这样的多向量数组,您需要分别打乱每个 'row',然后打乱 'columns'。也许多次才能让它们变好并混合。
我不清楚为什么要将 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 或者除了 themeGrid 之外。
您好,我正在尝试创建一个记忆配对游戏。我能够随机化数组状态以获得不同的值,但是在我将 states 数组的 8 个元素中的 2x 加载到名为 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();
}
}
随机化列表或数组的最简单方法是创建一个 returns 随机值的比较器,如下所示:
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()
一起使用,以随机化任何数组或集合的顺序。
对于像您这样的多向量数组,您需要分别打乱每个 'row',然后打乱 'columns'。也许多次才能让它们变好并混合。
我不清楚为什么要将 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 或者除了 themeGrid 之外。