问题:为我的互联网编程课程创建视频扑克游戏。
我还有其他一切工作,除了我在下面的逻辑中遗漏了一些东西。 当我所拥有的只有3种时,它对Full House来说是真的。
我知道我对这三种作品的逻辑。但是,当比较两种不涉及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;
}
答案 0 :(得分: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;
}
等等......我不是故意写这么多代码,抱歉,大声笑
答案 1 :(得分:2)
作为完整的替代品:
拥有13个整数的数组。这代表了Aces到King的数量。
只需旋转一下手,增加该卡值的计数。
然后对于满堂红,阵列必须包含3和2.此技术还将简化其他手部检测和手部比较。
答案 2 :(得分:0)
循环应该从i = 3看到i = 5?
// replace ...
// for($i=0; $i<5; $i++)
// with ...
for($i=3; $i<5; $i++)
答案 3 :(得分: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;
}