By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

random card game need help in C++

P: 5
this code works but theres a problem wif it. wen it generates for 4 players, it has repeated values i.e 2 of diamond appear twice.

how do i avoid this?

and

i hav no idea how to program the points system. so numbers from 2 to 10 worth zero points, jack = 1 pt, queen = 2 pts, king = 3 pts, ace = 4pts

can ne1 help me to start the coding for teh points system as well?


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     const int card = 13;
  4.     const int suit = 4;
  5.     const int player = 4;
  6.     char *cards[card] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  7.     char *suits[suit] = {"S", "C", "D", "H"};
  8. int main()
  9. {
  10.  
  11.     int i, Card, Suit, points, total;
  12.  
  13.     srand(time(NULL));
  14.  
  15.     cout << "Note: S is Spades, C is Clubs, H is Hearts and D is Diamonds\n\n";
  16.  
  17.     //player 1
  18.     cout << "Player 1\n";
  19.          for (int i = 0; i < 13; i++)
  20.          {
  21.              Card= rand() % 13;
  22.              Suit= rand() % 4;
  23.              cout << cards[Card] <<  " of " << "[" << suits[Suit] << "]" << ", ";
  24.  
  25.          }
  26.  
  27.  
  28.          cout << "\nTotal Points: " <<  total;
  29.          cout << "\n-------------------------------------------";
  30.  
  31.     //player 2 
  32.     cout << "\n\nPlayer 2\n\n";              
  33.          for (int i = 0; i < 13; i++)
  34.          {
  35.              Card= rand() % 13;
  36.              Suit = rand() % 4;
  37.              cout << cards[Card] <<  " of " << "[" << suits[Suit] << "]" << ", ";
  38.          }
  39.          cout << "\nTotal Points: ";
  40.          cout << "\n-------------------------------------------";
  41.  
  42. }
  43.  
May 26 '07 #1
Share this Question
Share on Google+
3 Replies


DeMan
100+
P: 1,806
you could create a new array, that represents all the cards. This value can be a boolean of whether a particular card is allocated. When you select a random card, check the state of this boolean. if it has already been selected, then select another.

Perhaps (or perhaps not) a more efficient way, is to store each value in a linked list. When that value is dealt, remove it from the list. Then select your random number as being between 0 and the size of the list, and choose this element in the list.
May 26 '07 #2

P: 5
you could create a new array, that represents all the cards. This value can be a boolean of whether a particular card is allocated. When you select a random card, check the state of this boolean. if it has already been selected, then select another.

Perhaps (or perhaps not) a more efficient way, is to store each value in a linked list. When that value is dealt, remove it from the list. Then select your random number as being between 0 and the size of the list, and choose this element in the list.
so how do i do tat??
May 26 '07 #3

DeMan
100+
P: 1,806
I will give an example of 1, because 2 is more effort and requires an understanding of fundamental programmign concepts:

[code=cpp]
bool cards[52];

int getCard()
{
int card = rand() % 52;
while(cards[card] )
{
card = rand() % 52;
}
cards[card] = true;
return card;
}

int main (char* argv, int args)
{
/* Initialise to false */
for(int i=0; i<52; i++)
{
cards[i]= false;
}
int card = getCard();
}

[code]

you can then use basic arithmetic to decide which card is which..
May 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.