2014-01-10 44 views
0

就像一張紙條一樣,我可以製作一張通用的撲克牌(沒有任何問題)(對於52張單獨的撲克牌而言是一個枚舉器),但我在做某件事情時遇到了麻煩這使得它更有效率。創建一副撲克牌的一點幫助

我設置代碼的方式在枚舉器中使用枚舉器。我有一個西裝枚舉器,它只包含每套花色的名稱和一個卡片枚舉器,其中包含名稱,值,卡片是否已被使用以及卡片的套裝。

卡被放入Deck構造函數(包含Deck和Card數組名)。

我在我的12年級計算機科學課的最後一個項目中使用這種卡片生成器,在決定在這裏發佈之前,我問我的老師看看是否有任何問題。他告訴我這可能是因爲我在使用枚舉數的事實,因爲我的生成卡片組的算法不是問題。

枚舉套裝:

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

因爲這就是事情都可能出錯,因爲西服卡陣列中的填充時是正確的唯一的地方,但他們奇蹟般地在創建到甲板時更改。

我真的想要一個解決方案(如果有的話)或解釋(如果沒有一個)。

謝謝你的時間,我真的很感激。

+2

一個評論:一個ACE是一個ACE,沒有兩個不同的名爲ACE_HIGH和ACE_LOW的卡。一張王牌是高還是低取決於甲板的使用環境。您不應該在特定情況下將卡的身份與其解釋混合在一起。這應該在遊戲中處理。 –

回答

2

Enum值是恆定的,這意味着您只能擁有它們的一個實例。所以當你有一個Card.TWO例如,你改變它適合Suit.SPADES,那麼無論何時你在任何地方使用Card.TWO它將有訴訟Suit.SPADES。這是你遇到的問題。爲了解決這個問題,你需要製作進入你的類的卡組實例而不是枚舉值的卡片。也許你可以保留你的Card(也許將其重命名爲CardName)枚舉,並且有一個類表示具有特定名稱(例如CardName.TWO)和套裝(例如Suit.SPADES)的套牌。

1

是的,你的教授是正確的,你可以在你的enum Card指定套件中爲每張卡片定義全部52張卡片,或者創建class Card。其中的原因,以setSuit(Suite suite)最後調用設置套件所有Card enum

可以更上enums

1

問題是你使用的是枚舉爲一類,如果他們單獨的實例包含值讀取。在java中,在任何給定時間只有一個枚舉實例。出於這個原因,你很多時候不會在枚舉類型中看到setter方法。

因此,在你的代碼:

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

這裏發生的一切是你設置你的卡[...],以枚舉實例。然後你改變那張卡的套裝,這會改變套牌中其他每張卡的套裝。

你想做什麼?因爲這是我不會深入細節的作業,但它看起來像你幾乎在那裏。刪除Card類中的setter方法,也許將其稱爲CardValue或其他東西