I have difficulties with my project!

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!


You can use the rand function (http://www.cplusplus.com/reference/clibrary/cstdlib/rand/) and convert from the resulting int to whatever you're using to represent suits/numbers.

Are you sure you want to randomly generate individual cards though? Wouldn't you want all 52 cards, and randomly choose from those?
Last edited on
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.
http://www.cplusplus.com/doc/tutorial/structures/

I hope we're not doing someone's homework for them.
Last edited on
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 :(
Hi everyone ! I came across this post and I happen to have the exact same project too!!!

Sadly to say the project only insisted us to use class and not structures

I was thinking maybe if classes can be used?

PS: I also learn a simple structuring lesson at youtube: http://www.youtube.com/watch?v=-IrueTrxNHA
Just change keyword struct with keyword class ;)

struct and class are actually the same, except struct's members are initially public while class's memebers are initially private.
Thanks for the advice!!! :D

Some questions from my group is that we need some help in the initialising part.

We wanted to to an array and a "for" loop for the numbers but we got stuck with the alphabets
"Ace" "King" "Queen" and "Jack"

Here's what my group did and got stuck :

#include <iostream>
#include <string>
using namespace std;

struct CARD
{
string rank[4];
string suit[13];
}Cards[4][13];

class Deck //must contain 52 cards
{
private:
int x, y;
public:
void Shuffle(void); //initialize cards & shuffle
void HandOneCard (void); //pass card
};


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

cout<<"Thank you for playing"<<endl;
}
Topic archived. No new replies allowed.