对Hand对象

时间:2016-04-28 15:34:36

标签: java sorting poker

我几乎所有的东西都准备好了我的扑克游戏。我接下来要做的是按照等级值对玩家手中的牌进行排序。你知道“两个”是字节2,“三个”是字节3等... 这是我的枚举CardRank课程

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

我需要找到一种方法来使用枚举值对它们进行排序,但说实话我并不知道如何。 此方法返回玩家手牌的枚举值:

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

我不能从一个非常明显的原因使用集合: 类型集合中的方法sort(List)不适用于参数(Hand)

这是我的手班

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

这是我的卡片课程

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

帮助。我感谢任何能帮助我指明正确方向的帮助。

4 个答案:

答案 0 :(得分:0)

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将返回一个int。试试这个

答案 1 :(得分:0)

说你不能使用Guava(Google Collections)这看起来似乎不正确。只需给手一个排序方法,让它与卡片交互(这是一个ArrayList)。

DatabaseName    last_user_update    database_id object_id   index_id    user_seeks  user_scans  user_lookups    user_updates    last_user_seek  last_user_scan  last_user_lookup    last_user_update    system_seeks    system_scans    system_lookups  system_updates  last_system_seek    last_system_scan    last_system_lookup  last_system_update
TestTableName 2016-04-27 07:53:51.740   5   939150391   1   0   101556  101557  6   NULL    2016-04-28 07:27:17.933 2016-04-28 07:27:17.927 2016-04-27 07:53:51.740 0   3   0   0   NULL    2016-04-27 07:52:49.203 NULL    NULL
TestTableName   2016-04-27 07:53:51.740 5   939150391   2   101559  3   0   6   2016-04-28 07:27:17.927 2016-04-26 17:31:21.200 NULL    2016-04-27 07:53:51.740 0   1   0   0   NULL    2016-04-27 18:21:53.977 NULL    NULL

Hand.java

@set @a=0  /*
@cscript //nologo //E:JScript "%~F0" < input.xml > output.xml
@goto :EOF */


// JScript code section

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/versionNumber=[^ ]+ /g,"versionNumber=\"X.Y.Z\" "));

从那里开始,使用实施 Comparable 可以让您对列表进行排序。

Card.java

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

我建议您使用静态数组来存储卡片:

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

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