Write a program that can deal a deck of cards 1) 4 suits 2) 13 values A, 2-10, J
ID: 3846622 • Letter: W
Question
Write a program that can deal a deck of cards 1) 4 suits 2) 13 values A, 2-10, J, Q, K 3) Prompt for # users (Y) 4) Prompt for # cards/user (X) 5) Deal X cards to Y users, one each, in turn. 20 points for a deck. The deck can be any internal representation you like (vectors, strings, arrays), but when displayed, the user will see it as "A spades", "Q Hearts" or "2 Diamonds". Also, a deck is limited to the 52 possible cards - each deal is not simply a random number between 1 and 52! 20 points for a function to shuffle that deck. Defining a deck of 4 suits/13 values is one thing. Now create a function that when passed a deck, will somehow randomize their positions (think of a "reverse" sort-where instead of looping through the values to put them in increasing/decreasing order, you are purposefully mixing them up). It is possible that the shuffle routine need to be called multiple times to truly "mix them up". 20 points for making the deck into a "class", with at least one constructor method for initializing a new deck, and one to shuffle the cards. 20 points for a game!-Define and implement the rules for any card game you like (black jack, go fish!, crazy eights, war, etc.). Just use the deck and the shuffling routine, then add rules to allow more than one player to take turns. Define what each play is, as well as how to win or lose the game and have the program automatically determine when the game is over and who won or lost.Explanation / Answer
#include <iostream>
#include <vector>
#include <ctime>
#include <sstream>
using namespace std;
//Initilize constant variables
const int Suits = 4;
const int Faces = 13;
//put all the suit values in an array as strings
string suits[Suits]={"Hearts", "Diamonds", "Spades", "Clubs"};
//put all the face values in an array as strings
string faces[Faces]={"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
class Card
{
public:
int face,suit;
//constructer with parameters
Card(int face,int suit)
{
//assigns the paraments above to the two variables face and suit
this->face=face;
this->suit=suit;
}
string toString()
{
string nameSuit = suits[suit];
string nameFace = faces[face];
return nameFace+" of "+nameSuit;
}
};
class DeckOfCards
{
private:
vector<Card> deck;
int currentCard, tempLocation;
public:
DeckOfCards()
/*create deck*/
// Default constructor: assigns the 52 cards to deck
{
for(int i=0;i<Faces;i++)
for(int k=0;k<Suits;k++)
{
Card card(i,k);
deck.push_back(card);
currentCard++;
}
}
void shuffle()
/*shuffle the deck*/
//shuffles the deck once all the cards are assigned/
{
for(int i=0;i<52;i++)
{
tempLocation=rand()%52+1;
Card holder=deck[tempLocation];
deck[tempLocation]=deck[i];
deck[i]=holder;
}
}
void dealCard()
/deals out one card from the deck of 52, refrences class card
/*deal the shuffled cards*/
{
while(currentCard >0)
{
Card returnCard = deck[currentCard];
returnCard.toString();
currentCard--;
}
}
bool moreCards()
{
int size = (int) deck.size();
if(size == 0)
return false;
else
return true;
}
};
int main ()
/*main function*/
{
DeckOfCards deck;
deck.shuffle();
deck.dealCard();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.