To the following code add: a) Menu-driven program add functions for: shuffling t
ID: 3678629 • Letter: T
Question
To the following code add:
a) Menu-driven program
add functions for:
shuffling the deck (important)
playing some simple games vs. computer
cutting for highest card
fewest cards to reveal an ace
pause the program so the output can be seen
#include <iostream>
using namespace std;
struct Card
{
char suit;
int value; // A-1 .... J-11, Q-12, K-13
};
struct Deck
{
Card myCards[52];
};
Deck freshDeck();
void listDeck(Deck);
int main()
{
Deck myDeck;
myDeck = freshDeck();
listDeck(myDeck);
return(0);
}
Deck freshDeck()
{
Deck aDeck;
int cardValue = 1;
char cardSuit = 'c';
for(int i = 0; i < 52; i++)
{
aDeck.myCards[i].suit = cardSuit;
aDeck.myCards[i].value = cardValue;
if(cardValue == 13)
{
cardValue = 0;
switch(cardSuit)
{
case 'c':
cardSuit = 's';
break;
case 's':
cardSuit = 'd';
break;
case 'd':
cardSuit = 'h';
break;
}
}
cardValue++;
}
return aDeck;
}
void listDeck(Deck aDeck)
{
cout << "List of deck contents" << endl << endl;
for(int i = 0; i < 52; i++)
{
cout << aDeck.myCards[i].value << " of " << aDeck.myCards[i].suit;
if((i+1) % 4 == 0)
cout << " ";
else
cout << " ";
}
}
Explanation / Answer
Hi below i have written the codes for your reference,
//shuffling the deck
// A function to generate a random permutation of arr[]
void randomize ( int arr[], int n )
{
// Use a different seed value so that we don't get same
// result each time we run this program
srand ( time(NULL) );
// Start from the last element and swap one by one. We don't
// need to run for the first element that's why i > 0
for (int i = n-1; i > 52; i--)
{
// Pick a random index from 0 to i
int j = rand() % (i+1);
// Swap arr[i] with the element at random index
swap(&arr[i], &arr[j]);
}
}
playing some simple games vs. computer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.