I m having some difficulties with my project and i would appreciate some help.
The program is to simulate a simple version of a poker game. The team is required to create a class for the Card with at least the following attributes:
- Suit. That represents one of these four: Spades, Hearts, Diamonds and Clubs.
- Rank. Thirteen values running from two to ten, Jack, Queen, King, and Ace.
The team must design the class Deck. This class must contains 52 cards; each unique by the suit and rank values. The class Deck should have a function call Shuffle, to initialize the cards in a random order into an array, or perhaps to allow the user to pick random cards from the array of 52 cards.Finally, a class Player should also be design, this class is to receive five cards from the deck, and this could be done with a function SetCard in the class player and a function HandOneCard from the Deck class.
Im thinking of using a function to randomly generate one suit and one rank. but i dunno which function to use. can anyone help me with this?
Thanks!
So are u saying instead of randomly picking a suit and a rank to generate a card. i should randomly pick one card from the 52 cards that has it suits and ranks?
You need something to represent each card. Consider this simple suggestion..
1. represent a card using a struct (holding both suit and face)
2. represent a deck using an array of 52 struct cards
To shuffle
1. loop through the deck (52 cards)
2. randomize an index (random card from the deck)
3. swap the current card with the generated random card
So what i need to do now is to assign one suit and rank to each of the 52 cards. and then using the random function to randomly pick a card from the deck of 52. But how can i assign one suit and one rank to the 52 cards? Btw wads a struct. I have not learnt it yet.
If you don't know what a struct is, you definitely have to learn it now http://www.cplusplus.com/doc/tutorial/structures/
or else it's going to be a pain in the ass to represent a card in a raw 2d array :(
void Deck::Shuffle(void) //initialize cards & shuffle
int i;
int deck[52];
for (x=0; x<4; x++)
{
for (y=0; y<13; y++)
{
Cards[x][y]=y+1;
}
srand ( time(NULL) );
cout<<"Shuffling..."<<endl;
for(i=0; i<52; i++)
{
// generate a random index to swap with the card at index i.
int j = rand() % 52;
int temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
class Player : public Deck
{
private:
int z;
public:
void SetCard(void); //receive 5 cards from deck
void CheckHand(void); //displays on screen for combinations
};
void Player::SetCard(void)
{
int n;
for (n=0; n<5; n++)
{
cout<< (blank) <<"\t\t";
}
}
void Player::CheckHand(void)
{
//check hand gonna be very long
}
void main()
{
Player plays;
int i, opt;
cout<<"Poker Game Options:"<<endl;
cout<<"\t\t 1) Shuffle and hand"<<endl;
cout<<"\t\t 2) Exit"<<endl;
cin>>opt;
if (opt==1)
{
plays.Shuffle();
plays.HandOneCard();
plays.SetCard();
plays.CheckHand();
}