Domanda

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

Soluzione

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

Altri suggerimenti

if(isThreeOfKind() && cardTypes() ==2 && !(isFourOfKind()))

... because a full house only has 2 different values (f. ex A A A 7 7)

A "naive" poker hand evaluator like this (that is, one that matches each hand type individually) should test for hands in this order: straight flushes, then flushes, straights, quads, full houses, then trips, then two pair, and finally single pairs. You should return from the function when you find one when you do them in that order. Sorting the hand by rank makes it much easier. Also, using text for your internal representation is a bad idea. Check out my essay on the subject: Representing Cards in Software. There are also lots of links in that essay to more sophisticated poker code, including my own (which has a Java binding).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top