//declare variables and functions
int rand_0toN1(int q); //generates a random number up to last_card_position
void swap_to_last(); //moves the selected card to the last selected position and decreazses last_card position
void shuffle_the_deck(); //randomizes the deck order
void make_deck(); //makes the deck in order
void display_the_deck(); //displays the deck for debugging
int last_card_position = 52; //holds position of the last card in the array still valid for drawing
char*suits[4] = {"heart","Spade","diamond","Club"};
char *ranks[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
struct build_deck { // define the deck structure
char *suit;
char *rank;
};
build_deck deck[53]; //holds the deck we make
//53 is for shuffling to swap cards
int main()
{
int i, n;
srand(time(NULL)); // seeds the random number with current time
// make the deck
cout << "Making the deck" << endl; // for debugging
make_deck(); // build a deck of 52 cards
cout << endl;
while(1)
{
cout << "Shuffling the deck..." << endl;
shuffle_the_deck();
cout << "Enter no. of cards to deal (0 to exit): ";
cin >> n;
if (cin.fail()) break;
// test for valid entries
// but this does not capture decimals or letters. Need to add that feature.
while ( (n>52) || (n<0) )
{
cout << endl << endl << "WARNING: Number must be between 1 and 52!";
cout << endl << "Enter no. of cards to deal (0 to exit): ";
cin >> n;
if (cin.fail()) break;
}
if (n == 0) break;
// display the dealt cards
for (i = 0; i < n; i++) {
cout << deck[i].rank << "" << deck[i].suit << endl;
}
cout << endl << endl;
cout << "Cards have been dealt." << endl << endl;
}
return 0;
}
// Make the Deck
void make_deck() {
int s, r, c; //s for suit, r for rank, c for cards
c = 0; {
for (s=0; s<4; s++) {
for (r=0; r<13; r++) {
deck[c].rank = ranks[r];
deck[c].suit = suits[s];
c++;
}
}
}
}
// display the deck -- used for debugging
void display_the_deck() {
int display_card;
for (display_card=0; display_card<52; display_card++)