Why is my isFullHouse () method also accepting three simple options?

I have problems with my full method. I thought it was as easy as checking out three species and a pair. But with my current code, I get a full house with only three. The code for isFullHouse () isThreeOfAKind () and isPair () below thanks for all the help!

 public boolean isPair() {
     Pips[] values = new Pips[5];
     int count =0;

     //Put each cards numeric value into array
     for(int i = 0; i < cards.length; i++){
         values[i] = cards[i].getPip();
     }

     //Loop through the values. Compare each value to all values
     //If exactly two matches are made - return true
     for(int x = 1; x < values.length; x++){
         for(int y = 0; y < x; y++){
             if(values[x].equals(values[y])) count++;
         }
         if (count == 1) return true;
         count = 0;
     }
     return false;  
 }

 public boolean isThreeOfAKind() {
    Pips[] values = new Pips[5];
    int counter = 0;

    for(int i = 0; i < cards.length; i++){
        values[i] = cards[i].getPip();
    }

    //Same process as isPair(), except return true for 3 matches
    for(int x = 2; x < values.length; x++){
         for(int y = 0; y < x; y++){
             if(values[x].equals(values[y]))
                 counter++;
         }
         if(counter == 2) return true;
         counter = 0;
    }

    return false;
}

public boolean isFullHouse(){
    if(isThreeOfAKind() && isPair())
        return true;
    return false;
}
+5
source share
11 answers

Make sure the pair has a different rank than three. Otherwise, your function isPair()will find the same cards as three. Maybe like this:

public boolean isFullHouse(){
    int three = isThreeOfAKind();
    int pair = isPair();
    if (three != 0 && pair != 0 && three != pair) {
        return true;
    }
    return false;
}

(I used int, but you can change its type Pipsif you want).

+9
source

Can I suggest a way to make your logic much simpler?

partitionByRank():

public class RankSet {
    private int count;
    private Rank rank;
}

/**
 * Groups the hand into counts of cards with same rank, sorting first by
 * set size and then rank as secondary criteria
 */
public List<RankSet> partitionByRank() {
   //input e.g.: {Kh, Qs, 4s, Kd, Qs}
   //output e.g.: {[2, K], [2, Q], [1, 4]}
}

:

public boolean isFullHouse() {
    List<RankSet> sets = partitionByRank();
    return sets.length() == 2 && sets.get(0).count == 3 && sets.get(1).count() == 2;
}

public boolean isTrips() {
    //...
    return sets.length() == 3 && sets.get(0).count = 3;
}

, , , ,

+6

. , , . .

+2

( , , 2 )

- , .

if ( ((c1.rank == c2.rank == c3.rank) && (c4.rank == c5.rank)) ||
     (c1.rank == c2.rank) && (c3.rank == c4.rank == c5.rank))

ther emight (, ...

+1

: , . Soo... "", , , "" , , reset :

//Same process as isPair(), except return true for 3 matches
for(int x = 2; x < values.length; x++){
     cards[x].setCounted(true);  // by default, count the start card
     for(int y = 0; y < x; y++){
         // make sure the card isn't already counted:
         if(!cards[y].isCounted() && values[x].equals(values[y])) {
             counter++;
             cards[x].setCounted(true); // count it
         }
     }
     if(counter == 2) return true;
     counter = 0;
     // reset counted cards
     for(int z=0, zlen=values.length; z < zlen; z++) { cards[z].setCounted(false); }
}
+1

, - , . A A A 7 8, ThreeOfAKind, isPair true, ( ).

0

isPair() true, , y x.

AAA78, x = 1 y = 0, count == 1 true, . ,

if(values[x].equals(values[y]) && x != y)

- isNOfAKind(), , .

0

, - :

int[] count=new int[13];//size of all ranks
for (i=0;i<5;i++)
  count[ card[i].rank ] ++;

, , : 0 0 0 0 0 3 0 0 0 2 0 0 0 0 -. 5 : 0 0 0 0 1 1 1 1 1 0 0 0.

, , isPair() true, . true, , .

0

- #, Java :

int[] countOfRank = new int[13];
int[] countOfSuit = new int[4];
for(int i = 0; i < cards.length; i++)
{
     countOfRank[cards[i].Rank]++;
     countOfSuit[cards[i].Suit]++;
}

for (int i=0; i < countOfSuit.length; i++)
{
   isFlush = isFlush || countOfSuit[i] == 5;
}

int[] countOfTuple = new int[5];
int runLength=0;
for (int i=0; i < countOfRank.length; i++)
{
   if (countOfRank[i] == 1)
   {
      runLength++;
      isStraight = (isStraight || runLength == 5);
   }
   else
   {
      runLength=0;
   }
   countOfTuple[countOfRank[i]]++;
}
isPair = (countOfTuple[2] == 1 && countOfTuple[3] == 0);
isTwoPair = (countOfTuple[2] == 2);
isFullHouse = (countOfTuple[2] == 1 && countOfTuple[3] == 1);
isThreeOfAKind = (countOfTuple[2] == 0 && countOfTuple[3] == 1);
isFourOfAKind = (countOfTuple[4] == 1);
isStraightFlush = (isStraight && isFlush);
isStraight = (isStraight && !isStraightFlush);
isFlush = (isFlush && !isStraightFlush);
isRoyalFlush = (isStraightFlush && countOfRank[12] == 1);
isStraightFlush = (isStraightFlush && !isRoyalFlush);
0

, , , (, As, Ad, Ac, As-Ad, As-Ac Ad-Ac), - . , , . A-A-K-K-Q-Q-J ( , ) A-A-A-K-K-K-Q ( , ).

0

According to your code comments ( exactly two matcheswords), perhaps you are trying to implement the method isPairso that it returns falsein the case of three combinations of the form. If so, you need to change your isPair method to iterate over all elements of the array, for example:

//Loop through the values. Compare each value to all values
     //If exactly two matches are made - return true
     for(int x = 0; x < values.length; x++){
         for(int y = 0; y < values.length; y++){
             if(y != x && values[x].equals(values[y])) count++;
         }
         if (count == 1) return true;
         count = 0;
     }
0
source

All Articles