我有这个功能,如果手牌正好是5张牌,可以直接在扑克中检测到,但不确定如果在7张牌中找到一张牌。
public function straight($hand) {
sort($hand);
if ($hand == range($hand[0], $hand[count($hand)-1])) {
$this->hand_name = 'straight';
}
}
我们假设数组只是一串数字。所以这些都是理想的结果。
2 3 5 7 12 7 4 - no straight
2 3 5 6 12 7 4 - straight
7 8 9 10 11 11 11 - straight
2 4 5 6 14 10 9 - no straight
让我们不要担心Ace现在是1或14问题,我可以解决这个问题,但是这个功能应该允许重复,因为这就是卡片的方式。
还要注意我只需要确定是否有五张卡片是直接的....我不需要涉及卡片或将它们与另一张卡片进行比较等。
编辑:我还应该指出,它当然应该在6张卡或5张卡中直接检测5张牌,但显然不是4张。
您可以简单地修改现有函数,以便在排序后对7个组(使用array_slice
创建)进行迭代:
function straight($hand) {
sort($hand);
for ($i = 0; $i <= count($hand) - 5; $i++) {
$subhand = array_slice($hand, $i, 5);
if ($subhand == range($subhand[0], $subhand[count($subhand)-1])) {
echo implode(',' , $hand) . " => straight\n";
break;
}
}
}
straight(array(2, 3, 5, 7, 12, 7, 4));
straight(array(2, 3, 5, 6, 12, 7, 4));
straight(array(7, 8, 9, 10, 11, 11, 11));
straight(array(2, 4, 5, 6, 14, 10, 9));
straight(array(2, 3, 5, 7, 7, 4));
straight(array(3, 5, 6, 12, 7, 4));
straight(array(7, 8, 9, 10, 11));
straight(array(2, 4, 5, 6, 3));
输出:
2,3,4,5,6,7,12 => straight
7,8,9,10,11,11,11 => straight
3,4,5,6,7,12 => straight
7,8,9,10,11 => straight
2,3,4,5,6 => straight