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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
*******************************************************************************
// cardplay-1/main.cpp -- Very simple program to deal cards.
// Summary: Reads a number and then "deals" that many cards.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
#include "card.h"
#include "deck.h"
//================================================== main
int main()
{
int numOfCards;
Deck deck;
while (cin >> numOfCards ) // Input number for how many cards to deal.
{
deck.shuffle();
cout << "";
for (int cardNum=0; cardNum<numOfCards; cardNum++)
{
Card c = deck.dealOneCard();
string suit = c.getSuit();
string face = c.getFace();
cout << face << suit << " ";
}
cout << endl;
}
return 0();
}//end main
*****************************************************************************
// cardplay-1/deck.cpp
#include <cassert>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <mt19937ar.c>
using namespace std;
#include "card.h"
#include "deck.h"
//=========================================== Constructor
Deck::Deck()
{
// Initialize the array to the ints 0-51
for (int i=0; i<52; i++)
{
_cards[i] = Card(i);
}
shuffle();
_nextCardIndex = 0; // index of next available card
}
//================================================== deal
// Action : Returns random Card.
Card Deck::dealOneCard()
{
assert(_nextCardIndex >= 0 && _nextCardIndex<52);
return _cards[_nextCardIndex++];
}
//================================================ shuffle
// Action : Shuffles Deck.
// Returns :
void Deck::shuffle()
{
init_genrand(time(NULL));
// Shuffle by exchanging each element randomly
for (int i=0; i<(52-1); i++)
{
int genrand_real1 = i + (genrand_int32() % (52-i)); //error here!!
swap(_cards[i], _cards[genrand_real1]);
}
_nextCardIndex = 0;
}
********************************************************************************
// cardplay-1/card.cpp
#include <string>
using namespace std;
#include "card.h"
//================================================= static constants
const string Card::CARD_FACES[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
const string Card::CARD_SUITS[] = {"C","D","H","S"};
//================================================= Constructor
Card::Card()
{
_card = 0;
}
//================================================= Constructor
Card::Card(int card)
{
_card = card;
}
//================================================== getFace
// Action : Returns face value of card.
// Returns : a string representing card face: "A", "2", ETC........
string Card::getFace() const
{
return CARD_FACES[_card%13];
}//end getFace
//================================================== getSuit
// Action : Returns suit of a card value.
// Returns : a string "S" (Spades), "H", (Hearts),"C" (Clubs), or "D" (Diamonds).
string Card::getSuit() const
{
return CARD_SUITS[_card/13];
}//end getSuit
*****************************************************************************
// cardplay-1/card.h
#ifndef CARD_H
#define CARD_H
class Card {
public:
Card();
Card(int card);
string getSuit() const;
string getFace() const;
private:
int _card; // range 0..51
static const string CARD_FACES[];
static const string CARD_SUITS[];
};
#endif
************************************************************************
// cardplay-1/deck.h
#ifndef DECK_H
#define DECK_H
class Deck {
public:
Deck();
Card dealOneCard();
void shuffle();
private:
Card _cards[52];
int _nextCardIndex;
};
#endif
[output][/output]
|