PHP使用演算法求出最大同花順(優化版2.0)
阿新 • • 發佈:2018-11-08
PHP使用演算法求出最大同花順(優化版2.0)
<?php
// $poker[0] 撲克牌花色 1~4 代表黑桃 黑桃 梅花 方塊
// $poker[1] 撲克牌數字 1~13 代表1~10 J Q K
$poker = array (
0 =>
array (
0 => 1,
1 => 2,
2 => 1,
3 => 4,
4 => 1,
5 => 3,
6 => 2
),
1 =>
array (
0 = > 1,
1 => 6,
2 => 9,
3 => 10,
4 => 11,
5 => 12,
6 => 13
)
);
$max_flush_count = 7;
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1,1],[5,6,7,8,9,10,11]];//7,8,9,10,11
var_dump(get_straight_flush($poker,$max_flush_count ));
$poker = [[1,1,1,1,1,1,1],[5,6,7,8,9,10,12]];//678910
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1,1],[3,4,7,8,9,10,11]];//7,8,9,10,11
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1,1],[2,4,5,6,7,8,10]];//4,5,6,7,8,
var_dump(get_straight_flush ($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1,1],[2,4,5,6,7,8,11]];//4,5,6,7,8,
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1,1],[1,2,4,5,6,8,11]];//1568 11 無序
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1,1],[2,4,5,6,7,9,11]];//5,6,7,9,11 無序
var_dump(get_straight_flush($poker,$max_flush_count));
$max_flush_count = 6;
$poker = [[1,1,1,1,1,1],[6,7,8,9,10,11]];//7,8,9,10,11
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1],[6,7,8,9,10,12]];//6,7,8,9,10
var_dump(get_straight_flush($poker,$max_flush_count));
// exit;
$poker = [[1,1,1,1,1,1],[5,7,8,9,10,11]];//7,8,9,10,11
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1],[5,7,8,9,11,12]];//7,8,9,11,12 無序
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1],[1,7,8,9,11,12]];//1,8,9,11,12 無序
var_dump(get_straight_flush($poker,$max_flush_count));
$max_flush_count = 5;
$poker = [[1,1,1,1,1],[7,8,9,10,11]];//7,8,9,10,11
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1],[7,8,9,10,12]];//7,8,9,10,12
var_dump(get_straight_flush($poker,$max_flush_count));
$poker = [[1,1,1,1,1,1],[1,7,8,9,10]];//1,7,8,9,10 無序
var_dump(get_straight_flush($poker,$max_flush_count));
/**
* [get_straight_flush 統計出同花順]
* @param [type] $result_poker [玩家撲克牌]
* @param [type] $max_flush_count [最大同花數量]
*/
function get_straight_flush($result_poker,$max_flush_count)
{
// 同花大順 10
// 同花順 9
// 同花 6
// 順子 5
$king = [10,11,12,13,1];
$res = array_intersect($king,$result_poker[1]);
// var_dump($res);exit;
if(count($res) == 5){
//查詢有沒有10 J Q K A
$poker_level = 10;
$result_poker[1] = $king;
$result_poker[0][] = $result_poker[0][0];
unset($result_poker[0][0]);
unset($result_poker[0][1]);
unset($result_poker[0][2]);
}else if($result_poker[1][0] == 1 && $result_poker[1][4] == 5){
// 12345 查詢有沒有12345
$poker_level = 10;
unset($result_poker[0][5]);
unset($result_poker[0][6]);
unset($result_poker[1][5]);
unset($result_poker[1][6]);
}else{
//其餘的都是普通的順子 或者不是
// 7
// 1~5 下標:0~4 要刪除下標:5,6
// 2~6 下標:1~5 要刪除下標:0,6
// 3~7 下標:2~6 要刪除下標:0,1
// 6
// 1~5
// 2~6 下標:1~5 要刪除下標:0
// 5
// 1~5
$poker_level = 6;//牌的等級 默認同花
$start = $max_flush_count - 5;
for ($i=$start; $i >= 0; $i--) {
// var_dump($result_poker[1][$i+4] - $result_poker[1][$i]);
if($result_poker[1][$i+4] - $result_poker[1][$i] == 4){
$poker_level = 9;//牌的等級
foreach ($result_poker[1] as $k => $v) {
if($k < $i || $k > $i+4){
unset($result_poker[0][$k]);
unset($result_poker[1][$k]);
}
}
break;
}
}
//不是順子 說明都是單數 求出最大牌
if($poker_level == 6){
if($max_flush_count > 5){
foreach ($result_poker[1] as $k => $v) {
if(!empty($result_poker[1][0]) && $result_poker[1][0] == 1){
// 7 12
// 6 1
if($k !== 0 && $k <= $max_flush_count - 5){
unset($result_poker[0][$k]);
unset($result_poker[1][$k]);
}
}else{
//7 01 $max_flush_count - 5 == 2
//6 0 $max_flush_count - 5 == 1
if($k < $max_flush_count - 5){
unset($result_poker[0][$k]);
unset($result_poker[1][$k]);
}
}
}
}
}
}
return array(
'poker_level' => $poker_level,
'result_poker' => $result_poker
);
}