就像一張紙條一樣,我可以製作一張通用的撲克牌(沒有任何問題)(對於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);
因爲這就是事情都可能出錯,因爲西服卡陣列中的填充時是正確的唯一的地方,但他們奇蹟般地在創建到甲板時更改。
我真的想要一個解決方案(如果有的話)或解釋(如果沒有一個)。
謝謝你的時間,我真的很感激。
一個評論:一個ACE是一個ACE,沒有兩個不同的名爲ACE_HIGH和ACE_LOW的卡。一張王牌是高還是低取決於甲板的使用環境。您不應該在特定情況下將卡的身份與其解釋混合在一起。這應該在遊戲中處理。 –