php 模拟斗地主发牌简单易懂

时间:2023-02-13 14:25:26

闲来无聊,就写了这个方法,也算是熟悉下php的数组操作,还请各位大神多指教。$arr 数组,好像有点问题,应该 2=>'方片2',3=>'梅花2',4=>'红心2',5=>'黑桃2', 等的顺序换下。看来牌打少了。估计这个方法,一般程序都不会用吧。呵呵

<?php
wash_card();
function wash_card(){
    $tmp = $arr = array(
        0=>'大王',1=>'小王',
        2=>'方片2',3=>'梅花2',4=>'红心2',5=>'黑桃2',
        6=>'方片A',7=>'梅花A',8=>'红心A',9=>'黑桃A',
        10=>'方片K',11=>'梅花K',12=>'红心K',13=>'黑桃K',
        14=>'方片Q',15=>'梅花Q',16=>'红心Q',17=>'黑桃Q',
        18=>'方片J',19=>'梅花J',20=>'红心J',21=>'黑桃J',
        22=>'方片10',23=>'梅花10',24=>'红心10',25=>'黑桃10',
        26=>'方片9',27=>'梅花9',28=>'红心9',29=>'黑桃9',
        30=>'方片8',31=>'梅花8',32=>'红心8',33=>'黑桃8',
        34=>'方片7',35=>'梅花7',36=>'红心7',37=>'黑桃7',
        38=>'方片6',39=>'梅花6',40=>'红心6',41=>'黑桃6',
        42=>'方片5',43=>'梅花5',44=>'红心5',45=>'黑桃5',
        46=>'方片4',47=>'梅花4',48=>'红心4',49=>'黑桃4',
        50=>'方片3',51=>'梅花3',52=>'红心3',53=>'黑桃3',
    );
   

    foreach($arr as $k=>$v){
        $index = rand(0,54 - $k -1);
        $key = array_search($tmp[$index], $arr); 
        $cards[$key] = $tmp[$index];
        unset($tmp[$index]);
        $tmp = array_values($tmp);
    }
    
    $landowner = array(0=>'用户一',1=>'用户二',2=>'用户三');
    $lk = rand(0,2);
    echo '本轮地主:  '.$landowner[$lk].'<br >';
    
    $card['landwehr']['cards'] = array_slice($cards, 0, 3, true); 
    ksort($card['landwehr']['cards']);
    echo  '地主:  '.implode('  ',$card['landwehr']['cards']).'<br >';
    
    $card['user1']['cards'] = array_slice($cards, 3, 17, true); 
    ksort($card['user1']['cards']);
    echo  '用户一:  '.implode('  ',$card['user1']['cards']).'<br >';
    
    $card['user2']['cards'] =array_slice($cards, 20, 17, true); 
    ksort($card['user2']['cards']);
    echo  '用户二:  '.implode('  ',$card['user2']['cards']).'<br >';
    
 
    $card['user3']['cards'] = array_slice($cards, 37, 17, true); 
    ksort($card['user3']['cards']);
    echo  '用户三:  '.implode('  ',$card['user3']['cards']).'<br >';
}


?>

php 模拟斗地主发牌简单易懂