用PHP实现一个关于德州扑克算法的程序(四):代码

程序主入口


include "Table.class.php";
//显示函数
function pre($str){
    echo '
';
    print_r($str);
    echo '
'
; } //创建扑克牌 $c=new Cards(); //初始化玩家数量 $table=new Table(2,$c->Cards);

扑克牌类 Cards.class.php

class Cards{
    public $Cards=array();

    function __construct()
    {
        $this->Cards=$this->createCards();
        shuffle($this->Cards);
    }
    private function createCards()
    {
        $cards=array();
        $suit=array("H","S","D","C");
        $p=array("T","J","Q","K","A");

        for($i=0;$i$suit);$i++){
            for($j=2;$j<=9;$j++){
                $cards[]=$suit[$i].$j;
            }
            for($k=0;$k$p);$k++){
                $cards[]=$suit[$i].$p[$k];
            }
        }
        return $cards;
    }
}

玩家类 Player.class.php

class Player{

    public $score=0;
    public $CardInHand=array();

    function __construct($cards)
    {
        $this->getCardInHand($cards);
    }

    private function getCardInHand($cards){
        $randkey=array_rand($cards,2);
        $this->CardInHand[]=$cards[$randkey[0]];
        $this->CardInHand[]=$cards[$randkey[1]];
        return $this->CardInHand;
    }

}

牌桌类(核心算法类)Table.class.php

include "Cards.class.php";
include "Player.class.php";

class  Table{

    public $players=array();
    public $tablecards=array();

    function __construct($num,$cards)
    {
        for($i=0;$i<$num;$i++){
            $this->players[$i]=new Player($cards);
            $cards=array_diff($cards,$this->players[$i]->CardInHand);
            //echo count($cards).'
';
$k=$i+1; echo "玩家".$k."的手牌为
"
; $this->showCard($this->players[$i]->CardInHand); echo "
==========
"
; } array_shift($cards); for($i=0;$i<3;$i++) { $this->tablecards[] = $cards[$i]; } $cards=array_diff($cards,$this->tablecards); for($i=0;$i<2;$i++){ array_shift($cards); $this->tablecards[] = $cards[0]; } echo "
====公牌为====
"
; $this->showCard($this->tablecards); for($i=0;$i$this->players);$i++){ $k=$i+1; echo "
----玩家{$k}的结果为----
"
; $this->result($this->players[$i]); echo "
-----------------------
"
; } } private function showCard($cards){ foreach ($cards as $ca) { $path.= ""; } echo $path; } private function pre($str,$comment){ echo "
=={$comment}===
"; print_r($str); echo '
====
'
; } private function result(Player $p){ $result=array_merge($p->CardInHand,$this->tablecards); //pre($result); foreach($result as $c){ $suit.=$c[0]; $point.=$c[1]; } //字符串转换为数组 $su=str_split($suit); $po=str_split($point); //统计花色和点数 $su_count=array_count_values($su); $po_count=array_count_values($po); switch(max($su_count)){ case "5": echo "这是".array_keys($su_count,"5")[0]."同花"."
"
; break; default: switch(max($po_count)){ case "4": echo "这是".array_keys($po_count,"4")[0]."四条"."
"
; $this->showCard($this->pukesort($result)); break; case "3": //echo "这是".array_keys($po_count,"3")[0]."三条"."
";
$this->isStraight($result); break; case "2": //echo "这是".array_keys($po_count,"2")[0]."对子"."
";
$this->isStraight($result); break; default: $this->isStraight($result); } break; } } private function pukesort($cards){ foreach($cards as $ca){ $arr[]=$ca[1]; } $arr=array_count_values($arr); foreach(array_keys($arr) as $v) { switch ($v) { case "A": $poi[] = 14; break; case "K": $poi[] = 13; break; case "Q": $poi[] = 12; break; case "J": $poi[] = 11; break; case "T": $poi[] = 10; break; default: $poi[] = $v; break; } } $puke=array( "num"=>array_values($arr), "poi"=>$poi ); array_multisort($puke["num"], SORT_NUMERIC, SORT_DESC, $puke["poi"],SORT_NUMERIC, SORT_DESC ); $arr= array_combine($puke["poi"],$puke["num"]); foreach($arr as $key=>$value){ switch($key){ case "14": $arr1["A"]=$arr["14"]; break; case "13": $arr1["K"]=$arr["13"]; break; case "12": $arr1["Q"]=$arr["12"]; break; case "11": $arr1["J"]=$arr["11"]; break; case "10": $arr1["T"]=$arr["10"]; break; default: $arr1[$key]=$value; break; } } $keys=array_keys($arr1); foreach($keys as $v){ $pstr= '/.'.$v.'/'; foreach($cards as $c) { if(preg_match($pstr,$c)){ $maxcard[]=$c; } } } $maxcard=array_slice($maxcard,0,5); return $maxcard; } private function pointToNum($arr){ $num=0; //统计在这副牌型中点数为数字的情况,从而判断A是做14合适还是作1合适 foreach($arr as $p){ if(!is_numeric($p)) $num++; } foreach($arr as $p){ switch($p){ case "A": if($num>=5){ $poi[]=14; }else{ $poi[]=1;//A还可以作为1使用 } break; case "K": $poi[]=13; break; case "Q": $poi[]=12; break; case "J": $poi[]=11; break; case "T": $poi[]=10; break; default: $poi[]=$p; break; } } return $poi; } private function numToPoint($arr){ foreach($arr as $p){ switch($p){ case "14": case "1": $poi[]="A"; break; case "13": $poi[]="K"; break; case "12": $poi[]="Q"; break; case "11": $poi[]="J"; break; case "10": $poi[]="T"; break; default : $poi[]=$p; } } return $poi; } private function isStraight($cards){ foreach($cards as $c){ $point.=$c[1]; } $po=array_unique(str_split($point)); //如果元素不为5个,则不满足顺子成牌的基本条件 if(count($po)<5){ $this->showCard($this->pukesort($cards)); }else{ $po=$this->pointToNum($po); arsort($po); $a1=array_values($po); $j=0; for($i=1;$i$a1);$i++){ if($a1[$i]==$a1[$i-1]-1){ $j++; $a2[]=array_search($a1[$i-1],$po); $a2[]=array_search($a1[$i],$po); }else{ $j=0; } } if($j>=4){ $a2=array_unique($a2);//去掉重复值 $a2=array_values($a2);//重建索引 $a2=array_slice($a2,0,5);//取出5个元素 for($i=0;$i$a2);$i++){ $result[]=$po[$a2[$i]]; } $arr=$this->numToPoint($result); foreach($arr as $v){ $pstr= '/.'.$v.'/'; foreach($cards as $c) { if(preg_match($pstr,$c)){ $maxcard[]=$c; break; } } } $this->showCard($this->pukesort($maxcard)); }else{ $this->showCard($this->pukesort($cards)); } } } }

运行显示效果:
用PHP实现一个关于德州扑克算法的程序(四):代码_第1张图片

用PHP实现一个关于德州扑克算法的程序(四):代码_第2张图片

用PHP实现一个关于德州扑克算法的程序(四):代码_第3张图片

用PHP实现一个关于德州扑克算法的程序(四):代码_第4张图片

ps:以上算是德州扑克的算法程序的90%内容,还有10%的内容是同花顺的判断,以及玩家分数的计算和胜负的判断,但是这些内容已经很简单,已经没有算法上的挑战性,因此《用PHP实现一个关于德州扑克算法的程序》就告一段落了。

你可能感兴趣的:(PHP学习)