Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. /*
  2. ============================================================
  3. Name        : deck_of_cards
  4. Description :
  5.  
  6. Write a program to assign values to deck of cards,
  7. shuffle the cards and pick five cards randomly.
  8.  
  9. Since no two cards picked from a deck cards can repeat,
  10. in your programs output also no two cards can repeat
  11. ============================================================
  12. */
  13.  
  14. #include <iostream>
  15. #include <cstdlib>
  16. #include <ctime>
  17. #include <string>
  18. using namespace std;
  19.  
  20. struct Card {
  21.     unsigned char pips;
  22.     unsigned char suit;
  23. };
  24.  
  25.  
  26. enum Suit {
  27.     CLUBS = 1,
  28.     SPADES,
  29.     DIAMONDS,
  30.     HEARTS,
  31. };
  32.  
  33.  
  34. Card  carray[52]; // An array of cards,
  35.  
  36. void assign_values(int index, unsigned char pips, unsigned char suit);
  37. void print_values(Card c);
  38. void shuffle_deck(int shuffleCount);
  39.  
  40. int main() {
  41.  
  42.     unsigned char  pips;
  43.     unsigned char  suit;
  44.     int   i, j;
  45.  
  46.     unsigned seed = time(0);
  47.     srand(seed);
  48.  
  49.     int val[5] = { -1 };
  50.  
  51.     for (i = 0; i < 52; i++) { // There are 52 cards in a deck
  52.         pips = i % 13 + 1;     // There are 13 pips
  53.                                // And there are 4 suits
  54.         if (i < 13)
  55.             suit = CLUBS;
  56.         else
  57.             if (i < 26)
  58.                 suit = SPADES;
  59.             else
  60.                 if (i < 39)
  61.                     suit = DIAMONDS;
  62.                 else
  63.                     if (i < 52)
  64.                         suit = HEARTS;
  65.  
  66.         assign_values(i, pips, suit);
  67.     }
  68.  
  69.     for (i = 0; i < 52; i++)
  70.         print_values(carray[i]);
  71.     cout << endl;
  72.  
  73.     cout << "Shuffling the deck ..." << endl;
  74.     // Number of times to shuffle the deck
  75.     int shuffleCount = 40;
  76.     shuffle_deck(40);
  77.  
  78.     for (i = 0; i < 52; i++)
  79.         print_values(carray[i]);
  80.     cout << endl << endl;
  81.  
  82.     cout << "Selecting five cards randomly from the deck ...\n";
  83.     for (i = 0; i < 5; i++) {
  84.         do {
  85.             j = rand() % 52;
  86.         } while (j == val[0] ||
  87.             j == val[1] ||
  88.             j == val[2] ||
  89.             j == val[3] ||
  90.             j == val[4]);
  91.         val[i] = j;
  92.     }
  93.  
  94.     for (i = 0; i < 5; i++)
  95.         print_values(carray[val[i]]);
  96.     cout << endl;
  97.  
  98.     return 0;
  99. }
  100.  
  101. void
  102. assign_values(int index, unsigned char pips, unsigned char suit)
  103. {
  104.     carray[index].pips = pips;
  105.     carray[index].suit = suit;
  106. }
  107.  
  108. bool CheckExisted(char pips, char suit, int maxnumber) {
  109.     if (pips == 0 || suit == 0) return true;
  110.     for (size_t i = 0; i < maxnumber; i++)
  111.     {
  112.         if (carray[i].pips == pips && carray[i].suit == suit) {
  113.             return true;
  114.         }
  115.     }
  116.     return false;
  117. }
  118.  
  119. void
  120. shuffle_deck(int shuffleCount)
  121. {
  122.     int j = 0;
  123.     int randPips = 0;
  124.     int randSuit = 0;
  125.     //E thử tại đây với a xem
  126.     for (size_t i = 0; i < shuffleCount; i++)
  127.     {
  128.         for (j = 0; j < 52; j++) {
  129.             do {
  130.                 randPips = rand() % 14;
  131.                 randSuit = rand() % 5;
  132.                 if (!CheckExisted(randPips, randSuit, j)) break;
  133.             } while (1==1);
  134.             carray[j].pips = randPips;
  135.             carray[j].suit = randSuit;
  136.         }
  137.     }
  138. }
  139.  
  140. void
  141. print_values(Card c)
  142. {
  143.  
  144.     string suit_name;
  145.     string pips_name;
  146.  
  147.     switch (c.suit) {
  148.     case CLUBS:
  149.         suit_name = "Clubs";
  150.         break;
  151.     case SPADES:
  152.         suit_name = "Spades";
  153.         break;
  154.     case DIAMONDS:
  155.         suit_name = "Diamonds";
  156.         break;
  157.     case HEARTS:
  158.         suit_name = "Hearts";
  159.         break;
  160.     }
  161.  
  162.     switch (c.pips) {
  163.     case 1:
  164.         pips_name = "Ace";
  165.         break;
  166.     case 2:
  167.         pips_name = "Two";
  168.         break;
  169.     case 3:
  170.         pips_name = "Three";
  171.         break;
  172.     case 4:
  173.         pips_name = "Four";
  174.         break;
  175.     case 5:
  176.         pips_name = "Five";
  177.         break;
  178.     case 6:
  179.         pips_name = "Six";
  180.         break;
  181.     case 7:
  182.         pips_name = "Seven";
  183.         break;
  184.     case 8:
  185.         pips_name = "Eight";
  186.         break;
  187.     case 9:
  188.         pips_name = "Nine";
  189.         break;
  190.     case 10:
  191.         pips_name = "Ten";
  192.         break;
  193.     case 11:
  194.         pips_name = "Jack";
  195.         break;
  196.     case 12:
  197.         pips_name = "Queen";
  198.         break;
  199.     case 13:
  200.         pips_name = "King";
  201.         break;
  202.     }
  203.  
  204.     cout << "\t\t\t" << pips_name << " of " << suit_name << endl;
  205.  
  206.     return;
  207. }
  208.  
  209.  
  210. /*
  211. ./a.out
  212. Ace of Clubs
  213. Two of Clubs
  214. Three of Clubs
  215. Four of Clubs
  216. Five of Clubs
  217. Six of Clubs
  218. Seven of Clubs
  219. Eight of Clubs
  220. Nine of Clubs
  221. Ten of Clubs
  222. Jack of Clubs
  223. Queen of Clubs
  224. King of Clubs
  225. Ace of Spades
  226. Two of Spades
  227. Three of Spades
  228. Four of Spades
  229. Five of Spades
  230. Six of Spades
  231. Seven of Spades
  232. Eight of Spades
  233. Nine of Spades
  234. Ten of Spades
  235. Jack of Spades
  236. Queen of Spades
  237. King of Spades
  238. Ace of Diamonds
  239. Two of Diamonds
  240. Three of Diamonds
  241. Four of Diamonds
  242. Five of Diamonds
  243. Six of Diamonds
  244. Seven of Diamonds
  245. Eight of Diamonds
  246. Nine of Diamonds
  247. Ten of Diamonds
  248. Jack of Diamonds
  249. Queen of Diamonds
  250. King of Diamonds
  251. Ace of Hearts
  252. Two of Hearts
  253. Three of Hearts
  254. Four of Hearts
  255. Five of Hearts
  256. Six of Hearts
  257. Seven of Hearts
  258. Eight of Hearts
  259. Nine of Hearts
  260. Ten of Hearts
  261. Jack of Hearts
  262. Queen of Hearts
  263. King of Hearts
  264. Shuffling the deck ...
  265. Ace of Clubs
  266. Seven of Clubs
  267. King of Hearts
  268. Five of Clubs
  269. Eight of Spades
  270. Queen of Diamonds
  271. Two of Clubs
  272. Three of Diamonds
  273. Nine of Clubs
  274. King of Diamonds
  275. Queen of Spades
  276. Ace of Hearts
  277. Five of Diamonds
  278. Ace of Spades
  279. Ace of Diamonds
  280. Nine of Diamonds
  281. Six of Hearts
  282. Queen of Clubs
  283. Six of Spades
  284. Two of Spades
  285. Six of Diamonds
  286. Seven of Hearts
  287. Seven of Spades
  288. Eight of Clubs
  289. Jack of Clubs
  290. Five of Spades
  291. Five of Hearts
  292. Two of Diamonds
  293. Three of Clubs
  294. Queen of Hearts
  295. Ten of Spades
  296. King of Clubs
  297. Seven of Diamonds
  298. Jack of Hearts
  299. King of Spades
  300. Ten of Diamonds
  301. Jack of Diamonds
  302. Ten of Hearts
  303. Four of Clubs
  304. Ten of Clubs
  305. Two of Hearts
  306. Three of Spades
  307. Three of Hearts
  308. Four of Hearts
  309. Six of Clubs
  310. Jack of Spades
  311. Eight of Hearts
  312. Nine of Hearts
  313. Four of Spades
  314. Eight of Diamonds
  315. Four of Diamonds
  316. Nine of Spades
  317.  
  318.  
  319. Selecting five cards randomly from the deck ...
  320. King of Clubs
  321. Four of Clubs
  322. Queen of Diamonds
  323. Three of Spades
  324. Two of Hearts
  325.  
  326. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement