2014年七月写过的代码,现在看来,还有待改进呀
那个时候闲来无事,随手写了一个斗地主的逻辑
<?php /** * Created by JetBrains PhpStorm. * User: Administrator * Date: 14-7-30 * Time: 下午3:49 * To change this template use File | Settings | File Templates. */ class DemoAction extends Action{ private $order = array(); function _initialize(){ $this->rule(); } function index(){ $array = $this->getAllCard(); shuffle($array); $i = 0; //分牌 foreach($array as $val){ $j = ++$i%3; $result[$j][] = $val; } //三张底牌 $other = array(); $other[0] = array_pop($result[0]); $other[1] = array_pop($result[1]); $other[2] = array_pop($result[2]); //print_r($result[0]); $result2[0] = $this->sort($result[0]); $res = $this->insert_three($result2[0],$other); print_r($res);die; $result2[1] = $this->sort($result[1]); $result2[2] = $this->sort($result[2]); print_r($result2); print_r($other); } //排序 function sort($cards){ $temp = array(); foreach($cards as $k=>$v){ $temp[$v['num']][] = $k; } $temp2 = array(); foreach($this->order as $val){ $temp[$val] && $temp2[] = $temp[$val]; } $temp3 = array(); foreach($temp2 as $k2=>$v2){ foreach($v2 as $k3=>$v3){ $temp3[] = $cards[$v3]; } } return $temp3; } //抢地主之后,将底牌插入序列 function insert_three($array,$three){ foreach($three as $val){ $temp[$val['num']][] = $val['color']; } $nums = array_keys($temp); $new_array = array(); $insert1 = true; $insert2 = true; $insert3 = true; foreach($array as $key2=>$val2){ $new_array[] = $val2; $next_key = $key2+1; if($insert1){ $begin_compare = $this->compare_num($val2['num'],$nums[0]); $end_compare = $this->compare_num($nums[0],$array[$next_key]['num']); if($begin_compare<=0 && $end_compare<=0){ foreach($temp[$nums[0]] as $color){ $new_array[] = array('num'=>$nums[0],'color'=>$color); } unset($nums[0]); $insert1 = false; } } if($nums[1] && $insert2){ $begin_compare = $this->compare_num($val2['num'],$nums[1]); $end_compare = $this->compare_num($nums[1],$array[$next_key]['num']); if($begin_compare<=0 && $end_compare<=0){ foreach($temp[$nums[1]] as $color){ $new_array[] = array('num'=>$nums[1],'color'=>$color); } unset($nums[1]); $insert2 = false; } } if($nums[2] && $insert3){ $begin_compare = $this->compare_num($val2['num'],$nums[2]); $end_compare = $this->compare_num($nums[2],$array[$next_key]['num']); if($begin_compare<=0 && $end_compare<=0){ foreach($temp[$nums[2]] as $color){ $new_array[] = array('num'=>$nums[2],'color'=>$color); } unset($nums[2]); $insert3 = false; } } } $last_count = count($nums); if($last_count!=0){ if($last_count==1){ $this_num = current($nums); foreach($temp[$this_num] as $color){ $new_array[] = array('num'=>$this_num,'color'=>$color); } }else{ sort($nums); foreach($nums as $k=>$v){ foreach($temp[$v] as $color){ $new_array[] = array('num'=>$v,'color'=>$color); } } } } return $new_array; } function getAllCard(){ return array( array("num"=>1,"color"=>1), array("num"=>1,"color"=>2), array("num"=>1,"color"=>3), array("num"=>1,"color"=>4), array("num"=>2,"color"=>1), array("num"=>2,"color"=>2), array("num"=>2,"color"=>3), array("num"=>2,"color"=>4), array("num"=>3,"color"=>1), array("num"=>3,"color"=>2), array("num"=>3,"color"=>3), array("num"=>3,"color"=>4), array("num"=>4,"color"=>1), array("num"=>4,"color"=>2), array("num"=>4,"color"=>3), array("num"=>4,"color"=>4), array("num"=>5,"color"=>1), array("num"=>5,"color"=>2), array("num"=>5,"color"=>3), array("num"=>5,"color"=>4), array("num"=>6,"color"=>1), array("num"=>6,"color"=>2), array("num"=>6,"color"=>3), array("num"=>6,"color"=>4), array("num"=>7,"color"=>1), array("num"=>7,"color"=>2), array("num"=>7,"color"=>3), array("num"=>7,"color"=>4), array("num"=>8,"color"=>1), array("num"=>8,"color"=>2), array("num"=>8,"color"=>3), array("num"=>8,"color"=>4), array("num"=>9,"color"=>1), array("num"=>9,"color"=>2), array("num"=>9,"color"=>3), array("num"=>9,"color"=>4), array("num"=>10,"color"=>1), array("num"=>10,"color"=>2), array("num"=>10,"color"=>3), array("num"=>10,"color"=>4), array("num"=>11,"color"=>1), array("num"=>11,"color"=>2), array("num"=>11,"color"=>3), array("num"=>11,"color"=>4), array("num"=>12,"color"=>1), array("num"=>12,"color"=>2), array("num"=>12,"color"=>3), array("num"=>12,"color"=>4), array("num"=>13,"color"=>1), array("num"=>13,"color"=>2), array("num"=>13,"color"=>3), array("num"=>13,"color"=>4), array("num"=>14,"color"=>1), array("num"=>14,"color"=>2), ); } function rule(){ $this->order = array(3,4,5,6,7,8,9,10,11,12,13,1,2,14); } //根据规则比较牌的大小 function compare_num($num1,$num2){ if($num1===false)return false; $index1 = array_search($num1,$this->order); $index2 = array_search($num2,$this->order); if($index1>$index2)return 1; if($index1<$index2)return -1; return 0; } //炸弹 function is_bomb($cards){ $count = $cards[0]['num']==14?2:4; if(count($cards)!=$count) return false; if(!empty($cards[0]['num']) && $cards[0]['num']==$cards[1]['num'] && $cards[0]['num']==$cards[2]['num'] && $cards[0]['num']==$cards[3]['num']){ return true; } return false; } //炸弹KO function hit_bomb($initiative,$passive){ if( $this->is_bomb($initiative) && $this->is_bomb($passive)){ if($this->compare_num($initiative[0]['num'],$passive[0]['num'])==1){ return true; }else{ return false; } } return null; } //单牌$cards = array([0]=array(....)); function is_one($cards){ if(count($cards)!=1)return false; return true; } //单牌KO $passive是单牌 function hit_one($initiative,$passive){ if($this->is_one($initiative)){ if($this->compare_num($initiative[0]['num'],$passive[0]['num'])==1){ return true; } }elseif($this->is_bomb($initiative)) return true; } //对牌 function is_twins($cards){ if(count($cards)!=2)return false; return (!empty($cards[0]['num']) && $cards[0]['num']==$cards[1]['num'] && $cards[0]['num']!=14); } /** * 对牌KO,要求$passive必须是is_twins * @param $initiative主动 * @param $passive被动 */ function hit_twins($initiative,$passive){ //if($this->is_twins($passive)){ if($this->is_twins($initiative)){ if($this->compare_num($initiative[0]['num'],$passive[0]['num'])==1){ return true; } }elseif($this->is_bomb($initiative)) return true; //} //return null; } //三个X function is_three($cards){ if(count($cards)!=3)return false; return (!empty($cards[0]['num']) && $cards[0]['num']==$cards[1]['num'] && $cards[0]['num']==$cards[2]['num']); } //要求$passive必须是is_three function hit_three($initiative,$passive){ //if($this->is_three($passive)){ if($this->is_three($initiative)){ if($this->compare_num($initiative[0]['num'],$passive[0]['num'])==1){ return true; } }elseif($this->is_bomb($initiative)) return true; //} //return null; } //三带一 function is_three_one($cards){ if(count($cards)!=4)return false; $tongji = array(); foreach($cards as $ca){ ++$tongji[$ca['num']]; } if(count($tongji)!=2)return false; return array_search(3,$tongji); } //$passive_num 由is_three_one计算得来 function hit_three_one($initiative,$passive_num){ if($this->compare_num($this->is_three_one($initiative),$passive_num)==1) { return true; }elseif($this->is_bomb($initiative)) return true; } //三代二 function is_three_two($cards){ if(count($cards)!=5)return false; $tongji = array(); foreach($cards as $ca){ ++$tongji[$ca['num']]; } if(count($tongji)!=2)return false; if(array_search(2,$tongji)===false)return false; return array_search(3,$tongji); } //$passive_num 由is_three_two计算得来 function hit_three_two($initiative,$passive_num){ if($this->compare_num($this->is_three_two($initiative),$passive_num)==1) { return true; }elseif($this->is_bomb($initiative)) return true; } //四带二 function is_four_two($cards){ if(count($cards)!=6)return false; $tongji = array(); foreach($cards as $ca){ ++$tongji[$ca['num']]; } if(count($tongji)!=2)return false; if(array_search(2,$tongji)===false)return false; return array_search(4,$tongji); } //$passive_num 由is_four_two计算得来 function hit_four_two($initiative,$passive_num){ if($this->is_bomb($initiative)) return true; if($this->compare_num($this->is_four_two($initiative),$passive_num)==1) { return true; } } //顺子 function is_sequence($cards){ $count = count($cards); if($count<5)return false; $nums = array(); foreach($cards as $ca){ //小二,王都不算顺子牌 if(in_array($ca['num'],array(2,14)))return false; $nums[] = $ca['num']; } $min = min($nums); if($min==1)return false; $index = array_search($min,$this->rule); for($i=1;$i<$count;++$i) if(!in_array($this->rule[++$index],$nums))return false; return $min; } //$passive_num 由is_sequence计算得来 function hit_sequence($initiative,$passive_num){ if($this->is_bomb($initiative)) return true; if($this->compare_num($this->is_sequence($initiative),$passive_num)==1) { return true; } } //多联对 function is_more_twins($cards){ $count = count($cards); if($count<6 || $count%2==1)return false; $nums = $tongji = array(); foreach($cards as $ca){ //小二,王都不算顺子牌 if(in_array($ca['num'],array(2,14)))return false; $nums[] = $ca['num']; ++$tongji[$ca['num']]; } foreach($tongji as $v){ if($v!=2)return false; } $min = min($nums); if($min==1)return false; $index = array_search($min,$this->rule); $count = $count/2; for($i=1;$i<$count;++$i) if(!in_array($this->rule[++$index],$nums))return false; return $min; } //$passive_num 由is_more_twins计算得来 function hit_more_twins($initiative,$passive_num){ if($this->is_bomb($initiative)) return true; if($this->compare_num($this->is_more_twins($initiative),$passive_num)==1) { return true; } } //飞机 function is_plan($cards){ $count = count($cards); if($count<6)return false; $nums = $tongji = array(); foreach($cards as $ca){ //王都不算飞机牌 if(in_array($ca['num'],array(14)))return false; $nums[] = $ca['num']; ++$tongji[$ca['num']]; } $main = array(); $append_count = 0; foreach($tongji as $k=>$v){ if($v>3) return false; elseif($v==3) $main[] = $k; else ++$append_count; } $main_count = count($main); if($main_count!=$append_count)return false; $min = min($main); $index = array_search($min,$this->rule); $count = $count/2; for($i=1;$i<$count;++$i) if(!in_array($this->rule[++$index],$nums))return false; return $min; } function hit_plan($initiative,$passive_num){ if($this->is_bomb($initiative)) return true; if($this->compare_num($this->is_plan($initiative),$passive_num)==1) { return true; } } }
现在看来,当时还是挺不规范的