首頁 >後端開發 >php教程 >PHP實現炸金花遊戲比賽

PHP實現炸金花遊戲比賽

墨辰丷
墨辰丷原創
2018-06-12 17:33:183357瀏覽

這篇文章主要介紹了PHP實現炸金花遊戲比賽的方法,實例分析了炸金花遊戲的實現原理與相關演算法技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP實現炸金花遊戲比賽的方法。具體分析如下:

程式離不開演算法,前面討論過尋路的演算法。不過,在當時的範例圖中,可選的路徑是唯一的。我們挑選一個演算法,就是說要把這個唯一的路徑選出來,怎麼選呢?

炸金花兩副牌的比較規則就不說了,註明一下是順子的時候: JQK < A23 < QKA

思路:炸金花

1. 隨機產生兩幅牌,每副牌結構為

程式碼如下:

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

程式碼如下:

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

2. 計算每副牌的分值:每副牌有個原始大小(即排除對子,順子,金花,順金,筒子的大小),再

每張牌的分值為一個2位數,不足2位的補前導0,例如'A':14,'10':10,'2':'02','k':13,'7':07

將3張牌依點數大小排序(從大到小),湊成一個6位數。例如'A27':140702,'829':090802,'JK8':131108,'2A10':141002

#例外,對於子要將對子的位數放在前兩位(後面會看到為什麼這麼做)。例如'779':070709,'7A7':070714,'A33':030314

現在的分數是一個6位數,將對子設為一個原始值加上10*100000的值,現在為一個7位數。例如‘779':1070709,‘7A7':1070714,‘A33':1030314

對於順子,將結果加上20*100000.。例如‘345':2050403,‘QKA':2141312,‘23A':2140302

對於金花,將結果加上30*100000。例如‘Spade K,Spade 6,Spade J':3131106

因為順金的時候其實是金花和順子的和,所以順金應該是50*10000。例如‘Spade 7,Spade 6,Spade 8':5080706

#對於筒子,將結果加上60*100000。例如'666‘:6060606,'JJJ‘:6111111

3. 比較兩幅牌的大小(用計算的分數來比較)

#就這麼簡單! !

程式碼如下:

<?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;;  
}

程式碼如下:

<?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;;  
}

#總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php實作html標籤中結束標籤的偵測與補全功能

PHP實作針對指定後綴文件的批次上傳功能

php針對模板檔案的解析與字串處理

#

以上是PHP實現炸金花遊戲比賽的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn