0

下面是Deck和Card两个类。这些被用于二十一点游戏,因此我试图获得卡片的价值。这些值当前存储在枚举中,但是我似乎无法弄清楚如何从枚举中获取值。

以下是我尝试过的步骤: 1. 创建一个手牌类,通过创建该类的一个实例然后使用方法deck.deal 从牌堆中处理两张牌。2. 尝试从发牌中获取排名值。这是我的方法失败的地方。我试图从 Enum 中获取值,但我似乎无法访问它们。例如,如果deck.deal 给了我梅花5 和红心6,我似乎无法拿到这些牌并运行Rank.getvalues 的方法。

这里最好的方法是什么?

public class Deck {

    private final Stack<Card> deckCards;


    public Deck() {
        this.deckCards = initDeck();
    }

    private Stack<Card> initDeck() {

        final Stack<Card> deckCards = new Stack<>();
        for (final Card.Suit suit : Card.Suit.values()) {
            for (final Card.Rank rank : Card.Rank.values()) {
                deckCards.push(Card.getCard(rank, suit));
            }
        }
        Collections.shuffle(deckCards);
        return deckCards;
    }

    public Optional<Card> deal() {
        return this.deckCards.empty() ? Optional.empty() :
                Optional.of(this.deckCards.pop());
    }

}
public class Card implements Comparable <Card> {

    private final Rank rank;
    private final Suit suit;

    private final static Map<String, Card> Card_Cache = initCache();

    public static Map<String, Card> initCache(){
        final Map<String, Card> cache = new HashMap<>();
        for(Suit suit: Suit.values()){
            for(Rank rank: Rank.values()){
                cache.put(cardKey(rank, suit), new Card(rank,suit));
            }
        }

        return Collections.unmodifiableMap(cache);
    }

    public static Card getCard (final Rank rank, final Suit suit){

        final Card card = Card_Cache.get(cardKey(rank, suit));

        if(card != null){
            return card;
        }

        throw new RuntimeException("Invalid card!");

    }

    private static String cardKey(final Rank rank, final Suit suit) {
        return rank + " of " + suit;
    }

    public Card(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return String.format("%s of %s", this.rank, this.suit);
    }

    @Override
    public int compareTo(Card o) {
        final int rankComparison = Integer.compare(this.rank.getRankValue(), o.rank.getRankValue());

        if(rankComparison != 0){

        }
        return Integer.compare(this.suit.getSuitValue(), this.suit.getSuitValue());
    }

    enum Suit{
        diamonds(1),
        clubs(2),
        hearts(3),
        spades(4);


        private final int suitValue;

        Suit(final int suitValue){
            this.suitValue = suitValue;
        }

        public int getSuitValue(){
            return this.suitValue;
        }

    }

    enum Rank{two(2), three(3), four(4), five(5), six(6),
        seven(7), eight(8), nine(9), ten(10), jack(11),
        queen(12), king(13), ace(14);

        private final int rankValue;

        Rank(final int rankValue){
            this.rankValue = rankValue;
        }



        public int getRankValue(){
            return this.rankValue;
        }

    }

public class Hand {

    private Optional<Card> card1;
    private Optional<Card> card2;
    Deck mynewdeck = new Deck();

    public Hand() {
        this.card1 = mynewdeck.deal();
        this.card2 = mynewdeck.deal();
    }

//    public String getvalue(){
//        get
//    }

    public Optional<Card> getCard1() {
        return card1;
    }

    public void setCard1(Optional<Card> card1) {
        this.card1 = card1;
    }

    public Optional<Card> getCard2() {
        return card2;
    }

    public void setCard2(Optional<Card> card2) {
        this.card2 = card2;
    }
}

下面是我的主要课程。其中包括一系列尝试(似乎都没有工作),以便访问 Hand 类给我的牌的等级值。希望这使我的尝试和方法更加清晰

public class Main {


    public static void main(String[] args) {

        Hand myhand = new Hand();



        System.out.println(myhand.getCard1());
        System.out.println(myhand.getCard2());
        System.out.println(myhand.getCard1(Card.Rank.valueOf();// incorrect

        Card mytestcard = new Card(Card.Rank.seven, Card.Suit.spades);
        System.out.println("this is the card value " + Card.Rank.valueOf(Card.Rank.seven.toString()));
        // incorrect


        String myfirstcard = (myhand.getCard1().toString()); // incorrect
        System.out.println(Card.Suit.valueOf(myfirstcard)); // incorrect



    }


}
4

2 回答 2

0

我最终想出了解决方案。基本上访问修饰符是错误的。在 Card 类中,我将 Rank 和 Suit 更改为 public,然后我可以访问 getRankvalue。

于 2020-04-14T20:31:41.410 回答
0

您应该在 Card 类中为“Suit”和“Rank”创建 getter 以便访问它们,例如:

public Suit getSuit() {
   return suit;
}

public Rank getRank() {
   return rank;
}

现在,您可以从 main 调用:

myhand.getCard1().getSuit().getSuitValue();
or
myhand.getCard1().getRank().getRankValue();
于 2020-04-14T11:35:48.343 回答