Search code examples
javaconsole-applicationpoker

Texas Hold'em poker recognizing full house


I'm creating a console texas hold'em poker. I'm already done making this game, everything works as supposed, expect for a full house for which I became undecided for a best way to write a code.

This is how I present cards: "D5", "S2", "SA"... I know it is a bad idea of representing cards, but I'm currently not thinking in OOP way, I'm actually playing around with indexes, which is a good code practice.

So, the problem isn't how to write a pair or three of a kind, I actually had a great idea to do something like this...

if (isPair() && isThreeOfKind()) {
   //
}

But it is impossible, because I'm dealing with a problem (for which I'm here), isPair() and isThreeOfAKind() will find a same card, let's say "DA", "CA", "SA", so we have a pair of "DA" and "CA", but also "DA", "CA", "SA" which stays for a three of a kind.

code update:

public boolean isPair(int playerIndex) {
        boolean isPair = false;

        if (hasSameRank(playerAndHand[playerIndex])) {
            isPair = true;
        } else {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                for (int j = 0; j < HAND_CARDS_LENGTH; j++) {
                    if (playerAndHand[playerIndex][j].charAt(1) == tableCards[i].charAt(1)) {
                        isPair = true;
                        break;
                    }
                }
                if (isPair) break; 
            }
        }
        return isPair;
    }

public boolean isThreeOfKind(int playerIndex) {
        boolean isThreeOfKind = false;

        // 2 from player hand 1 from table
        if (hasSameRank(playerAndHand[playerIndex])) {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                    isThreeOfKind = true;
                    break;
                }
            }
        } else {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                // first card in player hand and 2 more on table
                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                    for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                        if (j != i) {
                            if (playerAndHand[playerIndex][0].charAt(1) == tableCards[j].charAt(1)) {
                                isThreeOfKind = true;
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (isThreeOfKind) break;
                    // second card in player hand and 2 more on table   
                } else if (playerAndHand[playerIndex][1].charAt(1) == tableCards[i].charAt(1)) {
                    for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                        if (j != i) {
                            if (playerAndHand[playerIndex][1].charAt(1) == tableCards[j].charAt(1)) {
                                isThreeOfKind = true;
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (isThreeOfKind) break;
                }                   
            }
        }
        return isThreeOfKind;
    }

Solution

  • Get isThreeOfKind to return the card value or blank char if no three of kind. Then isPair should accept a card value to ignore. So, your check for a Full House becomes isPair(playerIndex, isThreeOfKind(playerIndex)).

    Note, your normal Three Of A Kind check should now be if (isThreeOfKind(playerIndex)!='') then it is three of a kind. Your normal Is Pair check becomes if (isPair(playerIndex,'')) then it is a pair.

    Example:

    public boolean isPair(int playerIndex, char charToIgnore) {
            boolean isPair = false;
    
            if (hasSameRank(playerAndHand[playerIndex])) {
                isPair = true;
            } else {
                for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                    if (tableCards[i].charAt(1) == charToIgnore) continue;
                    for (int j = 0; j < HAND_CARDS_LENGTH; j++) {                        
                        if (playerAndHand[playerIndex][j].charAt(1) == tableCards[i].charAt(1)) {
                            isPair = true;
                            break;
                        }
                    }
                    if (isPair) break; 
                }
            }
            return isPair;
        }
    
    public char isThreeOfKind(int playerIndex) {
            boolean isThreeOfKind = false;
            char cardValue = '';
    
            // 2 from player hand 1 from table
            if (hasSameRank(playerAndHand[playerIndex])) {
                for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                    if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                        cardValue = tableCards[i].charAt(1);
                        isThreeOfKind = true;
                        break;
                    }
                }
            } else {
                for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                    // first card in player hand and 2 more on table
                    if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                        for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                            if (j != i) {
                                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[j].charAt(1)) {
                                    cardValue = tableCards[j].charAt(1);
                                    isThreeOfKind = true;
                                    break;
                                }
                            } else {
                                continue;
                            }
                        }
                        if (isThreeOfKind) break;
                        // second card in player hand and 2 more on table   
                    } else if (playerAndHand[playerIndex][1].charAt(1) == tableCards[i].charAt(1)) {
                        for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                            if (j != i) {
                                if (playerAndHand[playerIndex][1].charAt(1) == tableCards[j].charAt(1)) {
                                    cardValue = tableCards[j].charAt(1);
                                    isThreeOfKind = true;
                                    break;
                                }
                            } else {
                                continue;
                            }
                        }
                        if (isThreeOfKind) break;
                    }                   
                }
            }
            return cardValue;
        }