从数组中筛选键?PHP扑克


filter keys from array? php poker

现在我知道了找到直的背后的基本逻辑,我认为这将包括一个伪

   function is_straight(array $cards) {
        sort($cards);
        if(($cards[4] - $cards[0]) == 5) {
                            //Code to make sure the cards in between are increment
            //is straight.
        }
   }

理论上适用于 5 张卡支票。

但是,如何从 7 张牌阵列中消除牌以找到直牌呢?

我是否必须单独检查 7 张牌数组中的所有 5 种手牌组合?

因此,从$cards阵列中消除两张牌并检查该组合是否为直卡?

所以我有点停留在逻辑方面,而不是代码方面。

在伪代码中

#filter doubles
cards = array_unique(cards)
sort(cards)
foreach cards as key, value:    
    if not key_exists(cards, key+4):
        return false
    if cards[key+4] == value + 4:
        return true

更长可能更明确的版本

#filter doubles
cards = array_unique(cards)
sort(cards)
straight_counter = 1
foreach cards as key, value:    
    if not key_exists(cards, key+1):
        return false
    # is the following card an increment to the current one
    if cards[key+1] == value + 1:
        straight_counter++
    else:
        straight_counter = 1            
    if straight_counter == 5:
        return true
    function is_straight(array $array) {
        $alpha = array_keys(array_count_values($array));
        sort($alpha);
        if (count($alpha) > 4) {
            if (($alpha[4] - $alpha[0]) == 4) {
                $result = $alpha[4];
                return $result;
            }
            if (count($alpha) > 5) {
                if (($alpha[5] - $alpha[1]) == 4) {
                    $result = $alpha[5];
                    return $result;
                }
            }
            if (count($alpha) > 6) {
                if (($alpha[6] - $alpha[2]) == 4) {
                    $result = $alpha[6];
                    return $result;
                }
            }
        }
    }

假设$cards是一个包含从 1 到 13 的卡片值的数组,我认为您需要以 4 而不是 5 的差异进行评估:

5 - 1 = 4
6 - 2 = 4
7 - 3 = 4
等。

您还需要为 10、J、Q、K、A 添加特定逻辑

但是对于您的具体问题,那么:

function is_straight(array $cards) {
    sort($cards);
    if((($cards[4] - $cards[0]) == 4) || 
       (($cards[5] - $cards[1]) == 4) || 
       (($cards[6] - $cards[2]) == 4)) {
                        //Code to make sure the cards in between are increment
        //is straight.
    }
}