We're doing this project with simulating a game of cards. First step, deal a deck of cards. Mission accomplished. Where I am hung up is when I want to output this deck of cards. I call my_deck.print_deck(); which I expect to work as shown below
1 2 3 4 5 6 7 8
int main()
{
//deal a deck of cards
//creates an array from my_cards[0] to my_cards[51]
Deck my_deck; //this part is easy
//output the cards
my_deck.print_deck();
Thanks. That's where I am confused. Hopefully I can explain this with some intelligence:
There are two classes: Deck and Card
The function "deal" is basically this:
1 2 3 4 5 6 7
void Deck::deal()
{
card my_cards[52];
for (int i = 0; i<52; i++)
{
//initializes each card based on my_cards[i]
}
There is a private variable in the Deck class like this:
std::list<Card> my_deck;
(I'm trying strip out the unnecessary code to keep this as simple as I can.)
So, after the constructor is called, there is an object consisting that consists of a deck of cards (actually, there can any multiple of 52 cards, but I left that part out keep this streamlined). I just need to cout the deck of cards, but I don't know how.
The my_cards variable in your deal function is a local variable.
It only exists inside the deal function. When deal returns,
my_cards is destroyed. You should use the private variable
my_deck in both your deal and print_deck functions, instead.
Using a std::list of Cards is probably not the best choice, as it does
not support random access ( operator [] ). A std::vector of Cards
would be a better choice. Bearing in mind that a deck will always
consist of 52 Cards, a plain array would be an even better choice.
I see what you are saying. The basic thing that I am struggling with is how to use classes in coordination with one another. In this case, my Card class creates a single deck of 52 different cards (ace of spades, three of diamonds, etc.) My Deck class (so far) creates a deck of cards from the Card class that is based on a multiple of 52 cards, so it could be 104 cards, or 156 cards, etc. Once I get the deck established, I then need to add functionality to shuffle it, etc., but that's the next challenge.
For now, Deck has two local variables: my_deck_size (integer of total cards) and my_deck. That's the tricky one. Its type is Card, and is declared like this:
std::list<Card> my_deck;
In my constructor, after I set my_deck_size to 52 or whatever, how do I initialize my_deck? Is something like this?:
my_deck = deal(my_deck_size)
I have to use the Card class within the Deal class because it has member functions that set up each of the cards.
Sorry I'm totally confused by this, but I don't know what to do. Thanks a lot for your help.
The Card class is provided my my instructor, but it's too long to post. What you probably would be interested in is that it has three private variables (my_rank, my_suit, my_value), all size_t.
I am totally confused (not by what you suggested, but by what to do next). I'm just going to lay out my Deck class, as ridiculous and incomplete as it is:
#include "deck.h"
#include "card.h"
usingnamespace std;
namespace csci2270_assignment5
{
// constructors---------------------------------------------------------
// Postcondition: establishes a single deck of 52 cards
Deck::Deck ()
{
my_deck_size = 52;
deal();
}
// Postcondition: establishes a deck of cards consisting of a multiple
// of 52 cards * deck_count
Deck::Deck (size_t deck_count)
{
my_deck_size = deck_count * 52;
deal();
}
// const Member Functions-----------------------------------------------
void Deck::deal()
{
int card_index = 0;
//Create an empty array with enough space for all the cards
Card my_cards[my_deck_size];
//deal the cards
for (int j = 0; j < (my_deck_size / 52); j++)
{
for(int i = 0; i < 52; i++, card_index++)
{
if( i < 13 )
{
my_cards[card_index].set_rank((i % 13) + 1);
my_cards[card_index].set_suit(1);
my_cards[card_index].set_face_up(false);
}
elseif( i < 26 )
{
my_cards[card_index].set_rank((i % 13) + 1);
my_cards[card_index].set_suit(2);
my_cards[card_index].set_face_up(false);
}
elseif( i < 39 )
{
my_cards[card_index].set_rank((i % 13) + 1);
my_cards[card_index].set_suit(3);
my_cards[card_index].set_face_up(false);
}
else
{
my_cards[card_index].set_rank((i % 13) + 1);
my_cards[card_index].set_suit(4);
my_cards[card_index].set_face_up(false);
}
}
}
}
void Deck::print_deck()
{
for( int i = 0; i < my_deck_size; i++ )
{
//cout << my_deck[i] << endl;
}
}
}
This will compile, but it doesn't do what I want. The main problem is in that in deal(), I'm creating an array based on the Card class called my_cards, but what i really need to do is create one in the Deck class. I don't know how to use the functionality from Card within Deck. I'm really stuck.
Thank you for your help. I don't know where else to turn.
Thanks, but I can't do it that way. We are not allowed to use vectors, and I have to store the cards in the Deck private variable my_deck.
Is there anything that you can tell me that will help me accomplish it this way? Maybe the whole idea of the deal() function is the wrong approach? Somehow, I have to wind up with the my_deck filled with the various cards.
You mean my_deck has to be a std::list? This is very strange. How are you supposed to
implement a random shuffle algorithm on a container that doesn't support random access?
Anyway, in this case, you can use your temporary my_cards array,
and once you fill it up, copy it to my_deck, like this (pseudocode):
1 2 3 4
for (each element in my_cards)
{
my_deck.push_back(current element);
}
This was a good idea. I have everything set up, but now my output doesn't work. I just get an empty screen. I'll put it in a different post since it's a little different problem.