Domanda

Problem: Creating a video poker game for my internet programming class.

I have everything else working except I'm missing something in my logic down below. It returns true for Full House when all I have is 3 of a Kind.

I know my logic for the 3 of the kind works. But when comparing the two cards not involved in the 3 of a Kind is where something is wrong.

Here's the code:

//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;
}
È stato utile?

Soluzione

Just a suggestion, but you're implementation of the cards seems overly complicated. It should be easy to do a check for pairs and fullhouses, and what not.

start with a class called Card (this is from memory so there may be syntax errors and what not):

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

and then you can add a class called Hand and it would tabulate the result

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

}

then you can call

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

It'd be easy to implement the rest of the card checks in the Hand class:

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

   }
  return false;
}

etc... I didn't mean to write so much code, sorry, lol

Altri suggerimenti

As a complete alterative:

Have an array of 13 ints. This represents the count of Aces through to King.

Spin through the hand just once, increment the count for that cards value.

Then for full house, the array must contain a 3 and a 2. This techique will also simplify other hand detection and hand comparison.

should the loop look from i=3 to i=5?

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

If you want to check your full house payment in java, then use this code:

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;   
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top