samedi 10 décembre 2016

Picking a card from an Arraylist

My Arraylist is an Arraylist made out of enums which have Suits and Ranks (all together 52cards)

    ArrayList<Card> deck = new ArrayList<Card>();
    Random rand = new Random();

I created my card deck and shuffled it and now I am trying to pick a random Card.

    public Card dealCard() {

    int index = rand.nextInt(deck.size());
    Card cards = deck.get(index);
    return cards;

But my main is giving me Errors

    public static void main(String[] args) {
            Deck deck1 = new Deck();
            deck1.dealCard();
            System.out.println(deck1.toString());

Any ideas on how I could pick that random card? I suppose it has something to do with that int and rand.nextInt as I am not picking Integers. But how can I pick my cards? Should I use my Suite.values() and Rank.values() here as well? And how do I apply them for random? *edit: my class Card contains the enums and setters and toString and my class Deck has the methods for creating the Deck shuffling it and picking one card and then I have the main part..




Aucun commentaire:

Enregistrer un commentaire