<?phpnamespace frontend\business;classniuniugamehelper{/** * @param $card * @return int Results-1 per cow, 0 kn, 1-9 kn to ox nine*/ Public Static functionJudgecowcow ($card) { if(!Is_array($card) ||Count($card)!== 5) { return-1; } $cow=-1; //calculate the total value of 5 cards, cow calculate the number of cows. $cardall= 0; $n= 0;//Store 10, J, Q, K number. for($i= 0;$i<4;$i++)//sort from big to small for 5 cards. { for($j=$i+1;$j<5;$j++) if($card[$i] <$card[$j]) { $a=$card[$i]; $card[$i] =$card[$j]; $card[$j]=$a; } } for($i= 0;$i<5;$i++) { if($card[$i] >= 10) { $n++; $card[$i] = 10; } $cardall+=$card[$i]; } //10 10 6 5 3 NIU NIU 5 card sum must be a multiple of 10, five card sum is 10 of the multiples may not be bull cattle, the following code is not established//if (cardall%10 = = 0)//{//print (0); Niuniu//exit (0); //} Switch($n) { Case0://5 cards do not have a 10, J, Q, K. { for($i= 0;$i<4;$i++) { for($j=$i+ 1;$j<5;$j++) if(($cardall-$card[$i]-$card[$j])%10==0) { $cow=($card[$i] +$card[$j])%10; return $cow; } } Break; } Case1://5 cards have a 10, J, Q, K. { //first of all to determine whether there are cattle and cattle, can not judge the remaining four added to 10 times times the number of cattle, such as Q 8 5 4 3//can only determine whether the two is a multiple of 10, if it is to determine whether the surplus is a multiple of 10, the limited judgment of the cow and cattle, and then to determine whether three is a multiple Cattle, otherwise no cattle for($i= 1;$i< 4;$i++) { for($j=$i+1;$j< 5;$j++) { if(($card[$i] +$card[$j])% 10 = = 0) { $cow=($cardall-$card[0]) %10; return $cow; } } } //determine if there are any cows for($i= 1;$i<5;$i++)//There are four cards left and three plus equals ten. { if(($cardall-$card[0]-$card[$i])%10==0) { $cow=($cardall-$card[0]) %10; Break; } } Break; } Case2://5 cards have two 10, J, Q, K. Three is a cow, you have a problem, you should prioritize the output { if(($cardall-$card[0]-$card[1]) %10 = = 0)//Priority cow and ox output such as J Q 2 3 5; Check if the remainder is bull or bull, otherwise the algorithm has a loophole. { $cow= 0; } Else { //Ten 6 5 3 n=2 i=3 j=4 cardall = for($i=$n;$i<4;$i++)//The remaining three (four) cards have two and add up equal to 10. { for($j=$i+1;$j<5;$j++) { if(($card[$i]+$card[$j]) ==10) { $temp=$cardall; for($k= 0;$k<$n;$k++)//the total value minus 10, J, Q, K cards. $temp-=$card[$k];// - $cow=$temp%10//8 } } /*Print_r (' Cardall: '. $cardall); Print_r ("<br/>"); Print_r (' cow '. $cow); Print_r ("<br/>");*/ } } Break; } Case3://5 cards have three 10, J, Q, K. Case4://5 cards have four 10, J, Q, K. Case5://5 cards in five are 10, J, Q, K. { for($i= 0;$i<$n;$i++)//the total value minus 10, J, Q, K cards. { $cardall-=$card[$i]; } $cow=$cardall%10; Break; } } return $cow; }}
Cow and Cow game algorithm