This is not an actual game, just deal cards to two players. Using your exisiting
ID: 3535122 • Letter: T
Question
This is not an actual game, just deal cards to two players.
Using your exisiting Card Deck code as a basis, use your
functions to deal cards to two players as in a game of
BlackJack. Deal each player two cards initially, then ask the
first player if they want an additional card. Repeatedly ask
until the first player wants no more cards. Then ask the second
player if they want an additional card. Repeatedly ask until
the second player wants no more cards. When the second player
has been dealt their cards, the program can quit.
Notes:
-----
- Just deal the hands. No scoring or evaluating of the hands
is needed
- Display the hands as the cards are dealt.
- Display both hands when the dealing is done.
- You can assume a player will never hold more than 10 cards
in their hand.
Explanation / Answer
//working code
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <ctime>
using namespace std;
vector<string> cardDeck;
vector<string> playerHand1;
vector<string> playerHand2;
const int DEAL_CARDS = 2;
//Declare Functions
void ResetDeck();
void Shuffle();
void DealACard();
string CStr(int);
int main()
{
//SCREEN SETUP
cout << "------------------------------------------------- ";
cout << "H = Hearts, D = Diamonds, C = Clubs, S = Spades ";
cout << "------------------------------------------------- ";
cout << "Player 1 would you like to be dealt in (y/n)? ";
char yn;
cin >> yn;
if (yn == 'n' || yn != 'y') return 0;
//Build a fresh deck of cards
ResetDeck();
//Pick random seed based off time so
//our shuffle is different with each run
srand(time(0));
//Shuffle the cards
Shuffle();
//Deal the cards to the player and
//display them on the screen
cout << " Here's your cards: ";
for (int i=0; i < DEAL_CARDS; ++i)
{
DealACard();
cout << playerHand1[i] << " ";
}
cout << " Player 2 would you like to be dealt in (y/n)? ";
cin >> yn;
if (yn == 'n' || yn != 'y') return 0;
//Build a fresh deck of cards
ResetDeck();
//Pick random seed based off time so
//our shuffle is different with each run
srand(time(0));
//Shuffle the cards
Shuffle();
//Deal the cards to the player and
//display them on the screen
cout << " Here's your cards: ";
for (int i=0; i < DEAL_CARDS; ++i)
{
DealACard();
cout << playerHand2[i] << " ";
}
//
while(yn!='n')
{
cout << " Player 1 would you like to be dealt again (y/n)? ";
char yn;
cin >> yn;
if (yn == 'y' || yn != 'n'){
//Build a fresh deck of cards
ResetDeck();
//Pick random seed based off time so
//our shuffle is different with each run
srand(time(0));
//Shuffle the cards
Shuffle();
//Deal the cards to the player and
//display them on the screen
cout << " Here's your cards: ";
for (int i=0; i < 1; ++i)
{
DealACard();
cout << playerHand1[i] << " ";
}
}
if (yn == 'n' || yn != 'y'){
break;
}
}
while(yn!='n')
{
cout << " Player 2 would you like to be dealt again (y/n)? ";
char yn;
cin >> yn;
if (yn == 'y' || yn != 'n'){
//Build a fresh deck of cards
ResetDeck();
//Pick random seed based off time so
//our shuffle is different with each run
srand(time(0));
//Shuffle the cards
Shuffle();
//Deal the cards to the player and
//display them on the screen
cout << " Here's your cards: ";
for (int i=0; i < 1; ++i)
{
DealACard();
cout << playerHand2[i] << " ";
}
}
if (yn == 'n' || yn != 'y'){
break;
}
}
cout << " If we were playing a game, we could do something here! ";
system("pause");
return 0;
}
void Shuffle() { random_shuffle(cardDeck.begin(),cardDeck.end()); }
void DealACard()
{
//Add top card to playerHand; remove that card from the cardDeck
playerHand1.push_back(cardDeck[0]);
playerHand2.push_back(cardDeck[0]);
cardDeck.erase(cardDeck.begin());
}
string CStr(int n)
{
stringstream s;
s << n;
return s.str();
}
void ResetDeck()
{
//Erase old deck and players hand
cardDeck.clear();
playerHand1.clear();
playerHand2.clear();
//Build 2's through 10's
for (int i=2; i<11; ++i)
{
cardDeck.push_back(CStr(i) + "H");
cardDeck.push_back(CStr(i) + "D");
cardDeck.push_back(CStr(i) + "C");
cardDeck.push_back(CStr(i) + "S");
}
//Build Jacks
cardDeck.push_back("JH");
cardDeck.push_back("JD");
cardDeck.push_back("JC");
cardDeck.push_back("JS");
//Build Queens
cardDeck.push_back("QH");
cardDeck.push_back("QD");
cardDeck.push_back("QC");
cardDeck.push_back("QS");
//Build Kings
cardDeck.push_back("KH");
cardDeck.push_back("KD");
cardDeck.push_back("KC");
cardDeck.push_back("KS");
//Build Aces
cardDeck.push_back("AH");
cardDeck.push_back("AD");
cardDeck.push_back("AC");
cardDeck.push_back("AS");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.