Skip to content

Instantly share code, notes, and snippets.

@40
Created October 18, 2012 06:20
Show Gist options
  • Select an option

  • Save 40/3910154 to your computer and use it in GitHub Desktop.

Select an option

Save 40/3910154 to your computer and use it in GitHub Desktop.
Card Deck
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
typedef struct {
char * card_face;
int card_rank;
char * card_suit;
} _card;
_card cards[] = { { "king", 12, "hearts" }, { "queen", 11, "hearts" }, { "jack", 10, "hearts" },
{ "ten", 9, "hearts" }, { "nine", 8, "hearts" }, { "eight", 7, "hearts" },
{ "seven", 6, "hearts" }, { "six", 5, "hearts" }, { "five", 4, "hearts" },
{ "four", 3, "hearts" }, { "three", 2, "hearts" }, { "two", 1, "hearts" },
{ "ace", 13, "hearts" }, { "king", 12, "clubs" }, { "queen", 11, "clubs" },
{ "jack", 10, "clubs" }, { "ten", 9, "clubs" }, { "nine", 8, "clubs" },
{ "eight", 7, "clubs" }, { "seven", 6, "clubs" }, { "six", 5, "clubs" },
{ "five", 4, "clubs" }, { "four", 3, "clubs" }, { "three", 2, "clubs" },
{ "two", 1, "clubs" }, { "ace", 13, "clubs" }, { "king", 12, "diamonds" },
{ "queen", 11, "diamonds" }, { "jack", 10, "diamonds" }, { "ten", 9, "diamonds" },
{ "nine", 8, "diamonds" }, { "eight", 7, "diamonds" }, { "seven", 6, "diamonds" },
{ "six", 5, "diamonds" }, { "five", 4, "diamonds" }, { "four", 3, "diamonds" },
{ "three", 2, "diamonds" }, { "two", 1, "diamonds" }, { "ace", 13, "diamonds" },
{ "king", 12, "spades" }, { "queen", 11, "spades" }, { "jack", 10, "spades" },
{ "ten", 9, "spades" }, { "nine", 8, "spades" }, { "eight", 7, "spades" },
{ "seven", 6, "spades" }, { "six", 5, "spades" }, { "five", 4, "spades" },
{ "four", 3, "spades" }, { "three", 2, "spades" }, { "two", 1, "spades" },
{ "ace", 13, "spades" }
};
class card {
private:
string card_face;
int card_rank;
string card_suit;
public:
card(card& copy) : card_face(copy.card_face.c_str()), card_suit(copy.card_suit.c_str()), card_rank(copy.card_rank) { };
card(char * face, int rank, char * suit) : card_face(face), card_rank(rank), card_suit(suit) { };
void set_face(const char * face) { card_face = face; };
void set_suit(const char * suit) { card_suit = suit; };
void set_rank(int rank) { card_rank = rank; };
string& get_face() { return card_face; };
string& get_suit() { return card_suit; };
int get_rank() { return card_rank; };
};
class card_deck {
private:
vector<card *> deck_of_cards;
public:
card_deck() {
int card_index;
srand((unsigned int )time(NULL));
for ( card_index = 0; card_index < 52; card_index++ ) {
card * this_card = new card(cards[card_index].card_face, cards[card_index].card_rank, cards[card_index].card_suit);
deck_of_cards.push_back(this_card);
}
}
void shuffle() {
int shuffle_index;
int shuffle_times = 1000 + rand()%1000;
for ( shuffle_index = 0; shuffle_index < shuffle_times; shuffle_index++ ) {
int card_no_1 = rand() % 52;
int card_no_2 = rand() % 52;
card temporary_card(*deck_of_cards[card_no_1]);
deck_of_cards[card_no_1]->set_face(deck_of_cards[card_no_2]->get_face().c_str());
deck_of_cards[card_no_1]->set_suit(deck_of_cards[card_no_2]->get_suit().c_str());
deck_of_cards[card_no_1]->set_rank(deck_of_cards[card_no_2]->get_rank());
deck_of_cards[card_no_2]->set_face(temporary_card.get_face().c_str());
deck_of_cards[card_no_2]->set_suit(temporary_card.get_suit().c_str());
deck_of_cards[card_no_2]->set_rank(temporary_card.get_rank());
}
}
card* deal() {
card * temporary_card = new card(*deck_of_cards[0]);
deck_of_cards.erase(deck_of_cards.begin());
deck_of_cards.push_back(temporary_card);
return temporary_card;
}
};
class hand {
private:
vector<card *> cards_in_hand;
public:
void dealt(card* this_card) { cards_in_hand.push_back(this_card); };
card * card_in_hand(int index) { return cards_in_hand[index]; };
};
int main() {
int deal_index;
card_deck deck_of_cards;
deck_of_cards.shuffle();
// -----------------------------------------------------------------
hand player1;
hand player2;
for ( deal_index = 0; deal_index < 7; deal_index++ ) {
player1.dealt(deck_of_cards.deal());
player2.dealt(deck_of_cards.deal());
}
cout << "Player 1 " << "Player 2" << endl;
cout << "--------------------- " << "---------------------" << endl;
for ( deal_index = 0; deal_index < 7; deal_index++ ) {
char print_line[70];
card * player1_card = player1.card_in_hand(deal_index);
card * player2_card = player2.card_in_hand(deal_index);
sprintf(print_line, "%-6s of %-20s", player1_card->get_face().c_str(), player1_card->get_suit().c_str());
sprintf(&print_line[30], "%-6s of %-10s", player2_card->get_face().c_str(), player2_card->get_suit().c_str());
cout << print_line << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment