为什么我的isFullHouse()方法也接受一个简单的三种类型?

LKA*_*ANL 5 java poker

我的全屋方法有问题.我认为这就像检查三种和一对一样简单.但是根据我目前的代码,我得到了一个只有三种类型的满堂红.isFullHouse()的代码isThreeOfAKind()和isPair()在下面感谢所有的帮助!

 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;
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 9

检查以确保该对的排名与三种排名不同.否则,您的isPair()功能将找到与三种卡相同的卡.也许是这样的:

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

(我用过int,但Pips如果你愿意,可以改用你的类型.)


Mar*_*ers 6

我可以建议一种使逻辑变得更简单的方法吗?

考虑一个名为的辅助方法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]}
}
Run Code Online (Sandbox Code Playgroud)

获得手的类型非常简单:

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;
}
Run Code Online (Sandbox Code Playgroud)

当你不可避免地需要检查一对是否比另一对更大时,这也会有所帮助,例如