Card Shuffling and Dealing Create a program to shuffle and deal a deck of cards.
ID: 3555455 • Letter: C
Question
Card Shuffling and Dealing
Create a program to shuffle and deal a deck of cards. The program should consist of a class Card, class DeckOfCards and a driver program (.cpp file with a main). Each class/cpp source file should have a corresponding .h file.
Class Card should provide:
a) Data members face and suit of type int.
b) A constructor that receives two ints representing the face and suit and uses them to initialize the data members.
c) Two static arrays of strings representing the faces and suits.
d) A toString function that returns the Card as a string in the form "face of suit." You can use the + operator to concatenate strings.
Class DeckOfCards should contain:
a) An array of Cards named deck to store the Cards.
b) An integer currentCard representing the next card to deal.
c) A default constructor that initializes the Cards in the deck.
d) A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the array of Cards. For each Card, randomly select another Card in the deck and swap the two Cards.
e) A dealCard function that returns the next Card object from the deck.
f) A moreCards function that returns a bool value indicating whether there are more Cards to deal.
The driver program should create a DeckOfCards object, shuffle the cards, and then deal the 52 cards. To deal, just print out the cards in a decent format (use setw) and print a newline every 4 cards.
I need this to be answered in the form of what I put in each seperate box. I'm guessing the format would be something like this?
Card.h-the code that goes into this
DeckOfCards.h-the code that goes into this
Driver.cpp-the code that goes into this.
This question has been answered here before but I have 14 errors from it.
Explanation / Answer
DO RATE if satisfied
==================Card.h====================
/*
* Card.h
*
* Created on: Apr 24, 2014
*/
#ifndef CARD_H_
#define CARD_H_
#include <string>
/*
*
*/
class Card {
private:
int face;
int suit;
public:
static const std::string faces[];
static const std::string suits[];
Card();
Card(int suit, int face);
virtual ~Card();
std::string toString();
};
#endif /* CARD_H_ */
=============================Card.cpp-=======================
/*
* Card.cpp
*
* Created on: Apr 24, 2014
*/
#include "Card.h"
const std::string Card::faces[] = {"Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"Jack", "Queen", "King"};
const std::string Card::suits[] = {"Spade", "Heart", "Diamond", "Club"};
Card::Card(){
face = 0;
suit = 0;
}
Card::Card(int suit, int face) {
this->face = face;
this->suit = suit;
}
Card::~Card() {
// TODO Auto-generated destructor stub
}
/*
* String representation of the Card
*/
std::string Card::toString(){
return Card::faces[face] + " of " + Card::suits[suit];
}
==========================DeckOfCard.h================================
/*
* DeckOfCards.h
*
* Created on: Apr 24, 2014
*/
#ifndef DECKOFCARDS_H_
#define DECKOFCARDS_H_
#include "Card.h"
/*
*
*/
class DeckOfCards {
private:
Card deck[52];
int currentCard;
public:
DeckOfCards();
void shuffleCards();
Card dealCard();
bool moreCards();
};
#endif /* DECKOFCARDS_H_ */
==================================DeckOfCards.cpp==============================
/*
* DeckOfCards.cpp
*
* Created on: Apr 24, 2014
*/
#include <stdlib.h>
#include "DeckOfCards.h"
/**
* Default Constructor for DeckOfCards. Initializes all cards
*/
DeckOfCards::DeckOfCards() {
for( int i=0; i<4; i++)
for(int j=0; j<13; j++)
deck[i*13+j] = Card(i, j);
currentCard = 0;
}
/**
* Shuffles cards in the deck;
*/
void DeckOfCards::shuffleCards(){
for(int i=0; i<52; i++){
int rnd = rand()%52;
Card temp = deck[i];
deck[i] = deck[rnd];
deck[rnd] = temp;
}
}
Card DeckOfCards::dealCard() {
return deck[currentCard++];
}
bool DeckOfCards::moreCards(){
return currentCard != 52;
}
================================Driver.cpp==========================
//============================================================================
// Name : Driver.cpp
// Author :
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <iomanip>
#include "card.h"
#include "DeckOfCards.h"
using namespace std;
int main() {
DeckOfCards doc;
doc.shuffleCards();
int j=0;
while (doc.moreCards()) {
Card c = doc.dealCard();
cout << setw(20) << c.toString();
if (++j % 4 == 0) {
j=0;
cout << endl;
}
}
}