I need the following in C++. 1. Specify, design, and implement a class for a car
ID: 3750703 • Letter: I
Question
I need the following in C++.
1. Specify, design, and implement a class for a card in a deck of playing cards. The object should contain methds for setting and retrieving the suit and rank of a card.
2. Use the card class developed to create a new class for a deck of cards. The deck class has a sequence with a capacity of 52 to hold the cards. The constructor assigns 52 cards in order of suit and rank to the sequence. A friend function should display the entire deck using words (i.e., "the ace of spades").
3. Modify the card and deck classes so that they will be useful in a program to shuffle a deck of cards, deal all the cards to four plaers, and display each player's hand. For the shuffle function, generate a random number k for each card index whose value is from 0 to index. Then define a swap function to exchange the values for card[index] and card[k], using a swap function that you define.
4. Use a cirucular linked list to run a simple simulation of a card game. Use the card and deck classes, and shuffle and deal already created. Create a player class to hold a hand of dealt cards. During each turn, a playerwill discard a card. Use rand() to determine who gets the first turn in each hand, and make sure each person has a turn during every hand. The program ends when all cards have been played.
The program should also include the following in the program:
a) Construct an object oriented approach that implements classes with private data, public constructors (default, parameter, and copy), public accessor and mutator methods for private data, friend functions and overloaded operators as appropriate. Only const accessor methods may be declared inline. Class methods should use passed parameters only when they are not already stored as private data members in the object. Include the following additional (to the textbook) design requirements in your solution: Card class declares and uses enumerated types for suit (clubs, diamonds, hearts, spades) and card rank (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace) Card class overloadsExplanation / Answer
Program to Generate Card Deck
#include<iostream.h> #include<cstdlib> //random number generator #include<ctime> //random number generator
using namespace std;
const int SIZE = 52;
class card{ public: card(); //default constructer card(string cardFace,string cardSuit); strnig print();
private: string face; //2 card variables face and suit string suit;
};
class deckOfCards { public: deckOfCards(); //assigns the 52 cards to deck void shuffle(); //suffle the deck once all the cards are assigned card dealCard();
private: card deck[SIZE]; int currentCard; };
int main() {
deckOfCards deck; //declare deckofcards(from class) called deck deck.suffle(); for(int i=0; i<=2; i++) { currentCard = deck.dealCard(); cout<<currentCard.print()<<endl;
}
return 0;
}
card::card() //default constructor
{
//nothing goes in here
}
card::card(string cardFace, string cardSuit) //constructor with two parameters
{
//assigns the paraments above to the two variables face and suit
face = cardFace;
suit = cardSuit;
}
string card::print()
{
return (face + " of " + suit); //return the way the card will be displayed
}
//assigns the 52 cards to deck
deckOfCards::deckOfCards()
{
//put all the face values in an array as strings
string faces[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
//put all the suit values in an array as strings
string suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
//initilize deck from the card class to a new array using the word "new"
deck = new card[SIZE];
//current card is equal to zero
currentCard = 0;
//create a for loop to literally place each card into the new array
for(int count = 0; count < SIZE; count++)
{
//deck at postion count will be equal to card, each with a different face and suit
deck[count] = card(faces[count % 13], suits[count / 13]);
}
}
void deckOfCards::shuffle() //shuffles the deck once all the cards are assigned
{
//start at current card
currentCard = 0;
//create a for loop so all 52 cards will be shuffled
for(int first = 0; first < SIZE; first++)
{
//create an int called second and set it equal to the random operator
int second = (rand() + time(0)) % SIZE;
//create an int called temp and set it equal to the deck at the first postiion
card temp = deck[first];
//swap deck at first and second postion
deck[first] = deck[second];
//swap deck and temp
deck[second] = temp;
}
}
card deckOfCards::dealCard()
{
//if we are out of cards
if(currentCard > SIZE)
{
//shuffle
shuffle();
}
//if we are not out of cards
if( currentCard < SIZE)
{
//return deck at that current card and then increment
return (deck[currentCard++]);
}
//return the first card in the deck that we just found
return (deck[0]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.