1

问题:为我的网络编程课创建一个视频扑克游戏。

除了我在下面的逻辑中遗漏了一些东西外,我还有其他一切工作。当我只有 3 个同类时,满屋返回 true。

我知道我对 3 种此类作品的逻辑。但是,当比较未涉及 3 of a Kind 的两张牌时,就会出现问题。

这是代码:

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

4 回答 4

4

只是一个建议,但您对卡片的实施似乎过于复杂。检查对子和满屋应该很容易,什么不是。

从一个名为 Card 的类开始(这是来自内存,因此可能存在语法错误等等):

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

然后你可以添加一个名为 Hand 的类,它会将结果制成表格

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

在 Hand 类中实现其余的卡片检查很容易:

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

   }
  return false;
}

等等……我不是故意写这么多代码的,抱歉,哈哈

于 2012-11-03T18:28:10.187 回答
2

作为一个完整的替代方案:

有一个 13 个整数的数组。这代表到国王的 A 数。

只旋转一次手,增加该卡值的计数。

那么对于满堂彩,数组必须包含一个 3 和一个 2。这种技术还将简化其他手牌检测和手牌比较。

于 2012-11-03T18:25:00.063 回答
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;   
}
于 2013-03-05T09:20:25.877 回答
0

循环应该从 i=3 到 i=5 吗?

// replace ...
// for($i=0; $i<5; $i++)
// with ...
for($i=3; $i<5; $i++)
于 2012-11-03T18:18:46.000 回答