php牛牛发牌算法,分享一个牛牛算法

分享一个算法计算牛牛的。

Taurus.php

$cards = str_split('1111222233334444555566667777888899990000JJJJQQQQKKKK');

// 洗牌

shuffle($cards);

// 假设游戏人数为4人 默认发排顺序为 A > B > C > D

$a = array($cards[0], $cards[4], $cards[8], $cards[12], $cards[16]);

$b = array($cards[1], $cards[5], $cards[9], $cards[13], $cards[17]);

$c = array($cards[2], $cards[6], $cards[10], $cards[14], $cards[18]);

$d = array($cards[3], $cards[7], $cards[11], $cards[15], $cards[19]);

// 随机抽取一张牌计算发牌顺序,0 J Q K 分别为10,11,12,13

# $number = $cards[rand(0, count($cards) - 1)];

$number = $cards[random(0, count($cards) - 1)];

switch ($number) {

case '1':case '5':case '9':case 'K':

echo "发排顺序:A > B > C > D\r\n";

$people = array("A" => $a, "B" => $b, "C" => $c, "D" => $d);

break;

case '2':case '6':case '0':

echo "发排顺序:B > C > D > A\r\n";

$people = array("B" => $a, "C" => $b, "D" => $c, "A" => $d);

break;

case '3':case '7':case 'J':

echo "发排顺序:C > D > A > B\r\n";

$people = array("C" => $a, "D" => $b, "A" => $c, "B" => $d);

break;

case '4':case '8':case 'Q':

echo "发排顺序:D > A > B > C\r\n";

$people = array("D" => $a, "A" => $b, "B" => $c, "C" => $d);

break;

}

foreach ($people as $key => $value) {

echo "$key: " . implode(',', $value) . " " . taurus($value) . "\r\n";

}

function taurus($cards){

// 4带1

$judge = array_values(array_count_values($cards));

if($judge == array(1, 4) || $judge == array(4, 1)){

return "4带1";

}

// 3带2

if($judge == array(2, 3) || $judge == array(3, 2)){

return "3带2";

}

// 处理数据

foreach ($cards as $key => $value) {

if(!is_numeric($value)){

$cards[$key] = 0;

}

$cards[$key] = (int)$value;

}

$r = arrangement($cards, 3);

foreach ($r as $key => $value) {

if(0 == (array_sum($value) % 10)){

# $res = array_diff($cards, $value);

$res = array_sum($cards) - array_sum($value);

$taurus = 0;

$tmp = array_sum($res) % 10;

if(0 == $tmp){

return "牛牛";

}

if(array_sum($res) % 10 > $taurus){

$taurus = $tmp;

}

}

}

if(isset($taurus)){

return "牛$taurus";

}

return "没牛";

}

function arrangement($a, $m){

$r = array();

$n = count($a);

if($m <= 0 || $m > $n){

return $r;

}

for ($i=0; $i < $n; $i++) {

$b = $a;

$t = array_splice($b, $i, 1);

if ($m == 1) {

$r[] = $t;

} else {

$c = arrangement($b, $m - 1);

foreach ($c as $v) {

$r[] = array_merge($t, $v);

}

}

}

return $r;

}

执行命令后。

57b7b39a935886a87cf27b5131becf47.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值