This article mainly introduces the method of realizing the big and small competitions of the Zhajinhua game with PHP and analyzes the Zhajinhua game with examples. The implementation principles and related algorithm techniques of the game have certain reference value. Friends in need can refer to it
The example in this article describes the method of realizing the large and small competitions of the Jinhua game in PHP. Share it with everyone for your reference. The specific analysis is as follows:
Programs are inseparable from algorithms. The pathfinding algorithm was discussed earlier. However, in the example diagram at that time, the optional path was the only one. When we choose an algorithm, we mean to choose this unique path. How to choose it?
I still remember that when I was in junior high school, I would often hide on the side of the road after school in the afternoon to make gold flowers and gamble* money. It seemed that I was addicted to it. Now during the Chinese New Year, we often make gold flowers and gamble* money together, but my luck is not good. I lose every time.
The sun is shining brightly today. I just went out to play during Qingming Festival, so I didn’t go anywhere today. When I had nothing to do, I thought about how to use a program to compare the sizes of two cards in Golden Flower. Now that I have implemented it, some methods are quite important, so I wrote them down.
Okay, no more nonsense.
I won’t go into the rules for comparing the two decks of cards in Zhajinhua. Please indicate when it is a straight: JQK < A23 < QKA
Idea: tie golden flowers
1. Randomly generate two decks of cards, and the structure of each deck is
The code is as follows:
The code is as follows:
2. Calculate the score of each deck of cards: each deck of cards has an original size (that is, excluding pairs, straights, golden flowers, straight golds, and bobbins), and then
The score of each card is a 2-digit number. If there are less than 2 digits, leading 0s are added, for example, 'A': 14, '10': 10, '2': '02', 'k': 13, '7': 07
Sort the 3 cards according to the number of points (from large to small) to form a 6-digit number. For example 'A27': 140702, '829': 090802, 'JK8': 131108, '2A10': 141002
Exception, for pairs, put the digits of the pair in the first two digits (you will see why we do this later). For example '779': 070709, '7A7': 070714, 'A33': 030314
The current score is a 6-digit number, and the pair is set to an original value plus 10*100000, which is now a 7-digit number. For example '779': 1070709, '7A7': 1070714, 'A33': 1030314
For a straight, add 20*100000 to the result. For example '345': 2050403, 'QKA': 2141312, '23A': 2140302
For Golden Flower, add 30*100000 to the result. For example 'Spade K,Spade 6,Spade J': 3131106
Because the straight gold is actually the sum of the golden flower and the straight child, so the straight gold should be 50*10000. For example 'Spade 7,Spade 6,Spade 8': 5080706
For the bobbin, add 60*100000 to the result. For example '666': 6060606, 'JJJ': 6111111
3. Compare the sizes of the two cards (use the calculated scores to compare)
It’s that simple! !
The code is as follows (PHP)
The code is as follows:
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,',').')';
}
代码如下:
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,',').')';
}
希望本文所述对大家的php程序设计有所帮助。