Array of pointers and shuffle and licensing of poker

Source: Internet
Author: User
Tags shuffle

date:1.2

Use Srand () before Rand ()?

Use: Different random numbers per generation

Not used: The same random number generated each time

  1. #include "stdafx.h"
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #define SUITS 4
  5. #define FACES
  6. #define CARDS
  7. void Shuffle (unsigned int wdeck[][faces]) // Shuffle Deck represents a Deck of Cards
  8. {
  9. For (size_t card = 1; card <= CARDS; card++)
  10. {
  11. size_t Row;
  12. size_t column;
  13. Do {
  14. row = rand ()% suits;
  15. column = rand ()% FACES;
  16. } while (wdeck[row][column]! = 0);
  17. Wdeck[row][column] = card;
  18. }
  19. }
  20. void deal (unsigned int wdeck[][faces], const char *wface[], const char *wsuit[])
  21. {
  22. For (size_t card = 1; card < CARDS; card++)
  23. {
  24. For (size_t row = 0; row < suits; row++)
  25. {
  26. For (size_t column = 0; column < FACES; column++)
  27. {
  28. if (wdeck[row][column] = = Card) {
  29. printf ("%5s of%-8s%c", Wface[column], Wsuit[row], card% 2 = = 0?  ' \ n ' : ' \ t ');
  30. }
  31. }
  32. }
  33. }
  34. }
  35. int Main ()
  36. {
  37. unsigned int deck[suits][faces];
  38. memset (deck, 0, sizeof(deck));
  39. Srand (Time (NULL));
  40. Shuffle (deck); // Shuffle   
  41. const char *suit[suits] = { " Red Peach ", " square " ," plum "," Span style= "font-family: Arial" > spades " };   
  42.      const   char  *face[faces] = {   "Ace" , "Deuce" ," three " ," Four " ," Five " , "Six"  , "Seven"  , "Eight" ,  < Span style= "COLOR: #5c5c5c" >
  43. "Nine","Ten","Jack","Queen","King" };
  44. deal (deck, face, suit); // Licensing   
  45. GetChar ();
  46. return 0;
  47. }

The algorithm is defective (deal () function if there is a wdeck[row][column] = = Card loop will continue), and then improved.

Array of pointers and shuffle and licensing of poker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.