Search code examples
javasortingpoker

Sorting Card objects inside of the Hand object


I have almost everything ready for my poker game. What I want to do next is to sort cards in players hand by rank value. You know "two" is byte 2, "three" is byte 3 etc... This is my enum CardRank class

public enum CardRank {
    TWO((byte)2, "Two"),
    THREE((byte)3, "Three"),
    FOUR((byte)4, "Four"),
    FIVE((byte)5, "Five"),
    SIX((byte)6, "Six"),
    SEVEN((byte)7, "Seven"),
    EIGHT((byte)8, "Eight"),
    NINE((byte)9, "Nine"),
    TEN((byte)10, "Ten"),
    JACK((byte)11, "Jack"),
    QUEEN((byte)12, "Queen"),
    KING((byte)13, "King"),
    ACE((byte)14, "Ace");

    private final byte rankValue;
    private final String rankValueName;

    //Constructor
    private CardRank(byte rankValue, String rankValueName){
        this.rankValue=rankValue;
        this.rankValueName = rankValueName;
    }

    //reusable methods
    protected byte getRankValue(){
        return rankValue;
    }

    protected String getRankValueName(){
        return rankValueName;
    }
}

CardsOutput

CardRankOutput

I need to find a way to sort them using enum values but to be honest I don't really know how. This method returns enum values of player hand cards:

protected void sortHand(){
    for (Hand playerHand: players){
        i = 0;
        while(i<5){
            System.out.println(playerHand.cards.get(i).getRankValue());
            i++;
        }
    }
}

I can't use Collections from a very obvious reason: The method sort(List) in the type Collections is not applicable for the arguments (Hand)

And this is my Hand class

import java.util.ArrayList;

    public class Hand {

        protected ArrayList<Card> cards;
        private String handCards;

        // Constructor
        protected Hand() {
            cards = new ArrayList<Card>(5);
        }

        // reusable methods

        protected void add(Card card) {
            cards.add(card);
        }

        protected void emptyHand() {
            cards.clear();
        }

        protected void flipHandCards() {
            for (Card card : cards) {
                card.flipCard();
            }
        }

        protected String displayHand() {
            handCards = "";
            for (Card i : cards) {
                handCards += i.toString() + "\n";
            }
            return handCards;
        }

        protected boolean giveCard(Card card, Hand differentHand) {
            if (!cards.contains(card)) {
                return false;
            } else {
                cards.remove(card);
                differentHand.add(card);
                return true;
            }
        }
    }

And this is my Card class

public class Card {

    private CardSuit suit;
    private CardRank rank;
    private String cardName;
    private boolean visibility;

    //Constructor
    protected Card(CardRank rank, CardSuit suit){
        this.rank = rank;
        this.suit = suit;
    }

    //reusable methods
    protected String getCardSuit(){
        return suit.getCardSuit();
    }

    protected byte getRankValue(){
        return rank.getRankValue();
    }
    protected void flipCard(){
        visibility = !visibility;
    }

    public String toString(){
        if (visibility){
            cardName="";
            cardName+=rank.getRankValueName() + " of " + suit.getCardSuit();
        }
        else{
            cardName = "You cannot see your opponents card";
        }
        return cardName;
    }
}

Help. I appreciate for any help that will direct me into the right direction.


Solution

  • Saying you can't use Guava (Google Collections) for this seems incorrect at face value. Just give hand a sort method that lets it interact with cards (which is an ArrayList).

    protected void sortHand(){
        for (Hand playerHand: players){
            List<Card> sortedHand = playerHand.getSortedCards();
            // Do whatever
        }
    }
    

    Hand.java

    public class Hand {
        ...
    
        public List<Card> getSortedCards() {
            Collections.sort(cards);
            return cards; // Consider deep copying.
        }
    }
    

    From there, making Card implement Comparable will let you sort the list.

    Card.java

    public class Card implements Comparable<Card>
    {
        ...
    
        @Override
        public int compareTo(Card o) {
            if (this.rank.getRankValue() < o.rank.getRankValue()) {
                return -1;
            }
            else if (o.rank.getRankValue() < this.rank.getRankValue()) {
                return 1;
            }
            return 0;
        }
    }