2012-11-03 35 views
1

問題:爲我的互聯網編程課程創建視頻撲克遊戲。如何檢查我的撲克遊戲中的Full House?

我有一切工作,除了我在我的邏輯下面丟失了一些東西。 當我擁有的是3種類型時,它會爲滿屋返回true。

我知道我的3種作品的邏輯。但是當比較兩種卡片中沒有涉及的3種情況時,哪裏出了問題。

下面的代碼:

//Calculate if Full House exist 
function checkHouse() 
{ 
    $kindFlag = false; 
    $pairFlag = false; 
    $tempCardValue = 0; 
    $temp = array(); 
    $counter = 0; 

    //check for 3 of a kind, save card positions so they aren't tested for a pair 
    for($i=0; $i<3; $i++) 
    { 
     for($j=($i+1); $j<4; $j++) 
     { 
      for($k=($j+1); $k<5; $k++) 
      { 
       if($this->Hand[$i]->GetSortValue() == $this->Hand[$j]->GetSortValue() && $this->Hand[$i]->GetSortValue() == $this->Hand[$k]->GetSortValue()) 
       { 
        $kindFlag = true; 
        $tempCardValue = $this->Hand[$i]->GetSortValue(); 
        break 3; 
       } 
      } 
     } 
    } 

    //Checks 2 remaining cards to see if they match 
    for($i=0; $i<5; $i++) 
    { 
     if($this->Hand[$i]->GetSortValue() != $tempCardValue) 
     { 
      $temp[$counter] = $this->Hand[$i]->GetSortValue(); 
      $counter++; 
     } 
    } 
    if($temp[0] == $temp[1]) 
    { 
     $pairFlag = true; 
    } 

    //Computes Full House or not 
    if($pairFlag && $kindFlag) 
     return true; 
    else 
     return false; 
} 
+1

不是一個答案,但哇,你已經結束了複雜化你的卡片。 – ajacian81

回答

4

只是一個建議,但你實施的卡看起來過於複雜。應該很容易檢查配對和滿屋,而不是。

啓動與一類名爲卡(這是從內存中,因此有可能是語法錯誤,什麼不可以):

class Card { 
    var $index; 
    var $suit; // 0 to 3 you can define which is what 
    var $value; // 0 to 12, aces are 12 or 0 or you can actually put their value this is just quick and dirty 
    function Card($index) { 
     $this->index = index; 
     $this->suit = index % 13; 
     $this->value = index % 4; 
    } 
} 

,然後你可以添加一個名爲手工類,它將製表結果

class Hand { 

    $values = array(); // value of cards 
    $suits = array(); 
    $cards = array(); 

    function Hand($cards) { 
     $this->cards = $cards; 
    } 
    function checkResult() { 
     foreach ($this->cards as $card) { 
       $values[$card->value]++; 
       $suits[$card->suit]++; 
     } 

    } 
    function getPairs() { 
     $pairs = array(); 
     foreach ($values as $key=>$value) { 
      if ($value == 2) 
       $pairs[] = $value; 

     } 
     return $pairs; 
    } 

    function getThreeOfAKind() { 
     $result = false; 
     foreach ($values as $key=>$value) { 
     if ($value == 3) 
      return $key; 
     } 
     return false; 
    } 

} 

,那麼你可以調用

$hand = new Hand($arrayOfCards); 
$hand->checkResult(); 
echo "This hand has this many pairs: " + count($hand->getPairs()); 
echo "Full house? " + (count($hand->getPairs()) + $hand->getThreeOfAKind !== false); 

這將會是很容易實現的C其餘在手工類ARD檢查:

function checkFlush() { 
    foreach ($this->suits as $suit=>$num) { 
    if ($num == 5) 
     return $suit; 

    } 
    return false; 
} 

等...我不是故意寫了這麼多的代碼,對不起,笑

+0

謝謝你!這有很大幫助!是的,我很想知道問題。但事實證明,我的解決方案工作,但我從我的檢查手功能調用錯誤的功能。我爲我的fullhouse調用ThreeOfaKind()的函數,而不是調用full house函數。不過,我很高興我問了這個問題,因爲現在我可以退後一步,意識到它簡單得多!再次感謝,我將不得不重寫一些類似於您爲項目第三階段所做的代碼。 – MSwezey

0

如果鎖相環的外觀從i = 3到i = 5?

// replace ... 
// for($i=0; $i<5; $i++) 
// with ... 
for($i=3; $i<5; $i++) 
2

作爲一個完整的備選:

具有13個整數的數組。這代表了王牌對王的統計。

只需旋轉一次手,即可遞增該卡片的計數值。

然後對於滿屋子,數組必須包含3和2.這個技巧還可以簡化其他手部檢測和手部比較。

0

如果您要檢查在Java中您的全房款,然後用此代碼:

public boolean fullhouse() 
    { 
     int l_iAce=0,l_iDeuce=0,l_iThree=0,l_iFour=0,l_iFive=0,l_iSix=0,l_iSeven=0,l_iEight=0,l_iNine=0,l_iTen=0,l_iJack=0,l_iQueen=0,l_iKing=0; 
     int []l_iSuit=new int[5]; 
     int []l_iRank=new int[5]; 
     for(int i=0;i< 5;i++) 
     { 
      for(int j=0;j<56;j++) 
      { 
       if(number[i] == j) 
       { 
        l_iSuit[i]=suit[j]; 
        l_iRank[i]=rank[j]; 
       } 
      } 
     } 

     for(int i=0;i< 5;i++) 
     { 

      switch(l_iRank[i]) 
      { 
       case 1: 
        l_iAce++; 
       break; 

       case 2: 
        l_iDeuce++; 
       break; 

       case 3: 
        l_iThree++; 
       break; 

       case 4: 
        l_iFour++; 
       break; 

       case 5: 
        l_iFive++; 
       break; 

       case 6: 
        l_iSix++; 
       break; 

       case 7: 
        l_iSeven++; 
       break; 

       case 8: 
        l_iEight++; 
       break; 

       case 9: 
        l_iNine++; 
       break; 

       case 10: 
        l_iTen++; 
       break; 

       case 11: 
        l_iJack++; 
       break; 

       case 12: 
        l_iQueen++; 
       break; 

       case 13: 
        l_iKing++; 
       break; 
      } 
     } 

     if(l_iAce == 3) 
     { 
      if(l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2 || l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iDeuce == 3) 
     { 
      if(l_iAce == 2 || l_iThree == 2 || l_iFour == 2 || l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iThree == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iFour == 2 || l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iFour == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iFive == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iSix == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iSeven == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iEight == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iNine == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iTen == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iJack == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iJack == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iQueen == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iQueen == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iKing== 2) 
      { 
       return true; 
      } 
     } 
     else if(l_iKing == 3) 
     { 
      if(l_iAce == 2 || l_iDeuce == 2 || l_iThree == 2 || l_iFour == 2|| l_iFive == 2|| l_iSix == 2|| l_iSeven == 2|| l_iEight == 2|| l_iNine == 2|| l_iTen == 2|| l_iJack == 2|| l_iQueen== 2) 
      { 
       return true; 
      } 
     } 

     return false; 
}