欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法

 更新時(shí)間:2015年03月10日 09:08:44   作者:i_like_cpp  
這篇文章主要介紹了PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法,實(shí)例分析了扎金花游戲的實(shí)現(xiàn)原理與相關(guān)算法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法。分享給大家供大家參考。具體分析如下:

程序離不開(kāi)算法,前面討論過(guò)尋路的算法。不過(guò),當(dāng)時(shí)的示例圖中,可選的路徑是唯一的。我們挑選一個(gè)算法,就是說(shuō)要把這個(gè)唯一的路徑選出來(lái),怎么選呢?

還記得上初中的時(shí)候經(jīng)常下午放學(xué)就躲在路邊扎金花來(lái)賭*錢(qián),貌似還上癮了,現(xiàn)在過(guò)年的時(shí)候還經(jīng)常一起扎金花賭*錢(qián),但運(yùn)氣不啥好,每次都是輸啊。

今天陽(yáng)光明媚,由于清明節(jié)才出去玩了,所以今天沒(méi)有去哪。閑著沒(méi)事就想了下怎么用程序?qū)崿F(xiàn)金花中兩幅牌的大小比較,現(xiàn)在把它實(shí)現(xiàn)了,有些方法還是蠻重要的,因此就記下來(lái)。

好了,不廢話了。

扎金花兩副牌的比較規(guī)則就不說(shuō)了,注明一下是順子的時(shí)候 : JQK < A23 < QKA

思路:扎金花

1. 隨機(jī)生成兩幅牌,每副牌結(jié)構(gòu)為

復(fù)制代碼 代碼如下:
array( 
    array('Spade','K'), 
    array('Club','6'), 
    array('Spade','J'), 
)

復(fù)制代碼 代碼如下:
array( 
    array('Spade','K'), 
    array('Club','6'), 
    array('Spade','J'), 
)

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

每張牌的分值為一個(gè)2位數(shù),不足2位的補(bǔ)前導(dǎo)0,例如'A':14,‘10':10,'2‘:'02‘,'k‘:13,'7‘:07

將3張牌按點(diǎn)數(shù)大小排序(從大到小),湊成一個(gè)6位數(shù)。例如'A27':140702,‘829':090802,‘JK8':131108,‘2A10':141002

例外,對(duì)于對(duì)子要將對(duì)子的位數(shù)放在前兩位(后面會(huì)看到為什么這么做)。例如‘779':070709,‘7A7':070714,‘A33':030314

現(xiàn)在的分值是一個(gè)6位數(shù),將對(duì)子設(shè)為一個(gè)原始值加上10*100000的值,現(xiàn)在為一個(gè)7位數(shù)。例如‘779':1070709,‘7A7':1070714,‘A33':1030314

對(duì)于順子,將結(jié)果加上20*100000.。例如‘345':2050403,‘QKA':2141312,‘23A':2140302

對(duì)于金花,將結(jié)果加上30*100000。例如‘Spade K,Spade 6,Spade J':3131106

因?yàn)轫樈鸬臅r(shí)候其實(shí)是金花和順子的和,所以順金應(yīng)該是50*10000。 例如‘Spade 7,Spade 6,Spade 8':5080706

對(duì)于筒子,將結(jié)果加上60*100000。例如'666‘:6060606,'JJJ‘:6111111

3. 比較兩幅牌的大小(用所計(jì)算的分值來(lái)比較)

就這么簡(jiǎn)單??!

代碼如下(PHP)

復(fù)制代碼 代碼如下:
<?php 
class PlayCards 

    public $suits = array('Spade', 'Heart', 'Diamond', 'Club'); 
    public $figures = array('2', '3', '4', '5', '6', '7', '8', '9', '10', '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); 
        //生成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; 
        } 
        //補(bǔ)齊前導(dǎo)0 
        for($i = 0; $i < 3; $i++){ 
            $figure[$i] = str_pad($figure[$i],2,'0',STR_PAD_LEFT); 
        } 
        rsort($figure); 
        //對(duì)于對(duì)子做特殊處理 
        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) =='140302'){ 
            $score += 20*100000; 
        } 
        //對(duì)子 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 'card1 is ',printCard($card1),'<br/>'; 
echo 'card2 is ',printCard($card2),'<br/>'; 
$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,',').')'; 
}


