对 Hand 对象内的 Card 对象进行排序

Sorting Card objects inside of the Hand object

我几乎已经为我的扑克游戏准备好了一切。接下来我要做的是按排名值对玩家手中的牌进行排序。你知道 "two" 是字节 2,"three" 是字节 3 等等...... 这是我的枚举 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;
    }
}

我需要找到一种使用枚举值对它们进行排序的方法,但老实说我真的不知道该怎么做。 此方法 returns 枚举玩家手牌的值:

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

我无法使用 Collections 的原因很明显: 类型 Collections 中的方法 sort(List) 不适用于参数 (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;
            }
        }
    }

这是我的名片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;
    }
}

求助。我感谢任何帮助我走向正确方向的帮助。

protected void sortHand(){
    for (Hand playerHand: players){
        i = 0;
        int []arr = new int[5]; 
        while(i<5){
            System.out.println(playerHand.cards.get(i).getRankValue());
            arr[i] = playerHand.cards.get(i).getRankValue();
            i++;
        }
        Arrays.sort(arr);
    }
}

您可以尝试将其添加到数组中并调用 Array.sort()。我假设 getRankvalue 将 return 一个 int 。试试这个

说你不能为此使用 Guava(Google 集合)从表面上看似乎是不正确的。只需提供一个排序方法,让它与卡片(这是一个 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.
    }
}

从那里开始,制作 Card 实现 Comparable 将让您对列表进行排序。

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;
    }
}

可以使用 Comparator.
轻松对数组列表进行排序 然后你会得到这样的东西:

cards.sort(new Comparator<Card>() {
    @Override
    public int compare(Card card1, Card card2) {
         // return -1 if card1 should come before card2
         // return 0 if card1 and card2 have equal values
         // return 1 if card1 should come after card2
    }
});

我建议你使用静态数组来存储手牌:

public class Hand {
    private static final int MAX_CARD = 5;//modify with your value

    protected Card[] cards;
    private int currentCardNumber;
    private String handCards;

    // Constructor
    protected Hand() {
        cards = new Card[MAX_CARD];
        currentCardNumber = 0;
    }

    // reusable methods

    protected void add(Card card) {
        if (currentCardNumber == MAX_CARD - 1) { return; }
        cards[currentCardNumber] = card;
        currentCardNumber++;
    }

    protected void emptyHand() {
        for (int i = 0; i < MAX_CARD; i++) {
            cards[i] = null;
        }
        currentCardNumber = 0;
    }

    protected void flipHandCards() {
        for (int i = 0; i < currentCardNumber; i++) {
            cards[i].flipCard();
        }
    }

    protected String displayHand() {
        handCards = "";
        for (int i = 0; i < currentCardNumber; i++) {
            handCards += cards[i].toString() + "\n";
        }
        return handCards;
    }
    protected boolean giveCard(Card card, Hand differentHand) {
        int index = getCardIndex(card);
        if (index == -1) {
            return false;
        } else {
            differentHand.add(remove(index));
            return true;
        }
    }

    protected void sortHand() {
        for(int i = 0; i < currentCardNumber; i++) {
            for(int j = i + 1; j < currentCardNumber; j++) {
                if(cards[j].getRankValue() < cards[i].getRankValue()) {
                    Card tmp = cards[j];
                    cards[j] = cards[i];
                    cards[i] = tmp;
                 }
             }
         }
     }

    private int getCardIndex(Card card) {
        for (int i = 0; i < currentCardNumber; i++) {
            if(card.equals(cards[i]) {
                return i;
            }
        }
        return -1;
    }

    private Card remove(int cardIndex) {
        if (currentCardNumber == 0) { return null; }
        Card tmp = cards[cardIndex];
        for (int i = cardIndex + 1; i < currentCardNumber; i++) {
            cards[i - 1] = cards[i];
        }            
        cards[currentCardNumber - 1] = null;
        currentCardNumber--;
        return tmp;
    }
}

或者您可以创建自己的列表并应用您自己的排序方法。