Search code examples
javapoker

assign values to Rank in a poker game


How do you assign values to Rank in a poker game. I have the name Ranks of the cards and they are stored in a Collection method, but I cant figure out how to pull out the values and assign them a digit number value.

private Collection cards; // so this is my hands and I have my cards stored here, but now I wanna pull them out and assign them digit values.

Thanks!

Update: I am trying to compare the ranks of the cards.


Solution

  • You can use enums to store your cards' Rank (DUECE to ACE) and Suit (CLUBS, DIAMONDS, HEARTS, SPADES)

    Something like this:

    enum Suit{
        CLUBS, DIAMONDS, HEARTS, SPADES;
    }
    
    enum Rank {
        DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
    }
    

    Card can be a class that has each of a Rank and Suit:

    public class Card{
        Rank rank;
        Suit suit;
    
        Card(Rank rank, Suit suit) {
            ...
        }
    
    }
    

    If you need a value for each rank, add a method to enum Rank so that it returns value. Like this:

    enum Rank {
        DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
    
        public int value() {
            return this.ordinal() + 2; // This will return 2 for DEUCE, 3 for THREE and so on..
        }
    }
    

    --

    Hope this helps!