To the following code add: a) Menu-driven program add functions for: shuffling t
ID: 3677868 • Letter: T
Question
To the following code add:
a) Menu-driven program
add functions for:
shuffling the deck
playing some simple games vs. computer
cutting for highest card
fewest cards to reveal an ace
bonus - WAR!
#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
Note:
i don't know how to play BONUS game hence i have implimented only 3 functions
Code:
#include <iostream>
#include<cstdlib>
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);
Deck shuffleDeck(Deck aDeck);
bool cuttingHigestCard(Deck aDeck);
bool firstAce(Deck aDeck);
int main()
{
Deck myDeck;
int ch;
myDeck = freshDeck();
while(1)
{
cout<<"1. create a fresh deck"<<endl;
cout<<"2. shuffle deck"<<endl;
cout<<"3. play cut the highest card game against computer"<<endl;
cout<<"4. play the first ace game against computer"<<endl;
cout<<"5. list Deck"<<endl;
cout<<"6. exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
myDeck = freshDeck();
break;
case 2:
myDeck = shuffleDeck(myDeck);
break;
case 3:
myDeck = shuffleDeck(myDeck);
if(cuttingHigestCard(myDeck))
cout<<"User one the game"<<endl;
else
cout<<"computer won the game"<<endl;
cout<<"the currunt deck is"<<endl;
listDeck(myDeck);
break;
case 4:
myDeck = shuffleDeck(myDeck);
if(firstAce(myDeck))
cout<<"User one the game"<<endl;
else
cout<<"computer won the game"<<endl;
cout<<"the currunt deck is"<<endl;
listDeck(myDeck);
break;
case 5:
listDeck(myDeck);
break;
case 6:
exit(0);
default:
cout<<"enter a valid option"<<endl;
}
}
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 << " ";
}
}
Deck shuffleDeck(Deck aDeck)
{
int i,j;
Card temp;
for(i=0;i<52;i++)
{
j=rand()%52;
temp = aDeck.myCards[i];
aDeck.myCards[i]=aDeck.myCards[j];
aDeck.myCards[j]=temp;
}
return aDeck;
}
bool cuttingHigestCard(Deck aDeck)
{
cout << "User got "<<aDeck.myCards[0].value << " of " << aDeck.myCards[0].suit<<endl;
cout << "Computer got "<<aDeck.myCards[1].value << " of " << aDeck.myCards[1].suit<<endl;
return (aDeck.myCards[0].value > aDeck.myCards[1].value);
}
bool firstAce(Deck aDeck)
{
int i=0;
while(1)
{
cout << "User got "<<aDeck.myCards[i].value << " of " << aDeck.myCards[i].suit<<endl;
if(aDeck.myCards[i].value==1)
return true;
cout << "Computer got "<<aDeck.myCards[i+1].value << " of " << aDeck.myCards[i+1].suit<<endl;
if(aDeck.myCards[i+1].value==1)
return false;
i=i+2;
}
}
Output:
6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.