PHP使用算法求出最大同花顺(优化版2.0)

版权声明:gg原创,未经授权不得转载。 https://blog.csdn.net/qq_34965877/article/details/83177778

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
  );
}

猜你喜欢

转载自blog.csdn.net/qq_34965877/article/details/83177778
今日推荐