2

I'm writing a game of Blackjack. I have an enum of all available card types. When dealing, I want to use Random to randomly select a card from the enum to give to the player. How can I use an integer value to correspond to an Enum Value? Or, how can I randomly select an enum value for an variable of that enum?

void chooseCard() {
    Random rn = new Random();
    Card myCard = rn.nextInt(); //How can I do something like this? (randomly select Card value)? 0 being ACE, 1 being TWO...
}

Enun of Cards:

public enum Cards {
    ACE("ACE", 1, 11), TWO("TWO", 2), THREE("THREE", 3), FOUR("FOUR", 4), FIVE("FIVE", 5), SIX("SIX", 6), 
    SEVEN("SEVEN",7), EIGHT("EIGHT", 8), NINE("NINE", 9), TEN("TEN", 10), JACK("JACK", 10), KING("KING", 10), QUEEN("QUEEN",10);

    private int value1;
    private int value2;
    private String name;

    private Cards(String name, int value1) {
        this.value1 = value1;
    }

    private Cards(String name, int value1, int value2) {
        this.value1 = value1;
        this.value2 = value2;
    }

    public int getValue1() {
        return value1;
    }

    public int[] getValue2() {
        int[] val = { value1, value2 };
        return val;
    }

    public String getName() {
        return name;
    }

}
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
  • I did find this: http://stackoverflow.com/questions/1972392/java-pick-a-random-value-from-an-enum. However, I don't actually get how the example code works so if this is the correct answer then I'd apreciate an explanation for it also. – Ashwin Gupta Feb 08 '16 at 19:02
  • 1
    Are you sure you want a random card? A player can get 5 spades of ace in row? I am not so sure you would want an `enum` as Cards either. A `class Card` with an `enum Suit` would maybe be preferred? – Emz Feb 08 '16 at 19:03
  • @Emz I thought of that. I just have an array: `private int[] deck = new int[13];` Each element is initalized to 4. Every time say ACE is picked, `deck[0] = deck[0] - 1`. Then I'll check before I give the card that the deck has more than 0 of that type. I could try your approach however it may be better. I'm just doing this all for fun/learning so I don't really have a fixed structure. – Ashwin Gupta Feb 08 '16 at 19:05
  • So much similar questions, did you read them all? http://stackoverflow.com/search?q=card+deck+java –  Feb 08 '16 at 19:08
  • One approach is to have a `class Deck` hold `Card[] cards = new Card[52]`, then randomize these cards. – Emz Feb 08 '16 at 19:08
  • 1
    @Emz actually that is a lot smarter then what I'm doing currently. That way I can also play with multiple decks if I wanted too later. I'm going to change it to that. Thanks, as a beginner I always aprecitate tips for cleaner/more efficient programming – Ashwin Gupta Feb 08 '16 at 19:10
  • @RC. read the first few, as far as I can see, none of them involve randomly selecting Enum values. Most are using array list. – Ashwin Gupta Feb 08 '16 at 19:11

1 Answers1

7
// get an array of all the cards
private Card[]cards=Cards.values();
// this generates random numbers
private Random random = new Random();
// choose a card at random
final Card random(){
     return cards[random.nextInt(cards.length)];
}
emory
  • 10,725
  • 2
  • 30
  • 58