復(fù)制代碼 代碼如下:
<?php 
class PlayCards 

    public $suits = array('Spade', 'Heart', 'Diamond', 'Club'); 
    public $figures = array('2', '3', '4', '5', '6', '7', '8', '9', '10', '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); 
        //生成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; 
        } 
        //補(bǔ)齊前導(dǎo)0 
        for($i = 0; $i < 3; $i++){ 
            $figure[$i] = str_pad($figure[$i],2,'0',STR_PAD_LEFT); 
        } 
        rsort($figure); 
        //對(duì)于對(duì)子做特殊處理 
        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) =='140302'){ 
            $score += 20*100000; 
        } 
        //對(duì)子 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 'card1 is ',printCard($card1),'<br/>'; 
echo 'card2 is ',printCard($card2),'<br/>'; 
$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,',').')'; 
}

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 分享php多功能圖片處理類

    分享php多功能圖片處理類

    這篇文章主要為大家分享php多功能圖片處理類,方便大家進(jìn)行學(xué)習(xí)研究,感興趣的小伙伴們可以參考一下
    2016-05-05
  • php若干單維數(shù)組遍歷方法的比較

    php若干單維數(shù)組遍歷方法的比較

    for循環(huán)只對(duì)數(shù)字索引有限;for和foreach遍歷結(jié)束后不需要對(duì)數(shù)據(jù)進(jìn)行reset()操作即可供下次遍歷,而each方法則需要。
    2011-09-09
  • Windows中安裝Apache2和PHP4權(quán)威指南

    Windows中安裝Apache2和PHP4權(quán)威指南

    Windows中安裝Apache2和PHP4權(quán)威指南...
    2006-11-11
  • php中addslashes函數(shù)與sql防注入

    php中addslashes函數(shù)與sql防注入

    這篇文章主要介紹了php中addslashes函數(shù)與sql防注入,實(shí)例講述了采用addslashes函數(shù)對(duì)于sql防注入的用處,對(duì)于PHP安全程序設(shè)計(jì)來(lái)說(shuō)具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • php ckeditor上傳圖片文件名亂碼解決方法

    php ckeditor上傳圖片文件名亂碼解決方法

    文件名亂碼一般是中文導(dǎo)致的,因?yàn)閏keditor使用的是uft8編碼如果我們頁(yè)面使用的是gbk或gb2312就有可能出現(xiàn)亂碼問(wèn)題,解決辦法只要對(duì)上傳文件重命名即可,下面是如何修改程序代碼的方法
    2013-11-11
  • 利用PHP快速抓取音頻數(shù)據(jù)的方法詳解

    利用PHP快速抓取音頻數(shù)據(jù)的方法詳解

    這篇文章主要給大家介紹了如何利用PHP快速抓取音頻數(shù)據(jù),文中使用Dusk庫(kù)和PHP編寫(xiě)的爬蟲(chóng)程序,用于爬取海量的音頻數(shù)據(jù),有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2023-10-10
  • PHP使用內(nèi)置函數(shù)生成圖片的方法詳解

    PHP使用內(nèi)置函數(shù)生成圖片的方法詳解

    這篇文章主要介紹了PHP使用內(nèi)置函數(shù)生成圖片的方法,結(jié)合實(shí)例形式詳細(xì)分析了php生成圖片的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-05-05
  • php讀取mssql的ntext字段返回值為空的解決方法

    php讀取mssql的ntext字段返回值為空的解決方法

    這篇文章主要介紹了php讀取mssql的ntext字段返回值為空的解決方法,可通過(guò)修改字段類型或采用adodb組件來(lái)解決,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12
  • PHP計(jì)算字符串真正的寬度和高度像素(圖片加文字水印示例)

    PHP計(jì)算字符串真正的寬度和高度像素(圖片加文字水印示例)

    這篇文章通過(guò)給圖片加文字水印示例來(lái)解釋PHP計(jì)算字符串真正的寬度和高度像素的方法。計(jì)算字符串用strlen()只能得到字符串長(zhǎng)度,不是寬高像素,而根據(jù)字體的大小和所用字體計(jì)算出的才是字符串真正的寬高像素。
    2023-02-02
  • php 7新特性之類型申明詳解

    php 7新特性之類型申明詳解

    在PHP7,一個(gè)新的功能,返回類型聲明已被引入。返回類型聲明指定的一個(gè)函數(shù)返回值的類型。下面這篇文章主要給大家介紹了php 7新特性之類型申明的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-06-06

最新評(píng)論