Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing a blackjack program for fun

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I'm trying to write a blackjack program in C++ to kinda refresh/relearn my C++. I am trying to write a shuffle and check_deck function so that when the deck is shuffled, there are no duplicates. Here is my code with the functions I think should work but don't. Feel free to point out any stupid code on my part. I have a somewhat basic understanding of C++ (i.e I understand arrays, pointers, functions) so please provide solutions using the extent of C++ I know, if you would be so kind. Thanks!!

Code:
/****************************************************************************************
*		Text Black Jack
****************************************************************************************/

#include <iostream>				
#include <fstream>				
#include <iomanip>
#include <ctime>			
using namespace std;

struct cards {int suit; int value;};

typedef struct cards Card;


void shuffle(Card n_deck[]);
void deal(Card ndeck[]);
int check_deck(Card ndeck[], int Face, int Value, int i);


int main()
{
   /* initialize deck array */
   Card deck[52]={0};

   srand(time(0)); /* seed random-number generator */

   shuffle(deck);
   deal(deck);

   return 0; /* indicates successful termination */

} /* end main */

/* shuffle cards in deck */
void shuffle(Card n_deck[])
{
   int suit=4, value=13;
  
   for (int i=0; i<52;  i++) 
   {
	   do
	   {
			suit=rand()%4;
			value=rand()%13;
	   }while(check_deck(n_deck, value, suit, i));
	   n_deck[i].suit=suit;
	   n_deck[i].value=value;

   }

} 

void deal(Card ndeck[] )
{
   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
   
  
   const char *face[ 13 ] = 
      { "Ace", "Deuce", "Three", "Four", 
        "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King" };

   for (int i=0; i<52; i++)
	   cout<<face[ndeck[i].value]<<" of "<<suit[ndeck[i].suit]<<endl;
} 

int check_deck(Card ndeck[], int Suit, int Value, int i)
{
	int ret=0;

	for (int j=0; j<i; j++)
	{
		if ((ndeck[j].suit==Suit)&&(ndeck[j].value==Value))
			ret=1;
	}

	return ret;
}

I think the problem is in the shuffle function, but I can't see it.
 
A more efficient way of shuffling would be

1) Set up your deck sequentially in an array of 52 cards i.e. A to K for each suit.
2) Loop from 52 to 2. Get a random number mod the loop counter. Swap card at loop counter and random card chosen.

You only go through the array once and it is completely shuffled. There is no need to search for duplicates.
 
of you can just use the random_shuffle() function in the <algorithm> header.

Code:
vector<Card> deck;
Card temp;

for ( int suit = 0; suit < 4; ++suit )
{
   for ( int value = 0; value < 13; ++value )
   {
      temp.suit = suit;
      temp.value = value;
      deck.push_back( temp );
   }
}

random_shuffle( deck.begin(), deck.end() );
 
You should use a vector, but random_shuffle will work with the Card array as well:
Code:
random_shuffle(deck, deck + 52);
Also remember that for many implementations of random_shuffle, you have to have a call to srand if you want to seed the random number generator just as if you were using rand yourself.
 
Update, thanks guys, I used part of cpjust's solution with uolj's solution, I kept it an array and initialized the way cpjust did and then used uolj's suggestion to shuffle the array.

I have been busy with school work but plan to keep working on this when I get time. I will update on here every once in a while.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top