【问题标题】:How to fill an int array with random values from a range with exactly one duplicate for each value?如何用一个范围内的随机值填充一个 int 数组,每个值只有一个重复值?
【发布时间】:2016-01-23 22:54:26
【问题描述】:

我正在尝试使用 JFrame 创建一个记忆匹配游戏。我正在使用一个包含 .png 文件(国家标志)的文件夹,它将使用以下代码填充字符串数组:

public String[] listPngFiles(String filePath) {
    File[] fileReader = fileReader(filePath);
    String[] files = new String[fileReader.length];

    for(int i=0; i<files.length; i++)
        files[i] = "" + fileReader[i];

    arraySize = files.length;
    return files;
}

public File[] fileReader(String filePath) {
    File folder = new File(filePath);
    return folder.listFiles (new FilenameFilter() { 
        public boolean accept(File filePath, String filename)
        { return filename.endsWith(".png"); }
    });
}

我这样做的原因是因为我想创建一个动态 JButton 数组,该数组以随机顺序保存每个标志,两次。每次执行程序时,标志都必须重新洗牌。

这是我目前正在处理的代码:

int[] flags = new int[arraySize*2];

for (int i=0; i<flags.length; i++){
    flags[i] = -1; // -1 represents "no flag"

    for (int r=0; r<2; r++)
    {
        int pos=0;
        do
        {
            pos = randomize();
            flags[pos] = i; // put the flag into the card at the available position
            }
            while (flags[pos] != -1); // if card[pos] is not -1, it already has a flag in it
        }
    }

非常感谢您对此事的帮助。

【问题讨论】:

  • 旁注:不要使用数组,而是使用 ArrayList / List。
  • 使用List,并使用Collections.shuffle

标签: java arrays eclipse random jframe


【解决方案1】:

最佳解决方案是:

运行一个循环,添加一个数字两次

洗牌

public static List<Integer> shuffleImages(int numOfPhotos)
{
    List<Integer> list = new ArrayList<>();
    for(int i = 0; i < numOfPhotos; ++i)
    {
     list.add(i);
     list.add(i);
    }
    Collections.shuffle(list);
    System.out.println(list);
    return list;
}

每次创建新游戏时,都会随机播放列表。

do
{
  List<Integer> list = shuffleImages(10);
  JButton []buttons = new JButton();// assuming this is your array of buttons
  for(int i = 0; i < list.size(); ++i)
  {
    buttons[i].setIcon(new ImageIcon(files[list.get(i)]));//set your background of your buttons
  }
  playGame();
}while();//exit conditon

【讨论】:

  • 感谢您提供优雅的解决方案。一旦我正确实例化了 JButton 数组中的每个元素,就可以完美地工作。
【解决方案2】:

这是使用列表、HashMap 和两个数组实现此目的的一种方法。

  1. 用不重复的随机数填充数组。
  2. 创建第二个数组,其大小是第一个数组的两倍。
  3. 使用 HashMap 输入第一个数组的值以测试重复项。

public static void main(String[] args) throws InterruptedException {
    Random r = new Random();
    int[] randoms = new int[5];
    List repeated = new ArrayList();
    int range = 5;

    // This will fill in a first array with unique randoms.
    for (int i = 0 ; i < randoms.length ; i++){
        int rand;
        do{
            rand = r.nextInt(range) + 1;
        }while(repeated.contains(rand));
        repeated.add(rand);
        randoms[i] = rand;
    }

    System.out.println(Arrays.toString(randoms)); // Prints : [2, 3, 5, 1, 4]

    // This will fill the second array with duplicates at random places.
    int[] finalArray = new int[randoms.length * 2];
    HashMap<Integer, Integer> intint = new HashMap<>();
    for (int i = 0 ; i < finalArray.length ; i++){
        int whichOne;
        do{
            whichOne = randoms[r.nextInt(randoms.length)];
            //Thread.sleep(1000);
        }while(intint.get(whichOne) != null && intint.get(whichOne) > 1);
        if (intint.containsKey(whichOne)) intint.replace(whichOne, 2);
        else intint.put(whichOne, 1);
        finalArray[i] = whichOne;
    }


    System.out.println(Arrays.toString(finalArray)); // Prints : [4, 3, 4, 2, 3, 2, 5, 5, 1, 1]

}

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2013-04-04
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多