Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP实现炸金花游戏比赛

PHP实现炸金花游戏比赛

墨辰丷
墨辰丷asal
2018-06-12 17:33:183340semak imbas

这篇文章主要介绍了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针对模板文件的解析与字符串处理

Atas ialah kandungan terperinci PHP实现炸金花游戏比赛. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php实现学生管理系统Artikel seterusnya:PHP如何生成推广海报