[ ] () PHP |
function cardsCreation()
{
$arrayCards = [];
for ($i = 0; $i < 7; $i++) {
$card = mt_rand(2, 14); // 2 14
$multiplier = mt_rand(0, 3); //
if (1 == $multiplier) { // 1, 100
$card = $card + 100;
} else if (2 == $multiplier) { // 2, 200
$card = $card + 200;
} else if (3 == $multiplier) { // 3, 300
$card = $card + 300;
}
if (!in_array($card, $arrayCards)) { //, ,
$arrayCards = array_merge($arrayCards, [$card]);
} else { // , ,
$i--;
}
}
return $arrayCards;
}
function straight(array $arrayCards) {
$newArrayCards = [];
$ace = false;
foreach ($arrayCards as $arrayCard) {
$newArrayCards = array_merge($newArrayCards, [$arrayCard % 100]); // 100
if(14 == $arrayCard % 100) { // , 2, 3, 4, 5
$ace = true;
}
}
if($ace == true) {
$newArrayCards = array_merge($newArrayCards, [1]); // , 1
}
rsort($newArrayCards); //
$count = 0; //, 1 , = 1
$length = 4; //,
$result = 0; //
$begin = 0; // ,
for($i = 1; $i <= $length; $i++) {
if (-1 == ($newArrayCards[$i] - $newArrayCards[$i - 1])) {
$count++;
}
if($length == $i) {
if(4 == $count) { //$count == 4,
$result = $newArrayCards[$begin] * 1e+8;
}
else if($length < 6 or (6 == $length and $ace == true)) {// ,
$length++; // ,
$begin++; // ,
$i = $begin; // for $i 1 ($i++ for)
$count = 0; //
}
}
}
return $result;
}
function pokerFlush(array $arrayCards) {
$suit1 = []; //
$suit2 = []; //
$suit3 = []; //
$suit4 = []; //
foreach ($arrayCards as $arrayCard) { // 4 ,
if($arrayCard >= 2 and $arrayCard <= 14) {
$suit1 = array_merge($suit1, [$arrayCard]);
} else if($arrayCard >= 102 and $arrayCard <= 114) {
$suit2 = array_merge($suit2, [$arrayCard]);
} else if($arrayCard >= 202 and $arrayCard <= 214) {
$suit3 = array_merge($suit3, [$arrayCard]);
} else {
$suit4 = array_merge($suit4, [$arrayCard]);
}}
if(count($suit1) >= 5) { // 5
$result = straightFlush($suit1); //
if(0 == $result) {//
foreach ($suit1 as $key1 => $s1) {// 100
$suit1[$key1] = $s1 % 100;
}
rsort($suit1); //
$result = $suit1[0] * 1e+10 + $suit1[1] * 1e+8 + $suit1[2] * 1e+6 + $suit1[3] * 1e+4 + $suit1[4] * 1e+2;
}
} else if (count($suit2) >= 5) { // 5
$result = straightFlush($suit2);
if(0 == $result) {
foreach ($suit2 as $key2 => $s2) {
$suit2[$key2] = $s2 % 100;
}
rsort($suit2);
$result = $suit2[0] * 1e+10 + $suit2[1] * 1e+8 + $suit2[2] * 1e+6 + $suit2[3] * 1e+4 + $suit2[4] * 1e+2;
}
} else if (count($suit3) >= 5) { // 5
$result = straightFlush($suit3);
if(0 == $result) {
foreach ($suit3 as $key3 => $s3) {
$suit3[$key3] = $s3 % 100;
}
rsort($suit3);
$result = $suit3[0] * 1e+10 + $suit3[1] * 1e+8 + $suit3[2] * 1e+6 + $suit3[3] * 1e+4 + $suit3[4] * 1e+2;
}
} else if (count($suit4) >= 5) { // 5
$result = straightFlush($suit4);
if(0 == $result) {
foreach ($suit4 as $key4 => $s4) {
$suit4[$key4] = $s4 % 100;
}
rsort($suit4);
$result = $suit4[0] * 1e+10 + $suit4[1] * 1e+8 + $suit4[2] * 1e+6 + $suit4[3] * 1e+4 + $suit4[4] * 1e+2;
}
} else {
$result = 0;
}
return $result;
}
function straightFlush(array $arrayCards) {
$newArrayCards = [];
$ace = false;
foreach ($arrayCards as $arrayCard) {
$newArrayCards = array_merge($newArrayCards, [$arrayCard % 100]);
if (14 == $arrayCard % 100) {
$ace = true;
}
}
if ($ace == true) {
$newArrayCards = array_merge($newArrayCards, [1]);
}
rsort($newArrayCards);
$count = 0;
$length = 4;
$result = 0;
$begin = 0;
for ($i = 1; $i <= $length; $i++) {
if (-1 == ($newArrayCards[$i] - $newArrayCards[$i - 1])) {
$count++;
}
if ($length == $i) {
if (4 == $count) {
$result = $newArrayCards[$begin] * 1e+16;
} else if ((7 == count($arrayCards) and ($length < 6 or (6 == $length and $ace == true))) or
(6 == count($arrayCards) and ($length < 5 or (5 == $length and $ace == true))) or // = 6
(5 == count($arrayCards) and (5 == $length and $ace == true))) { // = 5
$length++;
$begin++;
$i = $begin;
$count = 0;
}
}
}
return $result;
}
function couple(array $arrayCards) {
$newArrayCards = [];
foreach ($arrayCards as $arrayCard) {
$newArrayCards = array_merge($newArrayCards, [$arrayCard % 100]); // 100
}
rsort($newArrayCards); //
$count1 = 0; //
$count2 = 0; //
$count3 = 0; //
$match1 = 0; //
$match2 = 0; //
$match3 = 0; //
for($i = 1; $i < count($newArrayCards); $i++) {
if ($newArrayCards[$i] == $match1 or $match1 == 0) { //
if ($newArrayCards[$i] == $newArrayCards[$i - 1]) {
$match1 = $newArrayCards[$i];
$count1++;
}
} else if ($newArrayCards[$i] == $match2 or $match2 == 0) { //
if ($newArrayCards[$i] == $newArrayCards[$i - 1]) {
$match2 = $newArrayCards[$i];
$count2++;
}
} else if ($newArrayCards[$i] == $match3 or $match3 == 0) { //
if ($newArrayCards[$i] == $newArrayCards[$i - 1]) {
$match3 = $newArrayCards[$i];
$count3++;
}
}
}
// 111 110 (2 ) 211 210 ( )
if(($count1 == 1 or $count1 == 2) and $count2 == 1 and $count3 == 1) {
$count3 = 0;
}
// 121 211 , 210 ( )
else if($count2 == 2 and $count1 == 1 and $count3 == 1) {
$support = $match2;
$match2 = $match1;
$match1 = $support;
$count1 = 2;
$count2 = 1;
$count3 = 0;
}
// 112 211 , 210 ( )
else if($count3 == 2 and $count1 == 1 and $count2 == 1) {
$support = $match3;
$match2 = $match1;
$match1 = $support;
$count1 = 2;
$count3 = 0;
}
// 220 210 ( )
else if($count1 == 2 and $count2 == 2 and $count3 == 0) {
$count2 = 1;
}
// 120 210 ( )
else if ($count1 == 1 and $count2 == 2 and $count3 == 0) {
$support = $match1;
$match1 = $match2;
$match2 = $support;
$count1 = 2;
$count2 = 1;
}
//320 300 310 300
else if($count1 == 3 and ($count2 == 2 or $count2 == 1)) {
$count2 = 0;
}
//230 320 300 130 310 300
else if($count2 == 3 and($count1 == 2 or $count1 == 1)) {
$support = $match2;
$match2 = $match1;
$match1 = $support;
$count1 = 3;
$count2 = 0;
}
//
if ($count1 == 3) {
$count1 = 1e+14;
}
//
else if ($count1 == 2 and $count2 == 1) {
$count1 = 1e+12;
$count2 = 1e+10;
}
//
else if ($count1 == 2 and $count2 == 0) {
$count1 = 1e+6;
}
//2
else if ($count1 == 1 and $count2 == 1) {
$count1 = 1e+4;
$count2 = 1e+2;
}
//
else if ($count1 == 1 and $count2 == 0) {
$count1 = 1e+2;
}
$result = $match1 * $count1 + $match2 * $count2;// $match1 $match2 0,
return $result;
}
function priority(array $arrayCards) {
//
if($arrayCards[5] % 100 > $arrayCards[6] % 100) { //, -
$highCard1 = $arrayCards[5] % 100;
$highCard2 = $arrayCards[6] % 100;
} else {
$highCard1 = $arrayCards[6] % 100;
$highCard2 = $arrayCards[5] % 100;
}
$flush = pokerFlush($arrayCards); //
$straight = straight($arrayCards); //
$couple = couple($arrayCards); //
//
if($flush >= 1e+16) {
$result = $flush; //
} else if($couple >= 1e+14 and $couple < 1e+16) {
$result = $couple; //
} else if($couple >= 1e+12 and $couple < 1e+14) {
$result = $couple; //
} else if($flush >= 1e+10) {
$result = $flush; //
} else if($straight >= 1e+8 and $straight < 1e+10) {
$result = $straight; //
} else if($couple >= 1e+6 and $couple < 1e+8) {
$result = $couple; //
} else if($couple >= 1e+4 and $couple < 1e+6) {
$result = $couple; //
} else if($couple >= 1e+2 and $couple < 1e+4) {
$result = $couple; //
} else {
$result = $highCard1 + $highCard2 * 1e-2; //
}
return $result;
}
100/12 * ($arrayCards[$i] % 100 - 2)
100/4 * floor($arrayCards[$i] / 100)