过滤数组的键? php扑克

时间:2013-10-05 18:56:37

标签: php

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

   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数组中删除两张牌并检查该组合是否为直线?

所以我有点卡在这个逻辑方面,而不是代码方面。

3 个答案:

答案 0 :(得分:1)

在伪代码中

#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

答案 1 :(得分:0)

假设$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.
    }
}

答案 2 :(得分:0)

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