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!!
I think the problem is in the shuffle function, but I can't see it.
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.