1

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;
    }
Johannes
  • 113
  • 5
  • 3
    Put the cards into a list, find the three-of-a-kind and remove them, then check the other two. It should be simple if you've already got the three-of-a-kind finder working. Kind of depends on how you're organizing and implementing them, I suppose. – Reinstate Monica -- notmaynard May 31 '13 at 21:39
  • 2
    If you want to show more code, we might be able to give a better recommendation. – Reinstate Monica -- notmaynard May 31 '13 at 21:40
  • To that end, you may want to refactor your `isPair()` and `isThreeOfAKind()` methods so that they operate on _arbitrary collections of cards_. Something like `boolean isPair(List cards)` assuming you had a `Card` class (which would be a great idea btw). – Shrike May 31 '13 at 22:02
  • Here is how pair and three of a kind looks like. Actually idea with list sounds cool.. but still what do you think ? – Johannes May 31 '13 at 22:02
  • unfortunately not, i don't have a card class, this is more like procedural programming – Johannes May 31 '13 at 22:08
  • Then you'd probably have to operate on `List` or even `String[]`. Either way, if you can operate on arbitrary lists of cards then implementing iamnotmaynard's solution would be much easier. – Shrike May 31 '13 at 22:12
  • Please show the code where you check for a full house. – xagyg Jun 01 '13 at 02:11

3 Answers3

0

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

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

Gary Klasen
  • 1,001
  • 1
  • 12
  • 30
0

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).

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
0

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;
    }
xagyg
  • 9,562
  • 2
  • 32
  • 29