php 炸金花牌型 和 比牌 规则

获取炸金花牌的类型

参数 :

$Poker 是一个数组

"size": 1 - 13 表示 $size_arr = ['A','二','三','四','五','六','七','八','九','十','J','Q','K'];

"tag": 1 - 4 表示 $tag_arr = ['黑桃','红桃','梅花','方片'];

例子:

    豹子A
    $Poker = [

        ['size' => 1,'tag' => 1,'name' => '黑桃A'],

        ['size' => 1,'tag' => 2,'name' => '红桃A'],

        ['size' => 1,'tag' => 3,'name' => '梅花A']

    ];
    
    顺金:A二三
    $Poker = [

        ['size' => 1,'tag' => 1,'name' => '黑桃A'],

        ['size' => 2,'tag' => 1,'name' => '黑桃二'],

        ['size' => 3,'tag' => 1,'name' => '黑桃三']

    ]; 
    
    金花:A六八
    $Poker = [

        ['size' => 1,'tag' => 1,'name' => '黑桃A'],

        ['size' => 6,'tag' => 1,'name' => '黑桃六'],

        ['size' => 8,'tag' => 1,'name' => '黑桃八']

    ];
    
    顺子:A二三
    $Poker = [

        ['size' => 1,'tag' => 1,'name' => '黑桃A'],

        ['size' => 2,'tag' => 2,'name' => '红桃二'],

        ['size' => 3,'tag' => 1,'name' => '黑桃三']

    ];
    
    
    对子:A挂三
    $Poker = [

        ['size' => 1,'tag' => 1,'name' => '黑桃A'],

        ['size' => 1,'tag' => 2,'name' => '红桃A'],

        ['size' => 3,'tag' => 1,'name' => '黑桃三']

    ];
    
    
    单张:A三九
    $Poker = [

        ['size' => 1,'tag' => 1,'name' => '黑桃A'],

        ['size' => 3,'tag' => 2,'name' => '红桃三'],

        ['size' => 9,'tag' => 1,'name' => '黑桃九']

    ]; 
    
    

代码:

    /**
     * 获取牌类型 
     * @param  array 	$Poker  		扑克牌数据
     * @return array      
     */
    function GetPokerType($Poker){
        $size_arr = ['A','二','三','四','五','六','七','八','九','十','J','Q','K'];
        if ($Poker) { 
    
            if($Poker[0]['size'] == 1 && $Poker[1]['size'] == 12 && $Poker[2]['size'] == 13) { // AQK的牌型
                
                if($Poker[0]['tag'] == $Poker[1]['tag'] && $Poker[0]['tag'] == $Poker[2]['tag']) {
                    
                    return [
                        'type'      => 8,
                        'size'      => 1,
                        'size2'     => 12,
                        'size3'     => 13,
                        'tag'       => $Poker[2]['tag'],
                        't_name'    => '顺金:AQK' 
                    ];
                }else{
                    
                    return [
                        'type'      => 6,
                        'size'      => 1,
                        'size2'     => 12,
                        'size3'     => 13, 
                        'tag'       => $Poker[2]['tag'],  
                        't_name'    => '顺子:AQK' 
                    ];
                }  
            }elseif ($Poker[0]['size']  == $Poker[1]['size'] && $Poker[0]['size'] == $Poker[2]['size']) { // 豹子
    
                return [
                    'type'      => 9,
                    'size'      => $Poker[2]['size'],
                    'tag'       => $Poker[2]['tag'],
                    't_name'    => '豹子:'.$size_arr[$Poker[2]['size']-1] 
                ];
    
            }elseif($Poker[1]['size'] == $Poker[0]['size'] + 1 && $Poker[2]['size'] == $Poker[0]['size'] + 2 && $Poker[0]['tag'] == $Poker[1]['tag'] && $Poker[0]['tag'] == $Poker[2]['tag'] ) { // 顺金
    
                if ($Poker[0]['size'] == 1) {
                    $size  = 1;
                    $size2 = $Poker[2]['size'];
                    $size3 = $Poker[1]['size'];
                    $tag   = $Poker[0]['tag']; 
                }else{
                    $size  = $Poker[2]['size'];
                    $size2 = $Poker[1]['size'];
                    $size3 = $Poker[0]['size'];
                    $tag   = $Poker[2]['tag']; 
                }
      
                return [ 
                    'type'      => 8,
                    'size'      => $size,
                    'size2'     => $size2,
                    'size3'     => $size3,
                    'tag'       => $tag,
                    't_name'    => '顺金:'.$size_arr[$Poker[0]['size']-1].$size_arr[$Poker[1]['size']-1].$size_arr[$Poker[2]['size']-1] 
                ];
    
            }elseif($Poker[0]['tag'] == $Poker[1]['tag'] && $Poker[0]['tag'] == $Poker[2]['tag']) { // 金花
                
                if ($Poker[0]['size'] == 1) {
                    $size  = 1;
                    $size2 = $Poker[2]['size'];
                    $size3 = $Poker[1]['size'];
                    $tag   = $Poker[0]['tag']; 
                }else{
                    $size  = $Poker[2]['size'];
                    $size2 = $Poker[1]['size'];
                    $size3 = $Poker[0]['size'];
                    $tag   = $Poker[2]['tag']; 
                } 
    
                return [
                    'type'      => 7,
                    'size'      => $size,
                    'size2'     => $size2,
                    'size3'     => $size3,
                    'tag'       => $tag,   
                    't_name'    => "金花:".$size_arr[$Poker[0]['size']-1].$size_arr[$Poker[1]['size']-1].$size_arr[$Poker[2]['size']-1]
                ];
           
            }elseif ($Poker[1]['size'] == $Poker[0]['size'] + 1 && $Poker[2]['size'] == $Poker[0]['size'] + 2) { // 顺子
                
                if ($Poker[0]['size'] == 1) {
                   
                    $size  = 1;
                    $size2 = $Poker[2]['size'];
                    $size3 = $Poker[1]['size'];
                    $tag   = $Poker[0]['tag']; 
                }else{
                   
                    $size  = $Poker[2]['size'];
                    $size2 = $Poker[1]['size'];
                    $size3 = $Poker[0]['size'];
                    $tag   = $Poker[2]['tag']; 
                }
               
                return [
                    'type'      => 6,
                    'size'      => $size,
                    'size2'     => $size2,
                    'size3'     => $size3,
                    'tag'       => $tag,  
                    't_name'    => '顺子:'.$size_arr[$Poker[0]['size']-1].$size_arr[$Poker[1]['size']-1].$size_arr[$Poker[2]['size']-1] 
                ];
    
            }elseif ($Poker[0]['size'] == $Poker[1]['size'] || $Poker[0]['size'] == $Poker[2]['size'] || $Poker[1]['size'] == $Poker[2]['size']) { // 对子
    
                if ($Poker[0]['size'] == $Poker[1]['size']) {
    
                    $size   = $Poker[0]['size'];
                    $size2  = $Poker[2]['size'];
                    $tag    = $Poker[2]['tag'];
                    $t_name = '对子:'.$size_arr[$Poker[0]['size']-1]."挂".$size_arr[$Poker[2]['size']-1]; 
              
                }elseif ($Poker[0]['size'] == $Poker[2]['size']) {  
    
                    $size   = $Poker[2]['size'];
                    $size2  = $Poker[1]['size'];
                    $tag    = $Poker[1]['tag'];
                    $t_name = '对子:'.$size_arr[$Poker[2]['size']-1]."挂".$size_arr[$Poker[1]['size']-1];
                }else{
                    
                    $size   = $Poker[2]['size'];
                    $size2  = $Poker[0]['size'];
                    $tag    = $Poker[0]['tag'];
                    $t_name = '对子:'.$size_arr[$Poker[2]['size']-1]."挂".$size_arr[$Poker[0]['size']-1];
                }
    
                return [
                    'type'    => 5,
                    'size'    => $size,
                    'size2'   => $size2,
                    'tag'     => $tag,
                    't_name'  => $t_name  
                ];
            }elseif ($Poker[0]['size'] == 2  && $Poker[1]['size'] == 3 && $Poker[2]['size'] == 5 ) { // 特殊
    
                return [    
                    'type'    => 4,
                    'size'    => $Poker[2]['size'], 
                    'size2'   => $Poker[1]['size'],
                    'size3'   => $Poker[0]['size'], 
                    'tag'     => $Poker[2]['tag'], 
                    't_name'  => '特殊:235' 
                ];
    
           }else{ // 单张 
    
                if ($Poker[0]['size'] == 1) {
                   
                    $size  = 1;
                    $size2 = $Poker[2]['size'];
                    $size3 = $Poker[1]['size'];
                    $tag   = $Poker[0]['tag']; 
                }else{
                    
                    $size  = $Poker[2]['size'];
                    $size2 = $Poker[1]['size'];
                    $size3 = $Poker[0]['size'];
                    $tag   = $Poker[2]['tag']; 
                }
                return [
                    'type'    => 3,
                    'size'    => $size,
                    'size2'   => $size2,
                    'size3'   => $size3, 
                    'tag'     => $tag,
                    't_name'  => '单张:'.$size_arr[$Poker[0]['size']-1].$size_arr[$Poker[1]['size']-1].$size_arr[$Poker[2]['size']-1]  
                ];
    
            }  
        }   
    }


    /**
     * 判断炸金花输赢 
     * @param  array 	$Poker1  		玩家1扑克牌数据
     * @param  array 	$Poker2  		玩家2扑克牌数据
     * @return boole      
     */
    function PokerWinning($Poker1,$Poker2){ 
        $type1 = GetPokerType($Poker1);
        $type2 = GetPokerType($Poker2); 
    
        if ($type1['type'] == 5 && $type2['type'] == 5) {
            // 首先判断两个对子
            if ($type1['size']==$type2['size']) {
              //对子一样
                
                if ($type1['size2']==$type2['size2']) {
                    //单张一样  
                    
                    if (compare($type1['tag'],$type2['tag'],'tag')) {
                        
                        return true; 
                    }else{
                        
                        return false;
                    }
                }else{
                    
                    if (compare($type1['size2'],$type2['size2'],'size')) {
                        
                        return true;
                    }else{
                        
                        return false;
                    }
                }
           }else{
                
                if (compare($type1['size'],$type2['size'],'size')) {
                    
                    return true;
                }else{
                   
                    return false;
                }
            }
    
        }elseif ($type1['type'] == $type2['type']) {
            
            if ($type1['size']==$type2['size']) {
            
                if ($type1['size2']==$type2['size2']) {
                
                    if ($type1['size3']==$type2['size3']) {
                    
                        if (compare($type1['tag'],$type2['tag'],'tag')) {
                            
                            return true; 
                        }else{
                            
                            return false;
                        }
                        
                    }else{
                    
                        if (compare($type1['size3'],$type2['size2'],'size')) {
                            
                            return true;
                        }else{
                           
                            return false;
                        }
                    }
                }else{
                
                    if (compare($type1['size2'],$type2['size2'],'size')) {
                       
                        return true;
                    }else{
                        
                        return false;
                    }
                }
            }else{
            
                if (compare($type1['size'],$type2['size'],'size')) {
                    
                    return true;
                }else{
                
                    return false;
                }
            }    
        }else{
        
            // 特殊 235 和 豹子
            if ($type1['type'] == 9 && $type2['type'] == 4) {
              
                return false;
            }else if ($type1['type'] == 4 && $type2['type'] == 9) {
              
                return true;
            }else{
    
                if ($type1['type']>$type2['type']) {
                
                    return true;
                }else{
                
                    return false;
                }
            }
        }
    }
    
    
    
    /**
     * 炸金花单张比大小 
     * @param  array 	$Poker1  		玩家1扑克牌数据
     * @param  array 	$Poker2  		玩家2扑克牌数据
     * @return boole      
     */
    function compare($data1,$data2,$datatype){    
    
        //size(大小): 1-13
        if ($datatype == 'size') { // 大小  
            if ( $data1 == 1 ) {
                $tag1 = 14;
                $tag2 = $data2;
            }else if( $data2 == 1){
                $tag2 = 14;
                $tag1 = $data1;
            }else{
                $tag1 = $data1;
                $tag2 = $data2;
            }
            if ($tag1 > $tag2) {
    
                    return true;
            }else{
    
                return false;
            }
    
        }
    
        // tag (花色):1 -- 黑桃  2 -- 红桃 3 -- 梅花  4 -- 方片    
        if ($datatype == 'tag') {
    
            $tag_arr  = [1 => 4, 2 => 3, 3 => 2, 4 => 1]; 
    
            if ($tag_arr[$data1] > $tag_arr[$data2]) {
    
                return true;
            }else{ 
    
                return false;
            }
    
        }
    }

你可能感兴趣的:(算法,游戏开发,扎金花)