5:53 LTE I will be grading these problems using Visual Studio 2017 on a Windows
ID: 3757100 • Letter: 5
Question
5:53 LTE I will be grading these problems using Visual Studio 2017 on a Windows 10 machine. You should be turning in only cpp and .h files. I will be placing the .h files in the Header Files folder of a Visual Studio 2017 empty project and the .cpp files in the Source Files folder the same Visual Studio 2017 empty project and running the program using Debug ->Start Without Debugging. It is incumbent on you to make sure the code you submit runs in that environment if you are using a different programming environment than the ones in the Henning classroom. Make sure you are doing everything asked for in the problem clarification. Programming style should be the style used in our textbooks Problem 1 (65 points) Card Shuffling and Dealing) Part 1 Create a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and a driver program with a main function. Class Card should provide: Private data members face and suit ofExplanation / Answer
As per the instructions we have to solve only oen problem so I am solving the first problem.
//Header file----------------------------------------------------------------------------------------
//always include iostream
#include <iostream>
//random number generator
#include <cstdlib>
//random number generator
#include <ctime>
using namespace std;
//Initilize constant variables
const int SIZE = 52;
//create a class called card
class card
{
public:
//default constructer
card();
//constructer with parameters
card(string cardFace, string cardSuit);
//print function
string print();
private:
//create two card variables face and suit
string face;
string suit;
};
//create a class for deckOfCards
class deckOfCards
{
public:
// Default constructor: assigns the 52 cards to deck
deckOfCards();
//shuffles the deck once all the cards are assigned
void shuffle();
//deals out one card from the deck of 52, refrences class card
card dealCard();
private:
//variable card with a pointer to deck
card deck[SIZE]; // an array of cards of size SIZR
//keep track of what card you are dealing with
int currentCard;
};
//Main function
//-------------------------------------------------------------------------------------------------
int main()
{
//declare deckofcards(from class) called deck
deckOfCards deck;
//declate car called(from class) current card
//card currentCard;
//shuffle the deck that you just initalized
deck.shuffle();
//determine how many cards you want to print out to the user
//right now it is two because we decided that each player will get two cards when they start
for( int i = 0; i <= 2; i++)
{
//set current card equal to deck.dealcard
//the return value will replace it
currentCard = deck.dealCard();
//include to actually print out current card in the format we want
cout << currentCard.print() << endl;
}
return 0;
}
//------------------------------------------------------------------------------------------------//
//file implementation
//default constructor
card::card()
{
//nothing goes in here
}
//constructor with two parameters
card::card(string cardFace, string cardSuit)
{
//assigns the paraments above to the two variables face and suit
face = cardFace;
suit = cardSuit;
}
//print function defintion
string card::print()
{
//return the way the card will be displayed
return (face + " of " + suit);
}
//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]);
}
}
//shuffles the deck once all the cards are assigned
void deckOfCards::shuffle()
{
//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.