Java gets a random value from three different enumerations

I am implementing a simple version of Cluedo. The game has 3 types of cards: character, weapon and room. Since one card is nothing more than a line (for example, no functionality or information other than a name is stored on the card), I decided not to have a card interface, and each type extends the Card. Most likely, I had three listings in my game:

public enum Character {Scarlett, Mustard, White, Green, Peacock, Plum;} public enum Weapon {Candlestick, Dagger, LeadPipe, Revolver, Rope, Spanner;} public enum Room {Kitchen, Ballroom, Conservatory, BilliardRoom, Library, Study, Hall;} 

However, there is one case where three types of cards are added together and distributed evenly to each player in the game. For example, one player may have a hand of two characters, 2 weapons and 1 room, the other player may have 3 rooms and 2 characters, so the total number of cards does not even matter which type.

And why am I wondering if there is a way to randomly select one value from all three enumerations in Java?

Or should I not do this in the first place? (Poorly designed)

+5
source share
3 answers

A simple way is to collect all the members of the enumeration into a single Object[] , and then extract a random element from it.

Note that an enumeration can also implement an interface, so you can even have some common API for all enums. Typically, you will find that you write many switch for an enumeration value; in most cases, they can be replaced by dynamic sending with such interface methods. Next, note that each member of the enumeration can provide its own method implementation.

+5
source

I think you should keep it as it is, but then put everything in one list:

 List<Enum> enums = new ArrayList<>(); enums.addAll(Arrays.asList(Character.values())); enums.addAll(Arrays.asList(Weapon.values())); enums.addAll(Arrays.asList(Room.values())); 

And then you take the random values โ€‹โ€‹of this list. More closely resembles what you do in real life.

+2
source

You can write something like the following:

 public enum Character {Scarlett, Mustard, White, Green, Peacock, Plum;} public enum Weapon {Candlestick, Dagger, LeadPipe, Revolver, Rope, Spanner;} public enum Room {Kitchen, Ballroom, Conservatory, BilliardRoom, Library, Study, Hall;} private static final Random RANDOM = new Random(); // random number generator - declared static, because we need only one for whole program private static final int TOTAL_CARDS = Character.values().length + Weapon.values().length + Room.values().length; // sum up all enum lenghts - in this case, it will be 6 + 6 + 7 = 19 private Enum<?> getRandomCard() { int randomNumber = RANDOM.nextInt(TOTAL_CARDS); // we "roll a dice" to get some random number. Let assume that we get "15" if (randomNumber < Character.values().length) { // is 15 less then 6? No, so we skip this part return Character.values()[randomNumber]; } randomNumber -= Character.values().length; // randomNumber = 15 - 6 = 9 if (randomNumber < Weapon.values().length) { // is 9 < 6 ? No, so we skip this return Weapon.values()[randomNumber]; } randomNumber -= Weapon.values().length; // randomNumber = 9 - 6 = 3 if (randomNumber < Room.values().length) { // Is 3 < 7 ? Yes! // so it means that our "dice" has chosen a Room with array index 3 // We call Room.values() to get all room types as an array and then we pick one with index 3 return Room.values()[randomNumber]; } return null; // should never happen } 

If you do not understand any part of this, let me know, I will add some comments.

However, I believe that the solution proposed by Oscar Kjellin is much more elegant than that :)

+1
source

All Articles