Java类卡枚举示例。修订

时间:2010-01-27 23:43:55

标签: java

*非常感谢任何帮助*

我正在使用java网站上的类卡示例来尝试构建游戏。

http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

我想分配套装和等级值。我不知道该怎么做..

对于西装,我想做的是分配Heart = 4 diamond = 3,club = 2,spade = 1

为排名,ace = 11 杰克,女王,国王= 10
2-10卡的价值..

程序接受用户输入作为手数和每手牌数的参数。 像这样:$ java Deal 4 5

所以我想要它打印八个黑桃(8),十个心(40)

基于值...示例spade = 1 * 8 hearts = 4 * 10

我可以打印手而不是价值......

我的例子 电流输出缩小:

FIVE of CLUBS(0),
DEUCE of SPADES(0),
SEVEN of SPADES(0),
TEN of SPADES(0),
THREE of HEARTS(0),
FOUR of SPADES(0),
THREE of DIAMONDS(0),
[TEN of SPADES, THREE of HEARTS, FOUR of SPADES, THREE of DIAMONDS]
[FOUR of CLUBS, FIVE of CLUBS, DEUCE of SPADES, SEVEN of SPADES]
[QUEEN of HEARTS, SIX of HEARTS, FOUR of HEARTS, KING of DIAMONDS]

C:\Java\a02>

以下是两个不同类中程序的代码

import java.util.*;

public class Cards {

    public enum Rank {
        DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
                9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

        private int Rankpoints;

        Rank(int points) {
            this.Rankpoints = points;
        }

        public int getRankpoints() {
            return this.Rankpoints;
        }

    }

    public enum Suit {
        CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);

        private int Suitpoints;

        Suit(int points) {

            this.Suitpoints = points;

        }

        public int getSuitpoints() {
            return this.Suitpoints;
        }

    }

    private final Rank rank;
    private final Suit suit;

    private Cards(Rank rank, Suit suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public Rank rank() {
        return this.rank;
    }

    public Suit suit() {

        return this.suit;

    }

    public String toString() {
        return rank + " of " + suit;
    }

    private static final List<Cards> protoDeck = new ArrayList<Cards>();

    // Initialize prototype deck
    static {
        for (Suit suit : Suit.values())
            for (Rank rank : Rank.values())
                protoDeck.add(new Cards(rank, suit));

    }

    public static ArrayList<Cards> newDeck() {

        return new ArrayList<Cards>(protoDeck); // Return copy of prototype deck
    }

}

这是主要的

import java.util.*;

public class Deal {
    public static void main(String args[]) {
        int numHands = Integer.parseInt(args[0]);
        int cardsPerHand = Integer.parseInt(args[1]);
        List<Cards> deck = Cards.newDeck();
        Collections.shuffle(deck);

        for (Cards card : deck) {
            System.out.println(card.rank() + " of " + card.suit() + "("
                    + card.getSuitpoints() + ")" + ",");

        }

        for (int i = 0; i < numHands; i++)
            System.out.println(deal(deck, cardsPerHand));
    }

    public static ArrayList<Cards> deal(List<Cards> deck, int n) {
        int deckSize = deck.size();
        List<Cards> handView = deck.subList(deckSize - n, deckSize);
        ArrayList<Cards> hand = new ArrayList<Cards>(handView);
        handView.clear();
        return hand;
    }
}

当我尝试编译时,我收到错误.. 为了

card.getSuitpoints()

错误:找不到符号:方法getSuitpoints()

我发现这很奇怪,因为

card.getRankpoints() 编译.. 这是我把它放入枚举的方式吗?

getRankpoints() 返回零。 怎么了?

1 个答案:

答案 0 :(得分:3)

你在牌组中看到重复的原因是因为你在牌上迭代了两次。

for (Cards suit : deck) // ********* here is where i print the deck ********
{
    for (Cards rank : deck) {
        System.out.println(rank.rank() + " of " + suit.suit() + ",");
    }
}

假设你的甲板发生器工作,迭代一次就足够了:

for (Cards card : deck)
{ 
    System.out.println(card.rank() + " of " +  card.suit() + ",");  
} 

此外,对于代表卡片的班级,Card可能是比Cards更好的名称选择。

要获取该值,您需要添加一个返回它的方法。

Rank(int points)
{
    this.Rankpoints = points;  
}

public int getRankpoints() {
    return this.Rankpoints;
}

然后,当您想要打印值时,可以调用它。