Jinhua Bar Game PHP implementation code size game

Source: Internet
Author: User
Tags shuffle

Jinhua Bar Game PHP implementation code size game

The program is inseparable from the algorithm, in the previous blog, in fact, we have discussed the algorithm of finding the path. However, in the example diagram at that time, the optional path is unique. We choose an algorithm, that is, to choose the only path, how to choose?

Also remember that when the middle school often in the afternoon after school to hide in the roadside Jinhua bar to gamble * Money, seemingly also addicted, and now the new year time also often together golden Flower bet * money, but luck is not good, every time is lost ah.

Today is sunny, because the tomb-sweeping day went out to play, so today did not go. Idle nothing to think of the next how to use the program to achieve the size of two cards in the comparison, now it is realized, some methods are very important, so write down.

All right, no more nonsense.

Jinhua Bar Two decks of comparison rules will not say, note that is the time: Jqk < A23 < QKA

Idea: Jinhua Bar (http://www.a8u.net/)

1 "Randomly generated two cards, each deck structure is

[Php]view plaincopyprint?

    1. Array (
    2. Array (' Spade ',' K '),
    3. Array (' Club ',' 6 '),
    4. Array (' Spade ',' J '),
    5. )

Array ('    Spade ', ' K '), Array ('    Club ', ' 6 '),    array (' Spade ', ' J '),)

2 "Calculate the score per deck: Each deck has an original size (i.e. excludes pairs, straights, golden flowers, shun gold, the size of the package), and then

The score for each card is a 2-digit number, less than 2-bit of the complement leading 0, for example ' A ': ': ', ' 2 ': ' In ', ' K ': 13, ' 7 ': 07

The 3 cards are sorted by the number of points (from large to small) to a 6-digit size. For example ' A27 ': 140702, ' 829 ': 090802, ' JK8 ': 131108, ' 2a10 ': 141002

Exception, for the pair to put the number of bits in the first two bits (see why this is done later). For example ' 779 ': 070709, ' 7A7 ': 070714, ' A33 ': 030314

Now the score is a 6-digit number, set the pair to a primitive value plus a value of 10*100000, which is now a 7-digit number. For example ' 779 ': 1070709, ' 7A7 ': 1070714, ' A33 ': 1030314

For CIS, add 20*100000 to the result. For example ' 345 ': 2050403, ' QKA ': 2141312, ' 23A ': 2140302

For Golden Flower, the result is added 30*100000. For example ' Spade k,spade 6,spade J ': 3131106

Because the golden time is actually the flower and the son's and, so Shun Jin should be 50*10000. For example ' Spade 7,spade 6,spade 8 ': 5080706

For the package, add the result to 60*100000. For example ' 666 ': 6060606, ' jjj ': 6111111

3 "Compare the size of the two cards (compared with the calculated score)

It's so simple!!

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 ' , ' Ten ' , ' 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 3 cards
  20. return Array(array_pop($this->cards), Array_pop ($this->cards), Array_pop ($this->cards));
  21. }
  22. Public function comparecards ($card 1,$card 2)
  23. {
  24. $score 1 = $this ->ownscore ($card 1);
  25. $score 2 = $this ->ownscore ($card 2);
  26. if ($score 1 > $score 2) return 1;
  27. ElseIf ($score 1 < $score 2) 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. //complement 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. //special treatment for pairs
  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. //Cheese 60*100000
  50. if ($figure[0] = = $figure[1] && $figure [0] = = $figure[2]) {
  51. $score + = 60*100000;
  52. }
  53. //Jinhua 30*100000
  54. if ($suit[0] = = $suit[1] && $suit[0] = = $suit[2]) {
  55. $score + = 30*100000;
  56. }
  57. //Shun Zi 20*100000
  58. if ($figure[0] = = $figure[1]+1 && $figure[1] = = $figure[2]+1 | | implode ($figure) = =' 140302 ') {
  59. $score + = 20*100000;
  60. }
  61. //Pair 10*100000
  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. $card 1 = $playCard ->getcard ();
  71. $card 2 = $playCard ->getcard ();
  72. $result = $playCard ->comparecards ($card 1,$card 2);
  73. Echo ' card1 is ', Printcard ($card 1),'
    ';
  74. Echo ' Card2 is ', Printcard ($card 2),'
    ';
  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 3 Card return Array (Array_pop ($this->cards), Array_pop ($ This->cards), Array_pop ($this->cards));} Public Function Comparecards ($card 1, $card 2) {$score 1 = $this->ownscore ($card 1); $score 2 = $this->ownscore ($card 2 if ($score 1 > $score 2) return 1;elseif ($score 1 < $score 2) 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;} Padded leading 0for ($i = 0; $i < 3; $i + +) {$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', str_pad_left);} Rsort ($figure);//For a pair do special handling if ($figure [1] = = $figure [2]) {$temp = $figure [0]; $figure [0] = $figure [2]; $figure [2] = $temp;} $score = $figure [0]. $figure [1]. $figure [2];//Bobbin 60*100000if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2] ) {$score + = 60*100000;} Golden Flower 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) = = ' 140302 ') {$score + = 20*100000;} Pair 10*100000if ($figure [0] = = $figure [1] && $figure [1]! = $figure [2]) {$score + = 10*100000;} return $score;}} Test$playcard = new Playcards (); $card 1 = $playCard->getcard (); $card 2 = $playCard->getcard (); $result = $ Playcard->comparecards ($card 1, $card 2); Echo ' Card1 is ', Printcard ($card 1), '
'; Echo ' Card2 is ', Printcard ($card 2), '
'; $str = ' card1 equit card2 '; if ($result = = 1) $str = ' card1 is larger than Card2 '; ElseIf ($result = =-1) $str = ' Card1 are smaller than card2 '; Echo $str; function Printcard ($card) {$str = ' ('; foreach ($card as $v) {$str. = $v [0]. $v [1]. ', ';} Return trim ($str, ', '). ') ';}

The above describes the Jinhua Bar game PHP Implementation of the size of the code game, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.