Game 7 Lessons-4th-Introduction of poker Licensing

Source: Internet
Author: User
Tags shuffle

First three sections:

 

Section 1st-game introduction and basic algorithm http://student.csdn.net/space.php? Uid = 112600 & Do = Blog & id = 34066 section 2nd-24 point computing processing http://student.csdn.net/space.php? Uid = 112600 & Do = Blog & id = 34327 section 3rd-formatted Result Expression http://student.csdn.net/space.php? Uid = 112600 & Do = Blog & id = 35873

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Section 4th-number of licensed cards introduced

In the 24 o'clock game, poker is only used to provide numerical items, where the colors are not used, and the little devil does not need them. The value range is 1 ~ Within the range of 13.

We use an int array to express 52 cards. The internal storage value is 0 ~ 51. The formula for converting to a card value is: N % 13 + 1. % Is the remainder operator.

For example, 0 to> 0% 13 + 1 = 1. Therefore, in the program, 0 indicates the card with a card value of 1. Divide 0 by 13, and the remainder is 0. This is elementary arithmetic.

Another example is 2-> 2% 13 + 1 = 3. Therefore, 2 indicates a card with a card value of 3.

Another example is 15-> 15% 13 + 1 = 3. 15 also indicates that the card value is 3. (The color is different. The color can be represented by N/13, but it is meaningless here)

 

With this knowledge, you can write poker classes and create poker. h and poer. cpp to join the project.

Poker. h

Code:
  1. # Ifndef poker_h_included
  2. # Define poker_h_included
  3. Struct poker
  4. {
  5. Public:
  6. Poker ();
  7. Void shuffle (); // shuffling
  8. Bool deal (INT card [4]); // licensed. If there are not enough four, false is returned.
  9. PRIVATE:
  10. Int _ Cards [52];
  11. Int _ index;
  12. };
  13.  
  14. # Endif // poker_h_included

_ Cards [52] is used to store 0 ~ The value of 51 indicates playing cards. _ Index is required for licensing, indicating the number of cards currently sent.

Two major operations: One shuffling and one licensing.

Poker. cpp

Code:
  1. # Include "poker. H"
  2.  
  3. # Include <cstdlib>
  4. # Include <ctime>
  5. # Include <iostream>
  6. Using namespace STD;
  7. Poker: Poker ()
  8. : _ Index (0)
  9. {
  10. For (INT I = 0; I <52; _ Cards [I] = I, ++ I );
  11. }
  12. Void poker: shuffle () // shuffling
  13. {
  14. _ Index = 0;
  15. Srand (time (0 ));
  16.  
  17. # Define simple_swap_int (a, B) do {int T = A; A = B; B = T;} while (0)
  18. For (INT I = 52, c = 1; I> 0; -- I, ++ C)
  19. {
  20. Int rI = rand () % I;
  21. Simple_swap_int (_ Cards [52-c], _ Cards [ri]);
  22. }
  23. }
  24. Bool poker: Deal (INT card [4]) // Licensing
  25. {
  26. If (_ index + 4> 52)
  27. Return false;
  28. For (INT I = 0; I <4; ++ I, ++ _ index)
  29. {
  30. Card [I] = _ Cards [_ Index];
  31. }
  32. Return true;
  33. }

In the constructor, the card value is correct to ensure that all values in _ pokers [] are correct.

The shuffling algorithm is simple. To put it simply:

Step 1: Take one of the first 51 cards at random and exchange them with the other 52nd cards.

Step 2: Randomly select one of the first 50 cards and exchange them with the other 51st cards.

Repeat, that is, from the first 1 ~ In the N card, take a random card and exchange it with the N + 1 card. Until 1st.

 

There is no Algorithm for licensing: four from the _ index. Meanwhile, _ index advances 4.

Next, let's change the main function.

Main. cpp

Code:
  1. # Include <iostream>
  2. # Include <sstream> // For stringstream
  3.  
  4. # Include "calc_24.h"
  5. # Include "exp_getter.h"
  6.  
  7. # Include "poker. H"
  8. Using namespace STD;
  9. Int main ()
  10. {
  11. Int num [4];
  12. Poker poker;
  13. Poker. Shuffle ();
  14. While (poker. Deal (Num ))
  15. {
  16. Cout <"/n current card :";
  17. For (INT I = 0; I <4; ++ I)
  18. {
  19. Num [I] = num [I] % 13 + 1;
  20. Cout <num [I] <",";
  21. }
  22. Cout <"Think about it! "<STD: Endl;
  23. Cout <"press any key to view the current round of answer." <STD: Endl;
  24. Cin. Get ();
  25. If (! Calc_24 (Num ))
  26. {
  27. Cout <"No answer" <Endl;
  28. }
  29. }
  30. Return 0;
  31. }

It will output four numbers first, and then wait for us to press any key to give the answer. During this period, we can think about how to calculate the four numbers into 24. We cannot accept the answers we enter (and determine whether the answer is correct), nor provide human-machine confrontation ...... That is the content of the last two classes.

In any case, it looks very intelligent. We can use it to review and exercise the four arithmetic functions learned in primary school ...... Well, I called my daughter and asked her questions.

This is an example of running output:

 

Current card: 12, 6, 1, 13. Please think!

Press any key to view the current round of answers.


(13-1) x 12/6


Current card number: 5, 3, 9, 2. Please think!

By dutyItalianKey to view the current round of answer.


(5*9 + 3)/2


Current card: 8, 7, 8, 13. Please think!

By dutyItalianKey to view the current round of answer.

-------------------------------------------------------------------------------

If you want to communicate with me, please click the following link to become my friend: http://student.csdn.net/invite.php? U= 112600 & C = f635b3cf130f350c

 

 

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.