455,513 Members | 1,653 Online 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         const int card = 13;     const int suit = 4;     const int player = 4;     char *cards[card] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};     char *suits[suit] = {"S", "C", "D", "H"}; int main() {       int i, Card, Suit, points, total;       srand(time(NULL));       cout << "Note: S is Spades, C is Clubs, H is Hearts and D is Diamonds\n\n";       //player 1     cout << "Player 1\n";          for (int i = 0; i < 13; i++)          {              Card= rand() % 13;              Suit= rand() % 4;              cout << cards[Card] <<  " of " << "[" << suits[Suit] << "]" << ", ";            }              cout << "\nTotal Points: " <<  total;          cout << "\n-------------------------------------------";       //player 2      cout << "\n\nPlayer 2\n\n";                        for (int i = 0; i < 13; i++)          {              Card= rand() % 13;              Suit = rand() % 4;              cout << cards[Card] <<  " of " << "[" << suits[Suit] << "]" << ", ";          }          cout << "\nTotal Points: ";          cout << "\n-------------------------------------------";   }   May 26 '07 #1
3 Replies

 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

 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; 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 