1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include "Card.hpp"
#include "DeckOfCards.hpp"
int main() {
DeckOfCards testDeck;
//show deck before shuffle
std::cout << "Deck of cards" << std::endl;
for (int i = 0; i < 52; i++) {
std::string display = testDeck.showCard(i).printCard();
std::cout << display << std::endl;
}
//shuffle and print deck
std::cout << "\nDeck of Cards after shuffled" << std::endl;
testDeck.shuffle();
for (int i = 0; i < 52; i++) {
std::string display = testDeck.showCard(i).printCard();
std::cout << display << std::endl;
}
std::cout << "Number of cards available is: " << testDeck.sizeOfDeck() << std::endl;
return 0;
}
#include "Card.hpp"
#include <iostream>
#include <string>
Card::Card( int face_val, int suit_val ){
suit = suit_val;
face = face_val;
}
std::string Card::faces[13] = {"Ace","Two","Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
std::string Card::suits[4] = {"Hearts", "Clubs", "Diamonds", "Spades" };
std::string Card::getFace(){
return faces[face];
}
std::string Card::getSuit(){
return suits[suit];
}
std::string Card::printCard(){
std::string faceAndSuit = getFace() + " of " + getSuit();
return faceAndSuit;
}
#ifndef Card_hpp
#define Card_hpp
#include <string>
class Card{
public:
Card( int suit_val, int face_val );
static std::string faces[13];
static std::string suits[4];
std::string getSuit();
std::string getFace();
std::string printCard();
private:
int face;
int suit;
};
#endif /* Card_hpp */
#include "DeckOfCards.hpp"
#include <ctime>
#include <cstdlib>
DeckOfCards::DeckOfCards(){
for (int suit = 0; suit < 4; suit++){
for (int face = 0; face < 13; face++) {
Card cards(face, suit);
deck.push_back(cards);
}
}
}
Card DeckOfCards::showCard(int i){
Card topCard = deck[i];
return topCard;
};
void DeckOfCards::shuffle(){
srand(static_cast<unsigned int>(time(NULL)));
for (int i = 0; i < 52; i++) {
int shuffle = ( rand() % 52 );
Card c1 = deck[i];
Card c2 = deck[shuffle];
Card temp = c1;
deck[shuffle]= temp;
deck[i] = c2;
}
}
bool DeckOfCards::moreCards(){
if (deck.size() > 0) {
return true;
}
else
return false;
}
unsigned long int DeckOfCards::sizeOfDeck(){
return deck.size();
}
#ifndef DeckOfCards_hpp
#define DeckOfCards_hpp
#include <vector>
#include "Card.hpp"
class DeckOfCards{
public:
DeckOfCards();
void shuffle();
void dealCard();
Card showCard(int i);
bool moreCards();
unsigned long int sizeOfDeck();
private:
std::vector<Card> deck;
int currentCard;
};
#endif /* DeckOfCards_hpp */
|