PHP扑克德州扑克-顺子检查

问题描述 投票:0回答:1

我真的以为评估一个直线赛会比较容易,但是经过几天的尝试我会在这里。

我有一个牌组,每个牌手都得到两张牌,在评估时,我将牌手与社区牌结合在一起,并按从高到低的顺序排序。

[数组看起来像['14d', '13d', '12d', '1d', '10d', '9d', '8d'];,例如Ace钻石,King钻石,Queen钻石等。

我已经尝试了多种方法,例如循环和手动评估每张卡,但是至少需要评估5张卡(2张玩家卡+ 3张社区卡),最多7张卡(2张玩家卡+ 5张社区卡) )

这是我的尝试之一:

  $hand =  ['14d', '13d', '12d', '11d', '10d', '9d', '8d']; //Debug hand variable
  $hand_len = count($hand);
  $l_card = (int)$hand[0];

  $set = array();
  rsort($hand, SORT_NUMERIC);
  for ($i=0; $i < $hand_len; $i++) {

    //Which values to compare
    if ( $i+1 != $hand_len && $i+2 != $hand_len) {
      $c1 = (int)$hand[$i]; //14
      $c2 = (int)$hand[$i+1]; //13

      if ( $c1 == $c2+1 ) { //if 14 == 13(+1)
        array_push($set, $hand[$i]);
      }

    } elseif ($i+1 == $hand_len && $i+2 == $hand_len) {
      $c1 = (int)$hand[$i]; //8
      $c2 = (int)$hand[$i-1];//9

      if ( $c1 == $c2-1 ) { //if 8 == 9(-1)
        array_push($set, $hand[$i]);
      }
    }

  }
//Still to add evaluation for 14 (Ace) being used for low straight (eg. 14d, 2d, 3d...).

对于每张卡,我都必须转换为整数以消除西服字符,然后进行计算。我可以取得一定程度的成功,直到必须评估组中的最终卡。

例如。 ['14d', '13d', '12d', '11d', '10d', '9d', '8d'];

是否有人建议如何使它高效而可靠地运行?

谢谢!

php poker
1个回答
0
投票

您的带有“组”的方法很好,但是在某些时候您必须检查是否已达到5张卡片。您还必须检查在直道中是否有缝隙,您必须在此重新开始。而且您的代码不适用于12d12h之类的重复卡片。

为了简单起见,我从输入中删除了该诉讼,只处理数字。首先,您删除所有重复项,然后像对array_unique()rsort()一样对它们进行排序。然后,您保留一张/一组直牌的牌。在适合时,向该链中添加新卡。当最后一张卡的区别仅在于值1时,就是这种情况。当差异大于1时,您就知道它不再是直线了。此时,您必须从that位置检查是否有剩余的新顺子。请参阅以下示例源代码:

$cards = array(6, 9, 5, 12, 7, 8, 11);
echo "Input cards: ".implode(',', $cards)."\n";
$cards = array_unique($cards);
rsort($cards);

$set = array(array_shift($cards)); // start with the first card
foreach ($cards as $card) {
    $lastCard = $set[count($set)-1];
    if ($lastCard - 1 != $card) {
        // not a chain anymore, "restart" from here
        $set = array($card);
    } else {
        $set[] = $card;
    }
    if (count($set) == 5) {
        break;
    }
}
if (count($set) == 5) {
    echo "Found a straight with ".implode(',', $set)."\n";
} else {
    echo "No straight\n";
}

这将产生以下输出:

Input cards: 6,9,5,12,7,8,11
Found a straight with 9,8,7,6,5

循环遍历排序后的卡片12, 11, 9, 8, 7, 6, 5

sorted cards               set content
--------------------------------------
12, 11, 9, 8, 7, 6, 5      [12]  <-- start set
     ^                     [12, 11]
        ^                  [9]   <-- break, restart set from here
           ^               [9,8]
              ^            [9,8,7]
                 ^         [9,8,7,6] 
                    ^      [9,8,7,6,5]
© www.soinside.com 2019 - 2024. All rights reserved.