如何从包含两个变量的数组中选择一个随机元素

时间:2017-03-14 04:12:57

标签: java arrays random

我创建了一个程序,我想从数组中选择一个随机元素,但是我需要确保该元素不是某种类型。为了显示它是什么类型,当我创建数组时,我分配了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是字符的类型。

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

1 个答案:

答案 0 :(得分: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);

基本上,它只生成两个随机索引(row / 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来将一个实体映射到一个位置,这取决于你的需要)< / p>

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