2013-10-05 35 views
3

現在我知道後面找到一個筆直的基本邏輯,我認爲將包括從數組中篩選鍵? 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數組中排除兩張卡並檢查該組合是否是直的?

所以我有點卡在邏輯上,而不是代碼方面。

+0

可能的重複http://stackoverflow.com/questions/8696761/php-array-poker-hand-result?rq=1。 –

+1

他的問題是基於5張牌的手評價,我的基礎是7張。 –

+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 
+0

出色的工作,偉大的邏輯在這件事上。謝謝 –

0

假設$cards是含有卡值的數組從1至13,我想你需要的差,以評估4,not 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. 
    } 
} 
+0

我終於弄清楚了,4是最合適的數字,而且編碼方式與您提供的方式非常相似。感謝您花時間回答我。 –

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