Question

JAVA

Hello I have built two enum classes . Pics attached.

را اراده ج دا ما ها هم اليها و دے ، می داد که ما ده Files 4 package unocardgame; Projects public enum CardType { ZERO (ZeroFiles 4 package unocardgame; Projects public enum Color { YELLOW, BLUE, GREEN, RED;

How can I create a list of cards from these classes .. shuffle them.. and draw 7 cards at a time

ArrayList<Card> Cards;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For this, we need to create the Card class as below:

public class Card {
    CardType cardType;      // card type
    Color color;            // card color

    // constructor
    public Card(CardType cardType, Color color) {
        this.cardType = cardType;
        this.color = color;
    }
}

This class has card type and color.

Now, to declare the list of cards, we can do like below code in your main method:

public static void main(String[] args) {
        // declare the list of Card objects
        ArrayList<Card> Cards = new ArrayList<>();

        // fill the ArrayList as per the requirement
        Cards.add(new Card(CardType.THREE, Color.YELLOW));
        Cards.add(new Card(CardType.DRAW2, Color.BLUE));
        Cards.add(new Card(CardType.FIVE, Color.RED));
        Cards.add(new Card(CardType.FIVE, Color.GREEN));
        Cards.add(new Card(CardType.NINE, Color.GREEN));
        Cards.add(new Card(CardType.ONE, Color.YELLOW));
        Cards.add(new Card(CardType.WILD, Color.GREEN));
        Cards.add(new Card(CardType.ZERO, Color.RED));
        Cards.add(new Card(CardType.REVERSE, Color.BLUE));
        Cards.add(new Card(CardType.DRAW4, Color.RED));

        // shuffle the list
        Collections.shuffle(Cards);

        // create a list for hand of 7 cards
        ArrayList<Card> hand = new ArrayList<>();

        // draw 7 cards in hand, after this loop hand will have the
        // 7 cards drawn from shuffled list
        for (int i = 0; i < 7; i++) {
            hand.add(Cards.get(i));
        }
    }

This completes the requirement. Let me know if you have any questions.

Thanks!

Add a comment
Know the answer?
Add Answer to:
JAVA Hello I have built two enum classes . Pics attached. How can I create a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT