在创建一副牌上有点帮助

时间:2014-01-10 19:51:07

标签: java

就像一张纸条一样,我可以制作一张通用的纸牌套牌(带有52张单独纸牌的普查员),但我在某种特定的方式上遇到麻烦,这使得它更有效率。< / p>

我设置代码的方式使用枚举器中的枚举器。我有一个Suit枚举器,它只包含每个套装的名称,以及一个卡枚举器,其中包含名称,价值,是否使用了卡,以及卡的套装。

这些卡被放入Deck构造函数(包括Deck的名称和Card数组)。

我在我的12年级计算机科学课程的最后一个项目中使用了这个卡片组生成器,在决定发布之前,我让老师看看它是否有任何问题。他告诉我,我可能会使用枚举器,因为我生成牌组的算法并不是问题所在。

enum套装:

public enum Suit {
    // The suits
    HEARTS("Hearts"),
    DIAMONDS("Diamonds"),
    SPADES("Spades"),
    CLUBS("Clubs");

    // The properties of a suit
    private String name;

    /**
     * The constructor for creating a suit
     * @param name the name of the suit
     */
    private Suit(String name) {
        this.name = name;
    }
    /**
     * Gets the name of the suit
     * @return 
     */
    public String getName() { return this.name; }
}

枚举卡

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

    // The card properties
    private String name;
    private int value;
    private boolean isUsed;
    private Suit suit;

    /**
     * The constructor for making a Card
     * @param name the name of the card
     * @param value the value of the card
     */
    private Card(String name, int value) {
        this.name = name;
        this.value = value;
        this.isUsed = false;
        this.suit = null;
    }
    /**
     * Gets the name of the card
     * @return the name of the card
     */
    public String getName() { return this.name; }
    /**
     * Gets the value of the card
     * @return the value of the card
     */
    public int getValue() { return this.value; }
    /**
     * Gets whether or not the card has been used
     * @return returns true if the card has been used or false
     * if the card has not
     */
    public boolean isUsed() { return this.isUsed;}
    /**
     * Sets the suit of the card
     * @param suit the suit to set the card
     */
    public void setSuit(Suit suit) { this.suit = suit; }
    /**
     * Gets the suit of the card
     * @return returns the name of the suit
     */
    public String getSuit() { return this.suit.getName(); }
}

类常量:

public class Constants {
    // Card sets
    public static Card[] cardSetAceHigh = {Card.TWO, Card.THREE, Card.FOUR,
        Card.FIVE, Card.SIX, Card.SEVEN, Card.EIGHT, Card.NINE, Card.TEN, Card.JACK,
        Card.QUEEN, Card.KING, Card.ACE_HIGH};
    public static Card[] cardSetAceLow = {Card.ACE_LOW, Card.TWO, Card.THREE,
        Card.FOUR, Card.FIVE, Card.SIX, Card.SEVEN, Card.EIGHT, Card.NINE, Card.TEN,
        Card.JACK, Card.QUEEN, Card.KING};

    // Suit set
    public static Suit[] suits = {Suit.HEARTS, Suit.DIAMONDS, Suit.SPADES, Suit.CLUBS};
}

课程甲板:

public class Deck {
    // The deck properties
    Card[] cards;
    private String name;

    /**
     * The constructor for a deck, specifying the name
     * @param name the name of the deck
     */
    public Deck(String name) {
        this.name = name;
        this.cards = null;
    }
    /**
     * The constructor for a deck, specifying all parameters
     * @param name the name of the deck
     * @param cards the cards in the deck
     */
    public Deck(String name, Card[] cards) {
        this.name = name;
        this.cards = cards;
    }
    /**
     * Gets the length of the deck
     * @return the length of the deck
     */
    public int getDeckLength() { return this.cards.length; }
    /**
     * Gets the name of the deck
     * @return the name of the deck
     */
    public String getName() { return this.name; }
    /**
     * Makes a standard 52-card deck
     * @param aceHigh whether or not the ace's value is 1 or 14
     * @return the deck
     */
    public static Deck makeStandardDeck(boolean aceHigh) {
        int cardSetLength;
        Card[] set;
        if (aceHigh) {
            cardSetLength = Constants.cardSetAceHigh.length;
            set = Constants.cardSetAceHigh;
        }
        else {
            cardSetLength = Constants.cardSetAceLow.length;
            set = Constants.cardSetAceLow;
        }
        Card[] cards = new Card[Constants.suits.length * cardSetLength];
        for (int suit = 0; suit < Constants.suits.length; suit++) {
            for (int card = 0; card < cardSetLength; card++) {
                cards[(suit * cardSetLength) + card] = set[card];
                cards[(suit * cardSetLength) + card].setSuit(Constants.suits[suit]);
                Main.output(cards[(suit * cardSetLength) + card].getName() + "\t" + cards[(suit * cardSetLength) + card].getValue() + "\t" + cards[(suit * cardSetLength) + card].getSuit());
            }
        }
        Deck deck = new Deck("Standard deck", cards);
        return deck;
    }
    /**
     * Displays a deck and its contents
     * @param deck the deck to be displayed
     */
    public static void displayDeck(Deck deck) {
        String text = "Deck " + deck.getName() + " contents:\n";
        for (int card = 0; card < deck.getDeckLength(); card++) {
            text += deck.cards[card].getName() + "\t" + deck.cards[card].getValue() + "\t" + deck.cards[card].getSuit() + "\n";
        }
        Main.output(text);
    }
}

