Search code examples
javapoker

Java - How to evaluate the best high card hand?


I am making a poker application. Yet, I am stuck on evaluating the high card. (I know how to get the high card, but what if the high card of 2/3 players is the same?)

My list is ordered so that is not the problem. The problem is that I don't know a nice way to compare a hand of 5 cards with another player's hand.

And yes, the cards are sorted by the rankComparator class.

I have a simple Player class that holds a set of cards (his hand):

public class Player {

    private List<Card> cards;
    private int valueWaarde;
    private String valueAsString;
    private String name;
    private HandEval1 h1;

    public String getValueAsString() {
        return valueAsString;
    }


    public Integer getValueWaarde() {
        return valueWaarde;
    }

    public List<Card> getCards() {
        return cards;
    }

    public Player(String name){
        this.name = name;
    }

    public Player() {
    }

    public void addCard(Card c) {
        if (cards == null) {
            this.cards = new ArrayList<>();
        }

        this.cards.add(c);
    }

    public void clearCards() {
        if (cards != null) {
            cards.clear();
        }
    }

    public Map<String, Integer> valueOfHand(List<Card> cards, List<Card> cardsBoard) {
       h1 = new HandEval1(cards, cardsBoard);
       Map<String, Integer> map = h1.evaluateHand();

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            this.valueWaarde = entry.getValue();
            this.valueAsString = entry.getKey();
        }

       return map;
    }


    @Override
    public String toString() {
        return name;
    }

}

I have a simple card class which is as follows:

public class Card {

    private final int suitValue;
    private Rank rank;
    private Suit suit;
    private int value;

    public int getSuitValue() {
        return suitValue;
    }

    public int getValue() {
        return value;
    }

    public Rank getRank() {
        return rank;
    }

    public Suit getSuit() {
        return suit;
    }

    public enum Suit {
        h(1), c(2), d(3), s(4);

        final int value;
        Suit(int value) {
            this.value = value;
        }
    }

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

        final int value;
        Rank(int value) {
            this.value = value;
        }
    }

    //Constructor
    public Card(Rank rank, Suit suit){
        this.rank = rank;
        this.suit = suit;
        this.value = rank.value;
        this.suitValue = suit.value;
    }

    // methods
    public static String rankAsString(int rank){
        return String.valueOf(Rank.values()[rank ]);
    }
    public static String suitAsString(int suit){
        return String.valueOf(Suit.values());
    }

    public String getFilename()
    {
        return "resource/Cards/" + rank.value + suit + ".gif";
    }
}

And I have a class to evaluate my hand, "Where is now nothing in it. ":

// This is my comparator class:

public class rankComparator implements Comparator<Card> {
    @Override
    public int compare(Card card1, Card card2) {

        if (card1.getRank() == card2.getRank()) {
            return card1.getSuit().compareTo(card2.getSuit());
        }
        return card1.getRank().compareTo(card2.getRank());
    }
}

Solution

  • You could add a method to your Rank and Suit enums so that you can easily retrieve the int values of the enums. For example, your Rank enum could be like this:

    public enum Rank {
        ACE(1),TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10),
        JACK(11), QUEEN(12), KING(13);
    
        final int value;
        Rank(int value) {
            this.value = value;
        }
    
        public int getValue() { // Added method
            return value;
        }
    }
    

    So then you could do something like the following to compare two hands based on their rank:

    public void calculateGreaterHand(List<Card> cards1, List<Card> cards2) {
        for (int i = 0; i < cards1.size(); i++) {
            if (cards1.get(i).getRank().getValue() > cards2.get(i).getRank().getValue()) {
                System.out.println("cards1 is greater");
                return;
            } else if (cards1.get(i).getRank().getValue() < cards2.get(i).getRank().getValue()) {
                System.out.println("cards2 is greater");
                return;
            }
        }
        // If code gets to this point, then the hands are equal.
    }
    

    Let me know if this is similar to what you're looking for.