: This article mainly introduces the PHP implementation code of the zinjin game. if you are interested in the PHP Tutorial, please refer to it.
PHP implementation code-size competitions in zinjin games
Programs cannot do without Algorithms. in the previous blog, we have already discussed pathfinding algorithms. However, in the example at that time, the optional path is unique. We select an algorithm. that is to say, we need to select this unique path. how can we choose it?
I still remember that when I was in junior high school, I often spent money by hiding on the roadside when I was out of school in the afternoon. * It seems that I am still addicted. now I often bet on money together during the Chinese New Year, but I am not lucky, every time I lose.
Today, the sun is shining. I went out for a visit during the Tomb Sweeping Day, so I didn't go anywhere today. I thought about how to use a program to compare the sizes of the two cards in Jinhua. now I have implemented it. some methods are quite important, so I will write it down.
Okay, no nonsense.
The comparison rules of the two deck cards of zinjinhua will not be mentioned, indicating that JQK <A23 <QKA
Idea: zaginhua (http://www.a8u.net /)
1 "two cards are randomly generated, with each deck structured
[Php] view plaincopyprint?
- Array (
- Array ('spad', 'k '),
- Array ('Club ', '6 '),
- Array ('spade ', 'J '),
- )
array( array('Spade','K'), array('Club','6'), array('Spade','J'),)
2 "calculate the score of each sub-card: each sub-card has an original size (I .e. excluding the size of sub-card, sub-card, Jin Hua, Shun Jin, and sub-card), and then
Each card has A two-digit score with less than two leading zeros, for example, 'A': 14, '10': 10, '2': '02 ', 'K': 13, '7': 07
Sort the three cards by the number of points (from big to small) to a six-digit number. For example, 'a27': 140702, '2018010': 829, 'jk8': 090802, '2a10': 131108
With the exception, the number of digits of the child must be placed in the first two digits (the reason for this is later ). For example, '200': 779, '7a7 ': 070709, 'a33': 070714
The current score is a 6-digit number. The child is set to an original value plus a value of 10*100000. now it is a 7-digit number. For example, '200': 779, '7a7 ': 1070709, 'a33': 1070714
Add 20*100000 to the result .. For example, '200': 345, 'qka ': 2050403, '23a': 2141312
For Jinhua, add 30*100000 to the result. For example, 'spade K, Spade 6, Spade j': 3131106
Because Shun Jin is actually the sum of Jin Hua and Heshun Zi, So Shun Jin should be 50*10000. For example, 'spade 7, Spade 6, Spade 8': 5080706
Add 60*100000 to the result of the package. For example, '200': 666, 'jjj ': 6060606
3 "compare the two card sizes (use the calculated score for comparison)
That's easy !!
The code is as follows (PHP)
[Php] view plaincopyprint?
-
- Class PlayCards
- {
- Public $ suits = array ('spade ', 'heart', 'diamond', 'Club ');
- Public $ figures = array ('2', '3', '4', '5', '6', '7', '8', '9 ', '10', 'J', 'Q', 'K', 'A ');
- Public $ cards = array ();
- Public function _ construct ()
- {
- $ Cards = array ();
- Foreach ($ this-> suits as $ suit ){
- Foreach ($ this-> figures as $ figure ){
- $ Cards [] = array ($ suit, $ figure );
- }
- }
- $ This-> cards = $ cards;
- }
- Public function getCard ()
- {
- Shuffle ($ this-> cards );
- // Generate three cards
- Return array (array_pop ($ this-> cards), array_pop ($ this-> cards), array_pop ($ this-> cards ));
-
- }
- Public function compareCards ($ card1, $ card2)
- {
- $ Score1 = $ this-> ownScore ($ card1 );
- $ Score2 = $ this-> ownScore ($ card2 );
- If ($ score1> $ score2) return 1;
- Elseif ($ score1 <$ score2) return-1;
- Return 0;
- }
-
-
- Private function ownScore ($ card)
- {
- $ Suit = $ figure = array ();
- Foreach ($ card as $ v ){
- $ Suit [] = $ v [0];
- $ Figure [] = array_search ($ v [1], $ this-> figures) + 2;
- }
- // Complete the leading 0
- For ($ I = 0; $ I <3; $ I ++ ){
- $ Figure [$ I] = str_pad ($ figure [$ I], 2, '0', STR_PAD_LEFT );
- }
- Rsort ($ figure );
- // Perform special processing on the child
- If ($ figure [1] ==$ figure [2]) {
- $ Temp = $ figure [0];
- $ Figure [0] = $ figure [2];
- $ Figure [2] = $ temp;
- }
- $ Score = $ figure [0]. $ figure [1]. $ figure [2];
- // 60x100000
- If ($ figure [0] === figure [1] & $ figure [0] ===$ figure [2]) {
- $ Score + = 60*100000;
- }
- // Calendula 30*100000
- If ($ suit [0] ==$ suit [1] & $ suit [0] ===$ suit [2]) {
- $ Score + = 30*100000;
- }
- // Shunzi 20x100000
- If ($ figure [0] = $ figure [1] + 1 & $ figure [1] = $ figure [2] + 1 | implode ($ figure) = '000000 '){
- $ Score + = 20*100000;
- }
- // 10*100000 pairs
- If ($ figure [0] === figure [1] & $ figure [1]! = $ Figure [2]) {
-
- $ Score + = 10*100000;
- }
- Return $ score;
- }
- }
-
- // Test
- $ PlayCard = new PlayCards ();
- $ Card1 = $ playCard-> getCard ();
- $ Card2 = $ playCard-> getCard ();
- $ Result = $ playCard-> compareCards ($ card1, $ card2 );
-
- Echo 'card1 is ', printCard ($ card1 ),'
';
- Echo 'card2 is ', printCard ($ card2 ),'
';
- $ Str = 'card1 equit card2 ';
- If ($ result = 1) $ str = 'card1 is larger than card2 ';
- Elseif ($ result =-1) $ str = 'card1 is smaller than card2 ';
- Echo $ str;
-
-
- Function printCard ($ card)
- {
- $ Str = '(';
- Foreach ($ card as $ v ){
- $ Str. = $ v [0]. $ v [1]. ',';
- }
- Return trim ($ str ,',').')';
- }
Suits as $ suit) {foreach ($ this-> figures as $ figure) {$ cards [] = array ($ suit, $ figure );}} $ this-> cards = $ cards;} public function getCard () {shuffle ($ this-> cards ); // generate three cards return array (array_pop ($ this-> cards), array_pop ($ this-> cards), array_pop ($ this-> cards ));} public function compareCards ($ card1, $ card2) {$ score1 = $ this-> ownScore ($ card1); $ score2 = $ this-> ownScore ($ card2 ); if ($ score1> $ score2) return 1; elseif ($ Score1 <$ score2) return-1; return 0;} private function ownScore ($ card) {$ suit = $ figure = array (); foreach ($ card as $ v) {$ suit [] = $ v [0]; $ figure [] = array_search ($ v [1], $ this-> figures) + 2;} // complete the leading 0for ($ I = 0; $ I <3; $ I ++) {$ figure [$ I] = str_pad ($ figure [$ I], 2, '0', STR_PAD_LEFT);} rsort ($ figure ); // perform special processing on the child if ($ figure [1] =$ figure [2]) {$ temp = $ figure [0]; $ figure [0] = $ figure [2]; $ figure [2] = $ temp;} $ score = $ Figure [0]. $ figure [1]. $ figure [2]; // package 60 * 100000if ($ figure [0] = $ figure [1] & $ figure [0] = $ figure [2]) {$ score + = 60*100000 ;} // Jinhua 30 * 100000if ($ suit [0] ==$ suit [1] & $ suit [0] ==$ suit [2]) {$ score ++ = 30*100000 ;} // Shun Zi 20 * 100000if ($ figure [0] ==$ figure [1] + 1 & $ figure [1] ===$ figure [2] + 1 | implode ($ figure) = '000000') {$ score + = 20*140302 ;} // 10 x 100000if ($ figure [0] ==$ figure [1] & $ figure [1]! = $ Figure [2]) {$ score + = 10*100000;} return $ score ;}// test $ playCard = new PlayCards (); $ card1 = $ playCard-> getCard (); $ card2 = $ playCard-> getCard (); $ result = $ playCard-> compareCards ($ card1, $ card2 ); echo 'card1 is ', printCard ($ card1 ),'
'; Echo 'card2 is', printCard ($ card2 ),'
'; $ Str = 'card1 equit card2'; if ($ result = 1) $ str = 'card1 is larger than card2 '; elseif ($ result =-1) $ str = 'card1 is smaller than card2 '; echo $ str; function printCard ($ card) {$ str =' ('; foreach ($ card as $ v) {$ str. = $ v [0]. $ v [1]. ',';} return trim ($ str ,','). ')';}
The above section introduces the PHP code size competition of the zinjinhua game, including some content, and hopes to help friends who are interested in PHP tutorials.