How to implement the size of the golden flower in PHP
In chess game, whether it is real or online, fried gold is undoubtedly one of the most popular chess game, I was very fond of deep-fried golden flower, chance coincidence behind the IT industry, words not to say, go straight to the point.
Fried Golden Flower Two decks of comparison rules will not say, note that is the time: Jqk < A23 < QKA
Idea: Fried Golden Flower
Here is a personal point of advice for everyone's reference! (There is something wrong welcome advice board Platform customization: www.yasewl.com)
1. Randomly generate two cards, each deck structure is
Copy the code code as follows:
Array
Array (' Spade ', ' K '),
Array (' Club ', ' 6 '),
Array (' Spade ', ' J '),
)
Copy the code code 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)
Copy the code code 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 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, ', '). ') ';
}
Copy the code code 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 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, ', '). ') ';
}
This article from the "Network Fox chess Source" blog, please be sure to keep this source http://13161703.blog.51cto.com/13151703/1951068
The comparison algorithm of the size of golden flower in PHP in the course of making chess platform