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
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
using namespace std;
//Global constants
const int NUM_CARDS = 52;
const int NUM_PLAYERS=4;
const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
//Create Function prototypes for card loading and shuffling deck of
void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(vector<int>);
//Function for dealing cards to players
void deal(vector<vector<int>>&hands, vector<int>&deck);
void showHands(const vector<vector<int>>&hands);
//Function for sorting cards
void sort(vector<int>&);
void swap(vector<int> &deck, int first, int second);
int main() {
//Declare vector for the number of cards
vector<int>deck;
cout << "The deck, unwrapped: " << endl;
unwrap(deck);
printCards(deck);
cout << " The deck after shuffling: " << endl;
shuffle(deck);
printCards(deck);
cout << " The deck after sorting: " << endl;
sort(deck);
printCards(deck);
//Declare 2d vector for dealing the cards to each player
vector<vector<int>> players(NUM_PLAYERS);
cout << " Before cards have been dealt " << endl;
showHands(players);
cout << " After the cards have been dealt "<<endl;
deal(players, deck);
showHands(players);
return 0;
}
void swap(vector<int> &deck, int first, int second)
{
int temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
//Function definitions that load cards and randomly shuffles them
void unwrap(vector<int> &deck)
{
//Load vector with ints from 0 to 51
deck.clear(); // ensure that it starts emtpy
for (int i = 0; i < NUM_CARDS; i++)
{
deck.push_back(i);
}
}
// Randomize the cards in the deck
void shuffle(vector<int> &deck)
{
random_shuffle(deck.begin(), deck.end());
}
// Print the card's name to cout
void printCard(int card)
{
cout << RANKS[card % 13] << " of " << SUITS[card / 13];
}
void printCards(vector<int> deck)
{
for(size_t j=0; j<deck.size(); j++)
{
if (j%8==0)
cout << endl;
printCard(deck[j]);
cout << ", ";
}
cout << endl << endl;
}
// deal one card to each player in order, repeat until the entire deck is dealt
void deal(vector<vector<int>>& players, vector<int>& deck)
{
// clear the players' hands
for (size_t i = 0; i < players.size(); ++i) {
players[i].clear();
}
// Deal the cards from deck to the hands.
for (size_t i = 0; i < deck.size(); ++i) {
players[i%players.size()].push_back(deck[i]);
}
//clear deck
deck.clear();
}
//Show hand after cards have been randomly dealt to each player
void showHands(const vector<vector<int>>& players)
{
for (size_t i = 0; i < players.size(); ++i) {
cout << "PLAYER " << i + 1 << ":\n";
cout << "------" <<endl;
printCards(players[i]);
}
}
void sort(vector<int> &deck)
{
int firstCard, card;
for(firstCard = 0; firstCard<NUM_CARDS-1; firstCard++)
{
for(card = firstCard+1; card<NUM_CARDS; card++)
{
if( deck[card] < deck[firstCard] )
{
swap(deck, card, firstCard);
}
}
}
}
|