PHP implementation of the game of Golden Flower games of the size of the method
This article mainly introduced the PHP implementation of Golden Flower game of the size of the game, an example of the Jinhua bar game implementation of the principle and related algorithm skills, with a certain reference value, the need for friends can refer to the next
This paper introduces the method of PHP to realize the size match of Golden Flower game. Share to everyone for your reference. 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?
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
1. Randomly generate two cards, each deck structure is
The code is as follows:
Array
Array (' Spade ', ' K '),
Array (' Club ', ' 6 '),
Array (' Spade ', ' J '),
)
The code is as follows:
Array
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)
The code is as follows:
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 Cards
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 leading 0
for ($i = 0; $i < 3; $i + +) {
$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', str_pad_left);
}
Rsort ($figure);
Special handling for pairs
if ($figure [1] = = $figure [2]) {
$temp = $figure [0];
$figure [0] = $figure [2];
$figure [2] = $temp;
}
$score = $figure [0]. $figure [1]. $figure [2];
Cheese 60*100000
if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2]) {
$score + = 60*100000;
}
Golden Flower 30*100000
if ($suit [0] = = $suit [1] && $suit [0] = = $suit [2]) {
$score + = 30*100000;
}
Shun Zi 20*100000
if ($figure [0] = = $figure [1]+1 && $figure [1] = = $figure [2]+1 | | implode ($figure) = = ' 140302 ') {
$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), '
';
Echo ' Card2 is ', Printcard ($card 2), '
';
$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:
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 Cards
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 leading 0
for ($i = 0; $i < 3; $i + +) {
$figure [$i] = Str_pad ($figure [$i],2, ' 0 ', str_pad_left);
}
Rsort ($figure);
Special handling for pairs
if ($figure [1] = = $figure [2]) {
$temp = $figure [0];
$figure [0] = $figure [2];
$figure [2] = $temp;
}
$score = $figure [0]. $figure [1]. $figure [2];
Cheese 60*100000
if ($figure [0] = = $figure [1] && $figure [0] = = $figure [2]) {
$score + = 60*100000;
}
Golden Flower 30*100000
if ($suit [0] = = $suit [1] && $suit [0] = = $suit [2]) {
$score + = 30*100000;
}
Shun Zi 20*100000
if ($figure [0] = = $figure [1]+1 && $figure [1] = = $figure [2]+1 | | implode ($figure) = = ' 140302 ') {
$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), '
';
Echo ' Card2 is ', Printcard ($card 2), '
';
$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, ', '). ') ';
}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/966364.html www.bkjia.com true http://www.bkjia.com/PHPjc/966364.html techarticle PHP Implementation of the game of the size of Golden Flower games method This article mainly introduces the PHP implementation of the game of the size of golden Games, examples of the implementation of Jinhua bar game theory and related calculation ...