This article mainly introduces the method of PHP to realize the Golden Flower game competition, the example analyses the realization principle and the correlation algorithm skill of the Golden Flower game, has certain reference value, the need friend can refer to the next
The example of this paper is about how PHP implements the game of Golden Flower games. The specific analysis is as follows:
The program is inseparable from the algorithm, discussed in the previous path-finding algorithm. 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?
Fried Golden Flower Two decks of comparison rules will not say, note that is the time: Jqk < A23 < QKA
Idea: Fried Golden Flower
1. Randomly generate two cards, each deck structure is
The code is as follows:
Array (' Spade ', ' K '), Array (' Club ', ' 6 '), array (' Spade ', ' J '), )
The code is as follows:
Array (' Spade ', ' K '), Array (' Club ', ' 6 '), array (' Spade ', ' J '), )
2. Calculate the score of each deck: the original size of each deck (i.e. excluding 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 class Playcards {public $suits = array (' Spade ', ' heart ', ' Diamond ', ' Club '); Public $figures = Array (' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', ' 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 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; }//complement the leading 0 for ($i = 0; $i < 3; $i + +) {$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', Str_pad_lef T); } 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*100000 if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2]) {$score + = 60*100000 ; }//Jinhua 30*100000 if ($suit [0] = = $suit [1] && $suit [0] = = $suit [2]) {$score + = 30*100 000; }//CIS 20*100000 if ($figure [0] = = $figure [1]+1 && $figure [1] = = $figure [2]+1 | | implode ($figure) = = ' 1403") {$score + = 20*100000; }//Pair 10*100000 if ($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), ' <br/> '; Echo ' Card2 is ', Printcard ($card 2), ' <br/> '; $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 code is as follows:
<?php class Playcards {public $suits = array (' Spade ', ' heart ', ' Diamond ', ' Club '); Public $figures = Array (' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', ' 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 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; }//complement the leading 0 for ($i = 0; $i < 3; $i + +) {$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', Str_pad_lef T); } 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*100000 if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2]) {$score + = 60*100000 ; }//Jinhua 30*100000 if ($suit [0] = = $suit [1] && $suit [0] = = $suit [2]) {$score + = 30*100 000; }//CIS 20*100000 if ($figure [0] = = $figure [1]+1 && $figure [1] = = $figure [2]+1 | | implode ($figure) = = ' 1403") {$score + = 20*100000; }//Pair 10*100000 if ($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), ' <br/> '; Echo ' Card2 is ', Printcard ($card 2), ' <br/> '; $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, ', '). ') '; }
Summary : The above is the entire content of this article, I hope to be able to help you learn.