Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Determine if the given hand is legal.
- A legal hand must:
- 1.Contain at least one and no more than six cards.
- 2.Not contain a Joker as the only card.
- 3.Not contain more than one King.
- 4.Not contain more than one Ace.
- 5.Not contain both a King and an Ace.
- @return integer representing which rule is violated,
- or zero if the hand is legal.
- */
- public int isLegalHand(ArrayList<Card> hand)
- {
- int result = 0;
- if (hand.size() < 1 || hand.size() > 6)
- {
- result = 1;
- }
- if (hand.size() == 1 && hand.get(0) == Card.joker)
- {
- result = 2;
- }
- int aceCount = 0;
- int kingCount = 0;
- for (Card card : hand)
- {
- if (card == Card.king)
- {
- kingCount++;
- }
- if (card == Card.ace)
- {
- aceCount++;
- }
- }
- if (aceCount > 1)
- {
- result = 3;
- }
- if (kingCount > 1)
- {
- result = 4;
- }
- if (aceCount == 1 && kingCount == 1)
- {
- result = 5;
- }
- return result;
- }
- enum Card
- {
- ace, two, three, four, five, six, seven, eight, nine, ten,
- jack, queen, king, joker
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement