lundi 13 mars 2017

How to select a random element from an array with two variables

I have created a program and I would like to select a random element from an array, however I need to make sure that that element is not of a certain type. To show what type it is, when i created the array i assigned 3 numbers. 0=normal human, 1=Zombie, 2= cat. The array initially spawns a 10 * 10 array of normal humans however when you right click you can spawn a zombie. The function then runs when the play hits a start button(info on function not needed for question). If no zombie is selected i want it to create a random one from a human. This is the code so far:

    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 is the method (not important)

playingGrid is the array and typeOfChar is the type of character.

Any help is much appreciated thanks =)




Aucun commentaire:

Enregistrer un commentaire