Advertisement
jdalbey

Testing Homework - isLegalHand

May 22nd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1.     /** Determine if the given hand is legal.
  2.         A legal hand must:
  3.             1.Contain at least one and no more than six cards.
  4.             2.Not contain a Joker as the only card.
  5.             3.Not contain more than one King.
  6.             4.Not contain more than one Ace.
  7.             5.Not contain both a King and an Ace.
  8.        @return integer representing which rule is violated,
  9.        or zero if the hand is legal.
  10.     */        
  11.     public int isLegalHand(ArrayList<Card> hand)
  12.     {
  13.         int result = 0;
  14.        
  15.         if (hand.size() < 1 || hand.size() > 6)
  16.         {
  17.             result = 1;
  18.         }
  19.        
  20.         if (hand.size() == 1 && hand.get(0) == Card.joker)
  21.         {
  22.             result = 2;
  23.         }
  24.        
  25.         int aceCount = 0;
  26.         int kingCount = 0;
  27.         for (Card card : hand)
  28.         {
  29.             if (card == Card.king)
  30.             {
  31.                 kingCount++;
  32.             }
  33.             if (card == Card.ace)
  34.             {
  35.                 aceCount++;
  36.             }
  37.         }
  38.        
  39.         if (aceCount > 1)
  40.         {
  41.             result = 3;
  42.         }
  43.         if (kingCount > 1)
  44.         {
  45.             result = 4;
  46.         }
  47.        
  48.         if (aceCount == 1 && kingCount == 1)
  49.         {
  50.             result = 5;
  51.         }
  52.        
  53.         return result;
  54.     }
  55.  
  56.     enum Card
  57.     {
  58.         ace, two, three, four, five, six, seven, eight, nine, ten,
  59.         jack, queen, king, joker
  60.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement