ADTs

Hey guys!

I am new to programming and i have this big problem to present tomorrow. But i dont have the slightest clue on how to solve this problem. Could someone help me out? Thanks so much.

The Question is:

(ADT implementation) To prepare for the upcoming Integrated Resort, Ms.Betting has implemented the following
Card ADT in C++ to represent a single poker card:
class Card
{
public:
Card(Suit s, Rank r);
Suit getSuit( );
Rank getRank( );
void print( ); //Example: “Spade A”, “Diamond 10” etc.
};

For your information, Suit is an enumeration with 4 values representing the four
different suits in poker {Spade, Heart, Club, Diamond}, Rank is another
enumeration with values {Ace, Two, …, Ten, Joker, Queen, King} to
represent the poker card’s rank.

Make use of the Card ADT above to implement a Deck ADT which represents a
deck of 52 poker cards. The major operations supported by the Deck ADT are
summarized below:
class Deck
{
public:
Deck( ); //Initialize the deck
void shuffle( ); //shuffle the remaining cards in the deck
Card takeOneCard( ); //take one card from the top of deck
unsigned int remainingCard( ); //get number of remaining cards
void print( ); //show each remaining cards from top to bottom
}

When the deck is first initialized, the 52 cards are arranged nicely from Spade to
Diamond, and in each suit, from Ace to King. User of the ADT can use
takeOneCard( ) to remove one card from the deck. Whenever shuffle( ) is
called, the remaining cards in the deck are randomized.
do you already have some code? post it please (in code tags)...

let me guess ur big problem is the shuffle()-method?...
and how to store the arrangement of 52 cards in the deck?...

are u allowed to use stl-containers?...
Last edited on
Hey!

I was actually only able to do remainingCard(); the rest i was not really sure. And no, we are not allowed to use stls cause we have not been taught that yet. Just started on ADTs and other basic stuff on C++. Thanks!
So if you are able to do remainingCard(), what kind of data type are you storing the cards in? An array would probably be what your teacher wants, if you are in an ADT level class.
The next thing you will have to come up with is how to store information inside the array that represents the cards. Ex: You could represent the cards as numbers 1 through 13 and then maybe attach the a decimal .1, .2, .3, or .4 to represent the suits for each card.
(Also notice so far I have not written a single line of code. A little planning is actually required for some programs.)
Now that you have an idea for how to store the cards... (You could come up with another method, like instead of using a decimal for suit you could just add 100, 200, 300, or 400) You can begin coding for the functions that are part of your Card class.
Well now I feel stupid I just typed all that and remembered you are using enumerators ::bangs head::
I suppose I can help you with the initialization just to get you started, but to use the types of data you teacher wants you to you need to include all of the class Card and class Deck private data too. I do not mind helping you out with initializing a deck of cards for you, but I will need the whole Card class.
Please use the forum [code] tags to make your code easier to read.
@kevin: i guess the array he uses in the deck is of type Card (his class)... no numbers needed...
hey!

i think i kind of got what u were trying to tell me! thanks so much...i am trying it out on my own right now...if ive still got doubts i will post it on the forum again! THANKS so much for your help!
Hey guys...i managed to come up with something....can someone help see if its actually rightt?
thanks so much!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
/* Card ADT omitted */
class Deck
{
public:
Deck();
void shuffle();
Card takeOneCard();
unsigned int remainingCard();
void print();
private:
vector<Card> _vCard;
};
Deck::Deck()
{
4
int i, rank, suit;

for (i = 51; i >= 0; i--){
suit = i / 13;
rank = i % 13;

_vCard.push_back(Card(Suit(suit), Rank(rank));

}
srand(2314); //set the random seed
}
void Deck::shuffle()
{
unsigned int pos, target, size;
Card temp(Spade, Ace);
size = _vCard.size();
for (pos = 0; pos < size - 1; pos++){

target = rand() % (size - pos+1);
if (target != pos){
temp = _vCard[pos];
_vCard[pos] = _vCard[target];
_vCard[target] = temp;
}
}
}
Card Deck::takeOneCard()
{
Card result = _vCard.back();
_vCard.pop_back();
return result;
}
unsigned int Deck::remainingCard()
{
return _vCard.size();
}
void Deck::print()
{
int i;

for (i = _vCard.size()-1; i >= 0; i--){
_vCard[i].print();
}
}


Looks good to me. A few comments.

1. print() will fail if the deck is empty.
2. You probably don't want to call srand every time to make up a new deck.
3. In shuffle, if you declared temp where you used it, you wouldn't need to intialise it as a dummy Ace of Spade.
4. Function div does a divide giving you a divisor and modulus in one go. You could use that in your constructor rather than doing the calculation twice.

[EDIT] print() is ok.
Last edited on
and correct me if im wrong, but vector is part of the stl^^...
The use of vector is correct.
thanks so much for the comments and all the help u guys:) and yea i think vector is part of the stl.
preetii90 wrote:
And no, we are not allowed to use stls cause we have not been taught that yet.


that´s why i asked...
haha..yea...i know...but my friend told me there was no other way to code it..thats why i did not have a choice.
Hey guys am just new and wanted to say hi
i wanna learn everthing about C++ am 15 yrs and i
find this very interestring just wanted to say hi to the community :)
am reading all the tuts in the page so far so good did u uys read all the tuts?
Welcome to the forums tOoOxiiCc.
Please read the top 2 sticky posts on this forum and the articles section is really good for learning about what not to do, and how to do things properly. Also the tutorial is short and sweet but well documented.
Have fun.

PS. if you post code please you code tags for formatting.

edit: it's important if you teaching yourself via the tutorial, to try and make as many little sample programs as you read. As this will enforce what you reading.
Last edited on
Maybe he was not sure if STL was allowed... I would use it as long as the teacher does not say that I can not. Vectors are my friends =)
hey!

Yes i checked with my tutor later he said we were allowed to use vectors as long as we know how to use it properly because we just started on ADTs. so thanks again so much for all helping me edit my code. All the tips were useful. Btw...i'm a 'she' not a 'he'...hhahaa..

He-Man... da da dadada da da :P...
Topic archived. No new replies allowed.