类主要:

public class Main {

    /**
     * The main method of the program
     * @param args 
     */
    public static void main (String[] args) {
        Deck deck = Deck.makeStandardDeck(false);
        Deck.displayDeck(deck);
    }
    /**
     * Outputs a line of text
     * @param text the text to be outputted
     */
    public static void output (String text) {
        System.out.println(text);
}

当我运行程序时,我最终将其作为输出:

Ace 1   Hearts
Two 2   Hearts
Three   3   Hearts
Four    4   Hearts
Five    5   Hearts
Six 6   Hearts
Seven   7   Hearts
Eight   8   Hearts
Nine    9   Hearts
Ten 10  Hearts
Jack    11  Hearts
Queen   12  Hearts
King    13  Hearts
Ace 1   Diamonds
Two 2   Diamonds
Three   3   Diamonds
Four    4   Diamonds
Five    5   Diamonds
Six 6   Diamonds
Seven   7   Diamonds
Eight   8   Diamonds
Nine    9   Diamonds
Ten 10  Diamonds
Jack    11  Diamonds
Queen   12  Diamonds
King    13  Diamonds
Ace 1   Spades
Two 2   Spades
Three   3   Spades
Four    4   Spades
Five    5   Spades
Six 6   Spades
Seven   7   Spades
Eight   8   Spades
Nine    9   Spades
Ten 10  Spades
Jack    11  Spades
Queen   12  Spades
King    13  Spades
Ace 1   Clubs
Two 2   Clubs
Three   3   Clubs
Four    4   Clubs
Five    5   Clubs
Six 6   Clubs
Seven   7   Clubs
Eight   8   Clubs
Nine    9   Clubs
Ten 10  Clubs
Jack    11  Clubs
Queen   12  Clubs
King    13  Clubs
Deck Standard deck contents:
Ace 1   Clubs
Two 2   Clubs
Three   3   Clubs
Four    4   Clubs
Five    5   Clubs
Six 6   Clubs
Seven   7   Clubs
Eight   8   Clubs
Nine    9   Clubs
Ten 10  Clubs
Jack    11  Clubs
Queen   12  Clubs
King    13  Clubs
Ace 1   Clubs
Two 2   Clubs
Three   3   Clubs
Four    4   Clubs
Five    5   Clubs
Six 6   Clubs
Seven   7   Clubs
Eight   8   Clubs
Nine    9   Clubs
Ten 10  Clubs
Jack    11  Clubs
Queen   12  Clubs
King    13  Clubs
Ace 1   Clubs
Two 2   Clubs
Three   3   Clubs
Four    4   Clubs
Five    5   Clubs
Six 6   Clubs
Seven   7   Clubs
Eight   8   Clubs
Nine    9   Clubs
Ten 10  Clubs
Jack    11  Clubs
Queen   12  Clubs
King    13  Clubs
Ace 1   Clubs
Two 2   Clubs
Three   3   Clubs
Four    4   Clubs
Five    5   Clubs
Six 6   Clubs
Seven   7   Clubs
Eight   8   Clubs
Nine    9   Clubs
Ten 10  Clubs
Jack    11  Clubs
Queen   12  Clubs
King    13  Clubs

问题是当创建卡组时,数组中每张卡的套装变成了Constants.suits数组中的最后一套(我通过改变Constants.suits数组中套装的顺序来测试它) )。

现在从我所知道的情况来看,它可能就在行中:

Deck deck = new Deck("Standard deck", cards);

因为这是唯一可能出错的地方,因为在填充卡阵列时套装是正确的,但是当它们被创建到甲板时它们会神奇地改变。

我真的想要一个解决方案(如果有的话)或解释(如果没有解决方案)。

感谢您的时间,我真的很感激。

3 个答案:

答案 0 :(得分:2)

Enum值是常量意味着您只能拥有它们的一个实例。因此,如果你有一个Card.TWO,并且你将其更改为Suit.SPADES,则只要你在任何地方使用Card.TWO,它就会拥有诉讼Suit.SPADES。这是您遇到的问题。要解决这个问题,你需要制作进入类的甲板实例的卡而不是枚举值。也许你可以保留你的Card(可能将它重命名为CardName)枚举,并有一个代表卡片的卡片,该卡片具有特定的名称(例如CardName.TWO)并且适合(例如Suit.SPADES)。

答案 1 :(得分:1)

是的,您的教授是正确的,您可以在每张卡的enum Card指定套件中定义所有52张卡片,也可以创建class Card。上次调用setSuit(Suite suite)的原因是为所有Card enum

设置套件

您可以在enums

上阅读更多内容

答案 2 :(得分:1)

问题是您使用枚举作为类来包含值,就好像它们是单独的实例一样。在java中,在任何给定时间只有一个枚举实例。因此,您很多时候都不会在枚举类型中看到setter方法。

所以在你的代码中:

cards[(suit * cardSetLength) + card] = set[card];
cards[(suit * cardSetLength) + card].setSuit(Constants.suits[suit]);

这里发生的事情是你将卡片[...]设置为枚举实例。然后你改变了那张牌的套装,这改变了牌组中每张其他牌的套装。

你想做什么?因为这是作业,所以我不会详细介绍它,但看起来你几乎就在那里。删除Card类中的setter方法,可以将其称为CardValue或其他