gpt4 book ai didi

java - 对创建一副纸牌的一点帮助

转载 作者:行者123 更新时间:2023-12-01 18:37:56 30 4
gpt4 key购买 nike

请注意,我可以毫无问题地制作一副通用的牌(使用 52 张独立牌的枚举器),但我在采用特定的方法来提高效率时遇到了麻烦。

我设置代码的方式在枚举器中使用枚举器。我有一个花色枚举器,它只包含每个花色的名称,还有一个卡牌枚举器,其中包含名称、值、卡是否已使用以及卡的花色。

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

我在 12 年级计算机科学类(class)的期末项目中使用了这套卡片生成器,在决定在这里发帖之前,我请老师看看它是否有任何问题。他告诉我,这可能与我使用枚举器有关,因为我生成一副牌的算法不是问题。

枚举套装:

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

类(class)套牌:

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

因为这是唯一可能出错的地方,因为填充 Card 数组时的花色是正确的,但在创建到 Deck 中时它们会神奇地发生变化。

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

感谢您抽出宝贵的时间,我真的很感激。

最佳答案

Enum 值是常量,这意味着您只能拥有它们的一个实例。例如,当您有一个 Card.TWO 并将其套装更改为 Suit.SPADES 时,无论何时您在任何地方使用 Card.TWO将拥有套装Suit.SPADES。这就是您遇到的问题。为了解决这个问题,您需要将进入牌组实例的卡制作为类的实例,而不是枚举的值。也许您可以保留您的 Card (也许将其重命名为 CardName)枚举,并拥有一个类来表示牌组中具有特定名称的卡片(例如 CardName .TWO)和套装(例如 Suit.SPADES)。

关于java - 对创建一副纸牌的一点帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21053228/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com