【问题标题】:How to select a random element from an array with two variables如何从具有两个变量的数组中选择一个随机元素
【发布时间】:2017-03-14 04:12:56
【问题描述】:

我创建了一个程序,我想从数组中随机选择一个元素,但是我需要确保该元素不是特定类型。为了显示它是什么类型,当我创建数组时,我分配了 3 个数字。 0=正常人,1=僵尸,2=猫。该阵列最初会生成一个 10 * 10 的普通人类阵列,但是当您右键单击时,您可以生成一个僵尸。当播放按下开始按钮时,该功能就会运行(问题不需要有关功能的信息)。如果没有选择僵尸,我希望它从人类中随机创建一个。这是到目前为止的代码:

    for (int x = 0; x < 10; x++) {
            for (int y = 0; y < 10; y++) {
                if (canZombSpawn == false) {
                    ZombieSpreader(playingGrid[x][y].face, playingGrid[x][y].typeOfChar);
                } else {
                    do {
                        t = (int) ((Math.random() * 10));
                        p = (int) ((Math.random() * 10));

                    } while (playingGrid[t][p].typeOfChar != 0);
                    image = new ImageIcon("zombie.gif");
                    playingGrid[t][p].face.setIcon(image);
                    playingGrid[t][p].typeOfChar = 1;
                    ZombieSpreader(playingGrid[x][y].face, playingGrid[x][y].typeOfChar);
                }
            }
        }

ZombieSpreader是方法(不重要)

playingGrid 是数组,typeOfChar 是字符类型。

任何帮助都非常感谢谢谢 =)

【问题讨论】:

  • javajavascript 标签有什么用?
  • 请注意,Java 和 JavaScript 是不同的语言——您的问题是关于 Java 的。
  • 抱歉,我知道你要的是什么。
  • playingGrid 属于什么数组? ZombieSpreader 方法有什么作用?如果它们构成了上述代码的一半以上,为什么它们不重要?
  • 如果你使用实际的对象,我会容易很多,它保持它们在网格上的位置,你使用List而不是数组

标签: java arrays random


【解决方案1】:

这不是特别有效,但应该可以满足您的需求:

int[][] data = new int[10][10];
Random rnd = new Random();
for (int row = 0; row < 10; row++) {
    for (int col = 0; col < 10; col++) {
        data[row][col] = rnd.nextInt(4);
    }
}

Random rndFind = new Random();
int row = 0;
int col = 0;
boolean found = false;
do {
    row = rndFind.nextInt(10);
    col = rndFind.nextInt(10);
    if (data[row][col] == 0) {
        found = true;
    }
} while (!found);
System.out.println("Found human @ " + row + "x" + col);

基本上,它只是生成两个随机索引(行/列)并检查数组中的值,如果是“人”则退出循环,否则再次尝试。

如果每个元素都是一个自包含的对象(它知道它的位置和类型)并且你使用List而不是数组,那么生成一个更有效的方法会更容易

...因为我无法抗拒有戏...

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class Test {

    public enum CharacterType {
        HUMAN, ZOMBIE, CAT;

        private static Random rnd = new Random();

        public static CharacterType random() {
            switch (rnd.nextInt(3)) {
                case 0:
                    return HUMAN;
                case 1:
                    return ZOMBIE;
                default:
                    return CAT;
            }
        }
    }

    public static class CharacterEnity {

        private int gridX, gridY;
        private CharacterType type;

        public CharacterEnity(CharacterType type, int gridX, int gridY) {
            this.gridX = gridX;
            this.gridY = gridY;
            this.type = type;
        }

        public int getGridX() {
            return gridX;
        }

        public int getGridY() {
            return gridY;
        }

        public CharacterType getType() {
            return type;
        }

        public void setGridX(int gridX) {
            this.gridX = gridX;
        }

        public void setGridY(int gridY) {
            this.gridY = gridY;
        }

    }

    public static void main(String[] args) {
        // This is the main collection...
        List<CharacterEnity> entities = new ArrayList<>(100);

        // This is just setup...
        List<Integer> rows = new ArrayList<>(Arrays.asList(new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));
        List<Integer> columns = new ArrayList<>(Arrays.asList(new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}));

        Collections.shuffle(rows);
        List<Integer> unusedColumns = new ArrayList<>(10);
        while (!rows.isEmpty()) {
            Collections.shuffle(columns);
            unusedColumns.addAll(columns);
            int row = rows.remove(0);
            while (!unusedColumns.isEmpty()) {
                CharacterType type = CharacterType.random();
                entities.add(new CharacterEnity(type, unusedColumns.remove(0), row));
            }
        }
        // End setup...

        List<CharacterEnity> humans = entities.stream().filter(e -> e.getType() == CharacterType.HUMAN).collect(Collectors.toList());
        System.out.println("Found " + humans.size() + " humans");
        Collections.shuffle(humans);
        CharacterEnity turn = humans.get(0);
        System.out.println("Turning human @ " + turn.getGridX() + "x" + turn.getGridY());
    }

}

好的,所以基本上这定义了一个 CharacterEntity 类,它知道它在网格上的位置(您也可以使用 Map 将实体映射到某个位置,这取决于您的需要)

它过滤实体的List 以仅获取人类角色,然后将这个List 随机化,抓取第一个元素并转动它们

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多