2

Relating to my Poker Java program would it be a wise decision to choose enum's over chars and ints?

As far as I can tell assigning a separate integer value to a char has the benefit of ease of mathematical operators being applied when it comes to comparing card values to decide a winner. However this may be possible with enums, if so I'm unaware.

Please can someone explain the adv/disadv of each to me?

My first option of declaring card ranks is as enums:

public enum Rank { 
     DEUCE (1), 
     THREE (2), 
     FOUR (3), 
     FIVE (4), 
     SIX (5),     
     SEVEN (6), 
     EIGHT (7), 
     NINE (8), 
     TEN (9), 
     JACK (10), 
     QUEEN (11), 
     KING (12), 
     ACE (13)
}

public enum Suit { 
     CLUBS (1), 
     DIAMONDS (2), 
     HEARTS (3), 
     SPADES (4) 
}

My second option is as static final chars with assigned int values as such:

       public static final char ACE =   'A';
       public static final char TWO =   '2';
       public static final char THREE = '3';
       public static final char FOUR =  '4';
       public static final char FIVE =  '5';
       public static final char SIX =   '6';
       public static final char SEVEN = '7';
       public static final char EIGHT = '8';
       public static final char NINE =  '9';
       public static final char TEN =   'T';
       public static final char JACK =  'J';
       public static final char QUEEN = 'Q';
       public static final char KING =  'K';

         public Rank (char c)
         {
             switch (c)
             {
             case TWO:
                 _val = 0;
                 break;
             case THREE:
                 _val = 1;
                 break;
             case FOUR: 
                 _val = 2;
                 break;
             case FIVE: 
                 _val = 3;
                 break;
             case SIX:
                 _val = 4;
                 break;
             case SEVEN:
                 _val = 5;
                 break;
             case EIGHT:
                 _val = 6;
                 break;
             case NINE: 
                 _val = 7;
                 break;
             case 'T':
                 _val = 8;
                 break;
             case 'J':  
                 _val = 9;
                 break;
             case 'Q':
                 _val = 10;
                 break;
             case 'K': 
                 _val = 11;
                 break;
             case 'A':
                 _val = 12;
                 break;
             default:
                 _val = -1;
             }
         }

Thanks!

silverzx
  • 1,209
  • 4
  • 14
  • 18

4 Answers4

5

I would prefer Enum. Code looks cleaner and IDEs may help you to avoid missing a case in switch statements.

Sjon
  • 4,989
  • 6
  • 28
  • 46
MrSmith42
  • 9,961
  • 6
  • 38
  • 49
  • 1
    +1. And you don't need switch statements anymore, because you can replace them with polymorphism. – JB Nizet Jan 09 '13 at 20:41
  • @JB Nizet: I did not mean the switch from the static version. I meant if you have a need of a switch statement somewhere in your code the IDE might help you avoiding errors when you use Enums. – MrSmith42 Jan 09 '13 at 20:43
4

I'd suggest you read Item 30: Use enums instead of int constants from Effective Java by Joshua Bloch. It explains the many advantages to using enums over int constants.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
2

Use enums. Because of:

  • style - they look better and cleaner
  • IDE integration - the IDE can prompt you for valid input
  • methods can be passed enums which are automatically imbued with range checking (see below)
  • enums can have methods to return values and/or do calculations
  • the data you're dealing with has a definite range of values that won't ever change, so defining an enum instance for each value makes sense

Consider this method:

public static char addCards(char a, char b);

I can call it with invalid input

addCards('x', '$');

There's no range checking built in with char. But with enums, it comes for free.


As for your issue of ranking, with enums you can simply do this

Rank r1, r2;
boolean r1RankedHigherThanR2 = r1.ordinal() > r2.ordinal();

The order you define the enum instances in is enough to convey ranking order.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Great answer, I like the fact that the range checking is built in and it makes sense to have a fixed representation of the variables as you said - they never change or should be changed. Following on from your statement that ordering enum's should be enough to convey ranking order, would this be possible with all card combinations of each Hand Rank from high card to royal flush? I may open another question regarding this as it's my main concern at the moment. Many thanks! – silverzx Jan 09 '13 at 21:34
0

Enums have an ordinal number (0-based). So you can use that to rank them and you probably shouldn't duplicate that. e.g. KING.ordinal().

Also, as an aside, in poker suits don't have a rank - you have your suits in bridge rank order.

john_omalley
  • 1,398
  • 6
  • 13