2

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());
    }
}
  • 1
    I won’t solve it for you because you are very close but I can give you a hint. Implement the Ordering interface in your Card class, after that, you can just order the arrAy of cards – developer_hatch Aug 22 '18 at 00:41
  • My list is ordered. 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 – TheRealCodeGuy Aug 22 '18 at 00:42
  • Like comparing two lists? – 0xCursor Aug 22 '18 at 00:42
  • Exactly! comparing 2 lists of ( "Integers" ), – TheRealCodeGuy Aug 22 '18 at 00:43
  • Do you know how to compare 2 lists of cards – TheRealCodeGuy Aug 22 '18 at 00:44
  • @DamianLattenero You mean Comparable? – shmosel Aug 22 '18 at 00:45
  • Just like in real-life: Compare the highest card of both players. ( if its the same, compare the second highest card of both players, if again its the same compare the 3rd highest card of both players ). If 1 highest card is > than the other. You have a winner – TheRealCodeGuy Aug 22 '18 at 00:49
  • Since they are already sorted, cannot you just compare the two lists directly? Or is there something else in your mind? – Hearen Aug 22 '18 at 00:52
  • That seemed to me as a good idea, I tried. But couldn't get it to work – TheRealCodeGuy Aug 22 '18 at 00:54
  • Do you need to use the `compareTo()` method? – 0xCursor Aug 22 '18 at 00:57
  • No, not necessarily. It's just a hobby project. So I do not need to use anything specificly. – TheRealCodeGuy Aug 22 '18 at 00:58
  • Others have given you good advice about the code, but let me remind you of a very important rule: Poker hands have *exactly five cards*, no more, no fewer. If you're playing a seven-card game, you need to compare player A's best 5-card hand with player B's best 5-card hand. For example, A-K-J-9-8-6-3 and A-K-J-9-8-4-2 are *identical* hands, and would split the pot. – Lee Daniel Crocker Aug 23 '18 at 00:39
  • Yes i know, The board had 5 and yourself have 2. You have to get the best 5 cards. I said i wanted to compare 7 cards, that was a typo. I meant: compare the best 5 – TheRealCodeGuy Aug 23 '18 at 10:14

2 Answers2

0

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.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • It is similar to what I was looking. Thanks for your time, I can move forward with this. You put me in the right direction. – TheRealCodeGuy Aug 22 '18 at 01:13
  • @TheRealCodeGuy No problem. I am adding some stuff to my post that might be helpful. – 0xCursor Aug 22 '18 at 01:13
  • 2
    Shouldn't Ace have the highest rank, not the lowest? – Paul Samsotha Aug 22 '18 at 02:05
  • @PaulSamsotha It could be either the lowest or the highest. In this case, it is up to the OP. – 0xCursor Aug 22 '18 at 02:08
  • When comparing high cards only, Ace is always counted as high. The only except is in some lowball or hi-lo poker games where the Ace would be counted as low for the low hand. – Herb Aug 22 '18 at 16:00
0

Even if it's only a hobby project, you probably want to step forward sooner or later and evaulate/compare all possible poker hands, not only the high-card hands.

This is a great article about evaulating poker hands and it also has sources.

Selindek
  • 3,269
  • 1
  • 18
  • 25