Hi all,
How can make the output not to repeat again for many players. here is part of code.

struct
{
int face;
unsigned int value;
} 
card[13] = 
{   {'2', 2},{'2', 3},{'2', 4},
    {'5', 5},{'6', 6},{'7', 7},
    {'8', 8},{'9', 9},{'X', 10},
    {'J', 13},{'Q', 12},{'K', 11},
    {'A', 1}
};
char suit[4]={'D','C','H','S'};

ex of output is (1 player): D9 ,H2,SQ....
(2 player) : CX,H8,D9...

D9 came to second time again!!! pls help me :eek:

Recommended Answers

All 11 Replies

I would create more of a deck than you have right now, and treat it as a stack. You would have 52 cards, each card would have a suit, a face, and a value, and you can shuffle and deal from it easily.

#include <algorithm>
#include <iostream>
#include <string>

struct {
  char suit;
  std::string face;
  int value;
} deck[52];

struct {
  const char *face;
  int value;
} cardValues[] = {
  {"2", 2},{"3", 3},{"4", 4},
  {"5", 5},{"6", 6},{"7", 7},
  {"8", 8},{"9", 9},{"10", 10},
  {"J", 13},{"Q", 12},{"K", 11},
  {"A", 1}
};

const char cardSuits[] = {'D','C','H','S'};

int main()
{
  using namespace std;

  int i = 0;

  // Build the deck
  for ( int suit = 0; suit < 4; suit++ ) {
    for ( int face = 0; face < 13; face++ ) {
      deck[i].face = cardValues[face].face;
      deck[i].value = cardValues[face].value;
      deck[i].suit = cardSuits[suit];
      ++i;
    }
  }

  // Shuffle the deck
  random_shuffle( deck, deck + 52 );

  // Pop off hands of at most 5 until the deck is empty
  int n = 1;

  for ( i = 0; i < 52; ) {
    cout << "Hand #" << n++ << '\n';
    for ( int j = 0; i < 52 && j < 5; i++, j++ )
      cout << deck[i].suit << " -- " << deck[i].face << " -- " << deck[i].value << '\n';
    cout << '\n';
  }

  return 0;
}

Thank you! But when everytime i ran the program, output came the same. for example:

Hand #1
D --A--1
D--3--3
D--J--13
S--Q--12
D--2--2

like that for Hand 1 for everytime.
And other hands too.:rolleyes:

Thank you!

You're welcome! :)

But when everytime i ran the program, output came the same.

Your random_shuffle() uses rand() to generate the shuffle and rand() is always seeded by default to 1. You can just change the seed each time and get something different.

#include <cstdlib>
#include <ctime>
// Other includes...

// Deck definitions...

int main()
{
  using namespace std;

  // Build the deck...

  // Shuffle the deck
  srand( (unsigned int)time( 0 ) );
  random_shuffle( deck, deck + 52 );

  // Display the hands...

  return 0;
}

Your random_shuffle() uses rand() to generate the shuffle and rand() is always seeded by default to 1. You can just change the seed each time and get something different.

Edit - sorry, I misunderstood your post

Be careful with how you phrase that bit of advice. Seeding rand with time is a good idea, however, the call to srand() should only occur once - usually at the beginning of the program will do, in order to use a different seed whenever rand() is called.
Multiple calls to srand() will generally have the opposite effect

Be careful with how you phrase that bit of advice. Seeding rand with time is a good idea, however, the call to srand() should only occur once - usually at the beginning of the program will do, in order to use a different seed whenever rand() is called.
Multiple calls to srand() will generally have the opposite effect

Thank you, I will be more careful next time to say how one call to srand() takes care of multiple calls to rand().

Oh ! now it comes real. I do appreicate of that .
But can i ask one more Q?
I want to know what is following ths:

random_shuffle( deck, deck + 52 );

it seem to be function call. but no....:rolleyes:

And other one is when I ran with rand() I got one message..

Error	1	error C2661: 'rand' : no overloaded function takes 1 arguments

Oh ! now it comes real. I do appreicate of that .
But can i ask one more Q?
I want to know what is following ths:

random_shuffle( deck, deck + 52 );

it seem to be function call. but no....:rolleyes:

It is a function call. random_shuffle() is a standard C++ function from the <algorithm> header. It saves you the trouble of writing your own shuffling code. :) If you really wanted to, you could fake random_shuffle() manually.

namespace Raye {
  template <class T>
  inline void swap( T& a, T& b )
  {
    T temp = a;
    a = b;
    b = temp;
  }

  template <class T>
  inline void random_shuffle( T *first, T *last )
  {
    if ( first != last) {
      for ( T *i = first + 1; i != last; i++ ) {
        T *b = first + rand() % ( i - first + 1 );
        T temp = *i;
        *i = *b;
        *b = temp;
      }
    }
  }
}

But why bother when the standard library already gives it to you for free? :)

And other one is when I ran with rand() I got one message..

Error	1	error C2661: 'rand' : no overloaded function takes 1 arguments

It looks like you didn't use rand() the right way. I can't offer any other advice without seeing the code that caused that error. But I can guess that you wanted to limit the range of rand and tried to do that with an argument.

// This does not give you a random number from 0 to 5
rand( 5 );

// This does give you a random number from 0 to 5
rand() % 5;

// This does too, and it's theoretically better, but more cryptic
 rand() * ( 1.0 / ( RAND_MAX + 1.0 ) ) * 5;
struct {
  char suit;
  std::string face;
  int value;
} deck[52];

Ravalon, have you enabled all the warnings in your compiler settings (-Wall) since the above flags a warning saying "error: no matching function for call to `random_shuffle(<anonymous struct>[52], <anonymous struct>*)' ". You better name the struct just for standard's sake, its not good to let warnings hang around in your programs.

It is a function call. random_shuffle() is a standard C++ function from the <algorithm> header. It saves you the trouble of writing your own shuffling code.

...with such talk of random_shuffle, I would recommend the people to read this.

namespace Raye {
  template <class T>
  inline void swap( T& a, T& b )
  {
    T temp = a;
    a = b;
    b = temp;
  }

  template <class T>
  inline void random_shuffle( T *first, T *last )
  {
    if ( first != last) {
      for ( T *i = first + 1; i != last; i++ ) {
        T *b = first + rand() % ( i - first + 1 );
         T temp = *i;
        *i = *b;
        *b = temp;
      }
    }
  }
}

I wonder why you wrote the swap subroutine...;)

commented: I learned something new! -Raye +1

Ravalon, have you enabled all the warnings in your compiler settings (-Wall) since the above flags a warning saying "error: no matching function for call to `random_shuffle(<anonymous struct>[52], <anonymous struct>*)' ". You better name the struct just for standard's sake, its not good to let warnings hang around in your programs.

I did compile with full warnings, but my compiler isn't that smart. I guess I was trying to be too C-like and got burned yet again. :) The best part is that I originally had the structs named and went out of my way to remove them. ;)

I wonder why you wrote the swap subroutine...;)

Subconsciously making the snippet look more complex than it needed to be I guess.

I did compile with full warnings, but my compiler isn't that smart.

I can fairly bet that its a MS Compiler....:D

Subconsciously making the snippet look more complex than it needed to be I guess.

You show all signs of a frustrated C++ programmer.:cheesy:

I can fairly bet that its a MS Compiler....:D

That's why I don't gamble.

You show all signs of a frustrated C++ programmer.:cheesy:

No, I'm just a frustrated programmer in general. Juggling multiple languages with similar syntax and semantics but subtle differences does that to people. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.