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:
- # Ifndef poker_h_included
- # Define poker_h_included
- Struct poker
- {
- Public:
- Poker ();
- Void shuffle (); // shuffling
- Bool deal (INT card [4]); // licensed. If there are not enough four, false is returned.
- PRIVATE:
- Int _ Cards [52];
- Int _ index;
- };
-
- # 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:
- # Include "poker. H"
-
- # Include <cstdlib>
- # Include <ctime>
- # Include <iostream>
- Using namespace STD;
- Poker: Poker ()
- : _ Index (0)
- {
- For (INT I = 0; I <52; _ Cards [I] = I, ++ I );
- }
- Void poker: shuffle () // shuffling
- {
- _ Index = 0;
- Srand (time (0 ));
-
- # Define simple_swap_int (a, B) do {int T = A; A = B; B = T;} while (0)
- For (INT I = 52, c = 1; I> 0; -- I, ++ C)
- {
- Int rI = rand () % I;
- Simple_swap_int (_ Cards [52-c], _ Cards [ri]);
- }
- }
- Bool poker: Deal (INT card [4]) // Licensing
- {
- If (_ index + 4> 52)
- Return false;
- For (INT I = 0; I <4; ++ I, ++ _ index)
- {
- Card [I] = _ Cards [_ Index];
- }
- Return true;
- }
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:
- # Include <iostream>
- # Include <sstream> // For stringstream
-
- # Include "calc_24.h"
- # Include "exp_getter.h"
-
- # Include "poker. H"
- Using namespace STD;
- Int main ()
- {
- Int num [4];
- Poker poker;
- Poker. Shuffle ();
- While (poker. Deal (Num ))
- {
- Cout <"/n current card :";
- For (INT I = 0; I <4; ++ I)
- {
- Num [I] = num [I] % 13 + 1;
- Cout <num [I] <",";
- }
- Cout <"Think about it! "<STD: Endl;
- Cout <"press any key to view the current round of answer." <STD: Endl;
- Cin. Get ();
- If (! Calc_24 (Num ))
- {
- Cout <"No answer" <Endl;
- }
- }
- Return 0;
- }
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