C++ 中的简单类概念(牌组中的牌)
课程让我很困惑!对于这个特殊的作业,我们将执行经典的“牌堆”程序。我们要创建两个类,一个称为“Card”,另一个称为“Deck”。到目前为止,我已经完成了大部分工作,但我似乎忘记了一个概念。
当我们调用 Card 类的默认构造函数时,将创建所有 52 张牌,以便 Deck 类可以使用这些对象,并且......我们将创建一副牌!现在我参加了与助教的会议,他强调了一个事实,即默认构造函数是初始化值,而 Deck 类获取这些对象并循环它们以使用将真实卡值分配给 Card 对象的函数。
我的问题是:创建这些对象以便 Deck 可以引用它们的最佳方法是什么?我现在的想法是创建一个名为“initialCard[4][13]”的固定数组。从那里,Deck 类将选择一个值 - 例如“initialCard[0][0]”,并将其仅从二维数组的行和列分配给黑桃 A。然而,我的助教一直说“创建一个 Card 对象数组”。他这话是什么意思?我有一点把握,但我只是不确定......有人有什么想法吗?谢谢!
也许这会有所帮助;直接从作业页面:
您的卡类应该具有公共 getter 和 setter 函数 这两个变量(getValue、setValue 等)以及 函数将返回卡的 C 字符串表示形式。 (你 如果您愿意,可以将其称为 toCString。)它应该工作如下 如下:
void toCString(char cardString[]) INPUT:用于存储的字符数组 输出 OUTPUT: NONE 该函数应返回一个字符串 代表卡片对象。该字符串必须具有以下内容 形式:两个字符后跟第三个空字节。第一个 字符代表花色;第二个代表价值。西装应该 是“S”、“H”、“C”或“D”(代表黑桃、红桃)
俱乐部,钻石。第二个字符代表值:“A”代表 Ace, 2-9 代表编号牌,“T”代表 10,“J”代表 Jack,“Q”代表 Queen”,“K” 为了国王。一些例子: 黑桃 2 = 'S2' 红心 10 = 'HT' Jack 方块 = 'DJ' 梅花皇后 = 'CQ' 黑桃 A = 'SA' 等等
除了这些之外,您还应该至少有一个默认构造函数来初始化值。欢迎您创建其他 构造函数(也许是从卡的 C 字符串初始化一张卡的构造函数 代表?)但这不是必需的。
套牌类别:除了您的卡牌类别之外,您还需要创建一个 Deck 类封装所有卡片对象。在这个对象中你 应该有一个私有成员多维数组(大小为 SUITS Card 对象的 VALUES)。当创建 Deck 对象时,您 应该让它初始化卡片的多维数组。卡片 花色由枚举类型表示;卡牌值为 由 1 到 13 的整数表示(1 是 Ace,2-10 编号 值,11 是杰克,12 是皇后,13 是国王)。
你的构造函数应该循环多维数组并且 初始化所有值,以便整副牌是 代表。
Classes confuse me! For this particular assignment, we are to do the classic 'Cards in a Deck' program. We are to make two classes, one called 'Card' and the other 'Deck'. Now I've gotten a large chunk of this done so far but a concept seems to be escaping me.
When we call a default constructor for the Card class, all 52 cards are to be created so that the Deck class may use these objects and... well create a deck! Now I attended a session with our TA and he stressed the fact that the default constructor is to initialize the values while the Deck class takes these objects and loops through them to use a function that assigns real card values to the Card objects.
My question is this: what is the best approach for creating these objects so that Deck may reference them? My idea right now is to create a fixed array called 'initialCard[4][13]'. From there, the Deck class will pick a value - say 'initialCard[0][0]' and assign it to an Ace of Spades solely from the row and column of the two-dimensional array. However, my TA keep saying 'create an array of Card objects'. What could he mean by that? I have a slight grasp but I'm just unsure... anyone have any ideas? Thanks!
Maybe this will help; straight from the assignment page:
Your card class should have public getter and setter functions for
both of these variables (getValue, setValue, etc), as well as a
function that will return a c-string representation of the card. (You
can call it toCString if you want.) It should work something as
follows:void toCString(char cardString[]) INPUT: A character array for storing
the output OUTPUT: NONE This function should return a character string
representing the card object. This string must have the following
form: two characters followed by a third null byte. The first
character represents suit; the second represents value. Suits should
be “S”, “H”, “C”, or “D” for spades, hearts,clubs, diamonds. The second character represents value: “A” for Ace,
2-9 for numbered cards, “T” for 10, “J” for Jack, “Q” for Queen”, “K”
for King. Some examples: 2 of spades = 'S2' 10 of hearts = 'HT' Jack
of diamonds = 'DJ' Queen of clubs = 'CQ' Ace of spades = 'SA' etc.In addition to these, you should have at least a default constructor to initialize the values. You are welcome to create other
constructors (perhaps one that initializes a card from it's c-string
representation?) but it is not required.Deck Class: In addition to your card class, you will need to create a
Deck class to encapsulate all of your card objects. In this object you
should have a private member multi-dimensional array (of size SUITS by
VALUES) of your Card objects. When a Deck object is created, you
should have it initialize the multi- dimensional array of Cards. Card
suits are represented by an enumerated type; card values are
represented by an integer from 1 to 13 (1 is an Ace, 2-10 are numbered
values, 11 is the Jack, 12 is the Queen, and 13 is the King).Your constructor should loop over the multi-dimensional array and
initialize all values so that the complete deck of cards is
represented.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来你没有得到最好的建议。
将对象构造成真正有效的状态总是更干净。因此,不要用无意义的值默认构建卡片然后再填写,而是用正确的值构建它们。这可以使用
vector
来完成,它具有数组的大部分优点。因此,您应该
避免使用具有重要构造函数的 C 风格类数组。
It doesn't sound like you're getting the best advice.
It's always cleaner to construct an object into a truly valid state. So, rather than default-constructing the cards with a nonsense value and later filling in, construct them with the proper value. This can be done with
vector
, which has most of the advantages of an array.So, you would have
Avoid C-style arrays of classes with non-trivial constructors.
做某事总是有一百种方法。对我来说,在创建类和程序结构时,我总是思考事物之间是如何相互关联的。
在你的问题“牌组中的牌”中,说明了这种关系。每个 Card 对象都属于一个 Deck。我将从创建 Card 类开始,因为它没有比 Deck 类低的东西。听起来你已经走到这一步了。
我会设计 Card 类来仅描述一张卡,而不是其他任何东西。价值和套装。 Deck 类我将有一个私有变量来保存卡片数组。在 Deck 类构造函数中,我将创建 52 张卡片。如果您想使用列表而不是数组,这样您就可以添加任意数量的卡片。并非所有纸牌游戏都使用一副牌 52 张牌。
一旦 Deck 被实例化,它的所有方法都可以访问卡片数组。
There are always a hundred ways to do something. For me, when creating classes and the structure of my program, I always think how things are related to each other.
In your problem, Cards in a Deck, the relationship is stated. Each Card object belongs to a Deck. I would start by creating the Card class as it has nothing lower than it then the Deck class. It sounds like you have gotten this far.
I would design the Card class to describe just a card, nothing else. The value and the suit. The Deck class i would have a private variable holding an array of cards. In the Deck classes constructor i would create the 52 cards. If you want to get fancy use a List rather than an array so you can add as many cards as you like. Not all card games use 52 cards in a deck.
Once the Deck is instantiated all of its methods will have access to the array of cards.
Card 对象的数组。
为每一个分配一个值。
简单的?
An array of Card objects.
Assigning each one a value.
Simple?
考虑使用 STL 容器
Card 可以是 1 到 52 之间的整数,以便表示所有卡片。不需要二维数组。
Think about using the STL containers
Card can just be an int from 1 to 52 in order to represent all the cards. There is no need for a 2 dimensional array.