Java - How to evaluate the best high card hand?

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.


rankComparator



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


Player


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());






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
– Damian Lattenero
Aug 22 at 0: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 at 0:42





Like comparing two lists?
– LAD
Aug 22 at 0:42





Exactly! comparing 2 lists of ( "Integers" ),
– TheRealCodeGuy
Aug 22 at 0:43





Do you know how to compare 2 lists of cards
– TheRealCodeGuy
Aug 22 at 0:44




2 Answers
2



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:


Rank


Suit


int


Rank


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.





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 at 1:13





@TheRealCodeGuy No problem. I am adding some stuff to my post that might be helpful.
– LAD
Aug 22 at 1:13





Nice edited stuff !
– TheRealCodeGuy
Aug 22 at 1:44





Shouldn't Ace have the highest rank, not the lowest?
– Paul Samsotha
Aug 22 at 2:05





@PaulSamsotha It could be either the lowest or the highest. In this case, it is up to the OP.
– LAD
Aug 22 at 2:08




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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)