Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================
- Name : deck_of_cards
- Description :
- Write a program to assign values to deck of cards,
- shuffle the cards and pick five cards randomly.
- Since no two cards picked from a deck cards can repeat,
- in your programs output also no two cards can repeat
- ============================================================
- */
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- using namespace std;
- struct Card {
- unsigned char pips;
- unsigned char suit;
- };
- enum Suit {
- CLUBS = 1,
- SPADES,
- DIAMONDS,
- HEARTS,
- };
- Card carray[52]; // An array of cards,
- void assign_values(int index, unsigned char pips, unsigned char suit);
- void print_values(Card c);
- void shuffle_deck(int shuffleCount);
- int main() {
- unsigned char pips;
- unsigned char suit;
- int i, j;
- unsigned seed = time(0);
- srand(seed);
- int val[5] = { -1 };
- for (i = 0; i < 52; i++) { // There are 52 cards in a deck
- pips = i % 13 + 1; // There are 13 pips
- // And there are 4 suits
- if (i < 13)
- suit = CLUBS;
- else
- if (i < 26)
- suit = SPADES;
- else
- if (i < 39)
- suit = DIAMONDS;
- else
- if (i < 52)
- suit = HEARTS;
- assign_values(i, pips, suit);
- }
- for (i = 0; i < 52; i++)
- print_values(carray[i]);
- cout << endl;
- cout << "Shuffling the deck ..." << endl;
- // Number of times to shuffle the deck
- int shuffleCount = 40;
- shuffle_deck(40);
- for (i = 0; i < 52; i++)
- print_values(carray[i]);
- cout << endl << endl;
- cout << "Selecting five cards randomly from the deck ...\n";
- for (i = 0; i < 5; i++) {
- do {
- j = rand() % 52;
- } while (j == val[0] ||
- j == val[1] ||
- j == val[2] ||
- j == val[3] ||
- j == val[4]);
- val[i] = j;
- }
- for (i = 0; i < 5; i++)
- print_values(carray[val[i]]);
- cout << endl;
- return 0;
- }
- void
- assign_values(int index, unsigned char pips, unsigned char suit)
- {
- carray[index].pips = pips;
- carray[index].suit = suit;
- }
- bool CheckExisted(char pips, char suit, int maxnumber) {
- if (pips == 0 || suit == 0) return true;
- for (size_t i = 0; i < maxnumber; i++)
- {
- if (carray[i].pips == pips && carray[i].suit == suit) {
- return true;
- }
- }
- return false;
- }
- void
- shuffle_deck(int shuffleCount)
- {
- int j = 0;
- int randPips = 0;
- int randSuit = 0;
- //E thử tại đây với a xem
- for (size_t i = 0; i < shuffleCount; i++)
- {
- for (j = 0; j < 52; j++) {
- do {
- randPips = rand() % 14;
- randSuit = rand() % 5;
- if (!CheckExisted(randPips, randSuit, j)) break;
- } while (1==1);
- carray[j].pips = randPips;
- carray[j].suit = randSuit;
- }
- }
- }
- void
- print_values(Card c)
- {
- string suit_name;
- string pips_name;
- switch (c.suit) {
- case CLUBS:
- suit_name = "Clubs";
- break;
- case SPADES:
- suit_name = "Spades";
- break;
- case DIAMONDS:
- suit_name = "Diamonds";
- break;
- case HEARTS:
- suit_name = "Hearts";
- break;
- }
- switch (c.pips) {
- case 1:
- pips_name = "Ace";
- break;
- case 2:
- pips_name = "Two";
- break;
- case 3:
- pips_name = "Three";
- break;
- case 4:
- pips_name = "Four";
- break;
- case 5:
- pips_name = "Five";
- break;
- case 6:
- pips_name = "Six";
- break;
- case 7:
- pips_name = "Seven";
- break;
- case 8:
- pips_name = "Eight";
- break;
- case 9:
- pips_name = "Nine";
- break;
- case 10:
- pips_name = "Ten";
- break;
- case 11:
- pips_name = "Jack";
- break;
- case 12:
- pips_name = "Queen";
- break;
- case 13:
- pips_name = "King";
- break;
- }
- cout << "\t\t\t" << pips_name << " of " << suit_name << endl;
- return;
- }
- /*
- ./a.out
- Ace of Clubs
- Two of Clubs
- Three of Clubs
- Four of Clubs
- Five of Clubs
- Six of Clubs
- Seven of Clubs
- Eight of Clubs
- Nine of Clubs
- Ten of Clubs
- Jack of Clubs
- Queen of Clubs
- King of Clubs
- Ace of Spades
- Two of Spades
- Three of Spades
- Four of Spades
- Five of Spades
- Six of Spades
- Seven of Spades
- Eight of Spades
- Nine of Spades
- Ten of Spades
- Jack of Spades
- Queen of Spades
- King of Spades
- Ace of Diamonds
- Two of Diamonds
- Three of Diamonds
- Four of Diamonds
- Five of Diamonds
- Six of Diamonds
- Seven of Diamonds
- Eight of Diamonds
- Nine of Diamonds
- Ten of Diamonds
- Jack of Diamonds
- Queen of Diamonds
- King of Diamonds
- Ace of Hearts
- Two of Hearts
- Three of Hearts
- Four of Hearts
- Five of Hearts
- Six of Hearts
- Seven of Hearts
- Eight of Hearts
- Nine of Hearts
- Ten of Hearts
- Jack of Hearts
- Queen of Hearts
- King of Hearts
- Shuffling the deck ...
- Ace of Clubs
- Seven of Clubs
- King of Hearts
- Five of Clubs
- Eight of Spades
- Queen of Diamonds
- Two of Clubs
- Three of Diamonds
- Nine of Clubs
- King of Diamonds
- Queen of Spades
- Ace of Hearts
- Five of Diamonds
- Ace of Spades
- Ace of Diamonds
- Nine of Diamonds
- Six of Hearts
- Queen of Clubs
- Six of Spades
- Two of Spades
- Six of Diamonds
- Seven of Hearts
- Seven of Spades
- Eight of Clubs
- Jack of Clubs
- Five of Spades
- Five of Hearts
- Two of Diamonds
- Three of Clubs
- Queen of Hearts
- Ten of Spades
- King of Clubs
- Seven of Diamonds
- Jack of Hearts
- King of Spades
- Ten of Diamonds
- Jack of Diamonds
- Ten of Hearts
- Four of Clubs
- Ten of Clubs
- Two of Hearts
- Three of Spades
- Three of Hearts
- Four of Hearts
- Six of Clubs
- Jack of Spades
- Eight of Hearts
- Nine of Hearts
- Four of Spades
- Eight of Diamonds
- Four of Diamonds
- Nine of Spades
- Selecting five cards randomly from the deck ...
- King of Clubs
- Four of Clubs
- Queen of Diamonds
- Three of Spades
- Two of Hearts
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement