PHP implementation code-size competitions in zinjin games

Source: Internet
Author: User
: 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?

  1. Array (
  2. Array ('spad', 'k '),
  3. Array ('Club ', '6 '),
  4. Array ('spade ', 'J '),
  5. )

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?

  1. Class PlayCards
  2. {
  3. Public $ suits = array ('spade ', 'heart', 'diamond', 'Club ');
  4. Public $ figures = array ('2', '3', '4', '5', '6', '7', '8', '9 ', '10', 'J', 'Q', 'K', 'A ');
  5. Public $ cards = array ();
  6. Public function _ construct ()
  7. {
  8. $ Cards = array ();
  9. Foreach ($ this-> suits as $ suit ){
  10. Foreach ($ this-> figures as $ figure ){
  11. $ Cards [] = array ($ suit, $ figure );
  12. }
  13. }
  14. $ This-> cards = $ cards;
  15. }
  16. Public function getCard ()
  17. {
  18. Shuffle ($ this-> cards );
  19. // Generate three cards
  20. Return array (array_pop ($ this-> cards), array_pop ($ this-> cards), array_pop ($ this-> cards ));
  21. }
  22. Public function compareCards ($ card1, $ card2)
  23. {
  24. $ Score1 = $ this-> ownScore ($ card1 );
  25. $ Score2 = $ this-> ownScore ($ card2 );
  26. If ($ score1> $ score2) return 1;
  27. Elseif ($ score1 <$ score2) return-1;
  28. Return 0;
  29. }
  30. Private function ownScore ($ card)
  31. {
  32. $ Suit = $ figure = array ();
  33. Foreach ($ card as $ v ){
  34. $ Suit [] = $ v [0];
  35. $ Figure [] = array_search ($ v [1], $ this-> figures) + 2;
  36. }
  37. // Complete the leading 0
  38. For ($ I = 0; $ I <3; $ I ++ ){
  39. $ Figure [$ I] = str_pad ($ figure [$ I], 2, '0', STR_PAD_LEFT );
  40. }
  41. Rsort ($ figure );
  42. // Perform special processing on the child
  43. If ($ figure [1] ==$ figure [2]) {
  44. $ Temp = $ figure [0];
  45. $ Figure [0] = $ figure [2];
  46. $ Figure [2] = $ temp;
  47. }
  48. $ Score = $ figure [0]. $ figure [1]. $ figure [2];
  49. // 60x100000
  50. If ($ figure [0] === figure [1] & $ figure [0] ===$ figure [2]) {
  51. $ Score + = 60*100000;
  52. }
  53. // Calendula 30*100000
  54. If ($ suit [0] ==$ suit [1] & $ suit [0] ===$ suit [2]) {
  55. $ Score + = 30*100000;
  56. }
  57. // Shunzi 20x100000
  58. If ($ figure [0] = $ figure [1] + 1 & $ figure [1] = $ figure [2] + 1 | implode ($ figure) = '000000 '){
  59. $ Score + = 20*100000;
  60. }
  61. // 10*100000 pairs
  62. If ($ figure [0] === figure [1] & $ figure [1]! = $ Figure [2]) {
  63. $ Score + = 10*100000;
  64. }
  65. Return $ score;
  66. }
  67. }
  68. // Test
  69. $ PlayCard = new PlayCards ();
  70. $ Card1 = $ playCard-> getCard ();
  71. $ Card2 = $ playCard-> getCard ();
  72. $ Result = $ playCard-> compareCards ($ card1, $ card2 );
  73. Echo 'card1 is ', printCard ($ card1 ),'
    ';
  74. Echo 'card2 is ', printCard ($ card2 ),'
    ';
  75. $ Str = 'card1 equit card2 ';
  76. If ($ result = 1) $ str = 'card1 is larger than card2 ';
  77. Elseif ($ result =-1) $ str = 'card1 is smaller than card2 ';
  78. Echo $ str;
  79. Function printCard ($ card)
  80. {
  81. $ Str = '(';
  82. Foreach ($ card as $ v ){
  83. $ Str. = $ v [0]. $ v [1]. ',';
  84. }
  85. Return trim ($ str ,',').')';
  86. }

 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.

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.