Home > Backend Development > PHP Tutorial > PHP implements golden flower game competition

PHP implements golden flower game competition

墨辰丷
Release: 2023-03-31 20:32:02
Original
3341 people have browsed it

This article mainly introduces the method of realizing the Fried Golden Flower game competition in PHP. It analyzes the implementation principles and related algorithm techniques of the Fried Golden Flower game with examples. It has certain reference value. Friends in need can refer to it

The example in this article describes the method of implementing the Golden Flower Game Competition in PHP. 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 won’t go into the rules for comparing the two decks of Fried Golden Flowers. Just indicate when it’s a straight: JQK < A23 < QKA

Idea: Fried Golden Flowers

1. Randomly generate two cards, the structure of each deck is

The code is as follows:

array(  
    array(&#39;Spade&#39;,&#39;K&#39;),  
    array(&#39;Club&#39;,&#39;6&#39;),  
    array(&#39;Spade&#39;,&#39;J&#39;),  
)
Copy after login
Copy after login

The code is as follows:

array(  
    array(&#39;Spade&#39;,&#39;K&#39;),  
    array(&#39;Club&#39;,&#39;6&#39;),  
    array(&#39;Spade&#39;,&#39;J&#39;),  
)
Copy after login
Copy after login

2. Calculate each deck The score of the card: 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 , add leading 0 if there are less than 2 digits, for example, 'A': 14, '10': 10, '2': '02', 'k': 13, '7': 07

will be 3 The cards are sorted according to the size of the points (from large to small) and made up into a 6-digit number. For example, 'A27': 140702, '829': 090802, 'JK8': 131108, '2A10': 141002

Exception, for pairs, the number of digits of the pair should be placed in the first two digits (will be discussed later) See why this is done). 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. It 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 flowers, 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 tube, 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  
class PlayCards  
{  
    public $suits = array(&#39;Spade&#39;, &#39;Heart&#39;, &#39;Diamond&#39;, &#39;Club&#39;);  
    public $figures = array(&#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;10&#39;, &#39;J&#39;, &#39;Q&#39;, &#39;K&#39;, &#39;A&#39;);  
    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);  
        //生成3张牌  
        return array(array_pop($this->cards), array_pop($this->cards), array_pop($this->cards));     
    }  
    public function compareCards($card1,$card2)  
    {  
        $score1 = $this->ownScore($card1);  
        $score2 = $this->ownScore($card2);  
        if($score1 > $score2) return 1;  
        elseif($score1 < $score2) 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;  
        }  
        //补齐前导0  
        for($i = 0; $i < 3; $i++){  
            $figure[$i] = str_pad($figure[$i],2,&#39;0&#39;,STR_PAD_LEFT);  
        }  
        rsort($figure);  
        //对于对子做特殊处理  
        if($figure[1] == $figure[2]){  
            $temp = $figure[0];  
            $figure[0] = $figure[2];  
            $figure[2] = $temp;  
        }  
        $score = $figure[0].$figure[1].$figure[2];  
        //筒子 60*100000  
        if($figure[0] == $figure[1] && $figure[0] == $figure[2]){  
            $score += 60*100000;  
        }  
        //金花 30*100000  
        if($suit[0] == $suit[1] && $suit[0] == $suit[2]){  
            $score += 30*100000;  
        }  
        //顺子 20*100000  
        if($figure[0] == $figure[1]+1 && $figure[1] == $figure[2]+1 || implode($figure) ==&#39;140302&#39;){  
            $score += 20*100000;  
        }  
        //对子 10*100000  
        if($figure[0] == $figure[1] && $figure[1] != $figure[2]){  
  
            $score += 10*100000;  
        }  
        return $score;  
    }  
}  
  
//test  
$playCard = new PlayCards();  
$card1 = $playCard->getCard();  
$card2 = $playCard->getCard();  
$result = $playCard->compareCards($card1,$card2); 
echo &#39;card1 is &#39;,printCard($card1),&#39;<br/>&#39;;  
echo &#39;card2 is &#39;,printCard($card2),&#39;<br/>&#39;;  
$str = &#39;card1 equit card2&#39;;  
if($result == 1) $str =  &#39;card1 is larger than card2&#39;;  
elseif($result == -1) $str = &#39;card1 is smaller than card2&#39;;  
echo $str;  
function printCard($card)  
{  
    $str = &#39;(&#39;;  
    foreach($card as $v){  
        $str .= $v[0].$v[1].&#39;,&#39;;  
    }  
    return trim($str,&#39;,&#39;).&#39;)&#39;;  
}
Copy after login

The code is as follows:

<?php  
class PlayCards  
{  
    public $suits = array(&#39;Spade&#39;, &#39;Heart&#39;, &#39;Diamond&#39;, &#39;Club&#39;);  
    public $figures = array(&#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;10&#39;, &#39;J&#39;, &#39;Q&#39;, &#39;K&#39;, &#39;A&#39;);  
    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);  
        //生成3张牌  
        return array(array_pop($this->cards), array_pop($this->cards), array_pop($this->cards));     
    }  
    public function compareCards($card1,$card2)  
    {  
        $score1 = $this->ownScore($card1);  
        $score2 = $this->ownScore($card2);  
        if($score1 > $score2) return 1;  
        elseif($score1 < $score2) 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;  
        }  
        //补齐前导0  
        for($i = 0; $i < 3; $i++){  
            $figure[$i] = str_pad($figure[$i],2,&#39;0&#39;,STR_PAD_LEFT);  
        }  
        rsort($figure);  
        //对于对子做特殊处理  
        if($figure[1] == $figure[2]){  
            $temp = $figure[0];  
            $figure[0] = $figure[2];  
            $figure[2] = $temp;  
        }  
        $score = $figure[0].$figure[1].$figure[2];  
        //筒子 60*100000  
        if($figure[0] == $figure[1] && $figure[0] == $figure[2]){  
            $score += 60*100000;  
        }  
        //金花 30*100000  
        if($suit[0] == $suit[1] && $suit[0] == $suit[2]){  
            $score += 30*100000;  
        }  
        //顺子 20*100000  
        if($figure[0] == $figure[1]+1 && $figure[1] == $figure[2]+1 || implode($figure) ==&#39;140302&#39;){  
            $score += 20*100000;  
        }  
        //对子 10*100000  
        if($figure[0] == $figure[1] && $figure[1] != $figure[2]){  
  
            $score += 10*100000;  
        }  
        return $score;  
    }  
}  
  
//test  
$playCard = new PlayCards();  
$card1 = $playCard->getCard();  
$card2 = $playCard->getCard();  
$result = $playCard->compareCards($card1,$card2); 
echo &#39;card1 is &#39;,printCard($card1),&#39;<br/>&#39;;  
echo &#39;card2 is &#39;,printCard($card2),&#39;<br/>&#39;;  
$str = &#39;card1 equit card2&#39;;  
if($result == 1) $str =  &#39;card1 is larger than card2&#39;;  
elseif($result == -1) $str = &#39;card1 is smaller than card2&#39;;  
echo $str; 
function printCard($card)  
{  
    $str = &#39;(&#39;;  
    foreach($card as $v){  
        $str .= $v[0].$v[1].&#39;,&#39;;  
    }  
    return trim($str,&#39;,&#39;).&#39;)&#39;;  
}
Copy after login

Summary: The above is the entire content of this article, I hope it can be helpful to everyone’s learning helped.

Related recommendations:

php implements the detection and completion function for the end tag in the html tag

PHP implements the specified suffix file Batch upload function

php parsing and string processing of template files

The above is the detailed content of PHP implements golden flower game competition. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template