4

This is not a duplicate. I understand how easy this is if you can sort, but I am not allowed to use the sort method for arrays and I am not allowed to write my own. I can't find any help with this anywhere, including StackOverflow.

In this scenario, I have a method that is supposed to check whether or not a five card hand is a straight. I have a card object that holds a value (integer) and a suit (integer). I also have some rules about implementing this method.

  • There are no face cards besides the ace
    • The ace can count as either a 1 or a 10 but not both
  • A straight cannot wrap around
  • You cannot use the sorting method for arrays or write your own sorting method

That last rule is what is killing me. I have an array of cards. If I could just sort it, this would be so easy but I can't even write my own method to sort the array. For my other methods it simple to iterate through the hand and store the information about the hand in two separate arrays like this:

private static int[] cardValues = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
private static int[] cardSuits = {0, 0, 0, 0, 0};

private static void evaluationHelper(Card[] cards) {
    for (int i = 0; i < cardValues.length; i++) {
        cardValues[i] = 0;
    }

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

    for (int i = 0; i < 5; i++) {
        cardValues[cards[i].getValue() - 1]++;
        cardSuits[cards[i].getSuit()]++;

        if (cards[i].getValue() == 1) {
            cardValues[9]++;
        }
    }
}

So in my first attempt to solve this issue I tried something like this:

public static boolean hasStraight(Card [] cards) {
    int sequenCounter = 0;
    evaluationHelper(cards);

    for (int i = 0; i < cardValues.length; i++) {
        if (sequenCounter != 5) {
            if (cardValues[i] != 0) {
                sequenCounter++;
            } else {
                sequenCounter = 0;
            }
        } else {
            return true;
        }
    }
    return false;
}

That didn't work. So then I tried this:

public static boolean hasStraight(Card [] cards) {
    int min = 100, max = 0;
    boolean seenSix = false, seenAce = false;
    evaluationHelper(cards);

    for (int i = 0; i < cards.length; i++) {
        if (cards[i].getValue() > max) {
            max = cards[i].getValue();
        }

        if (cards[i].getValue() < min) {
            min = cards[i].getValue();
        }

        if (cards[i].getValue() == 6) {
            seenSix = true;
        }

        if (cards[i].getValue() == 1) {
            seenAce = true;
        }
    }

    if (seenSix && seenAce) {
        max = 10;
    }

    if (max - min == 4) {
        return true;
    }
    return false;
}

That doesn't work either. I'm getting frustrated as both of these attempts went through many different changes over the course of overs and nothing has worked. I can't even figure out why they aren't working. The only information I have is that this method isn't spitting out the correct value. I don't know what values are being passed to the method. I don't know what the method is spitting out during the test. It could be spitting out false when it's supposed to be spitting true or vice versa. Please help!

user2837136
  • 71
  • 1
  • 2
  • 6
  • Why are you "not allowed" to sort values? Can you make a copy of the hand and sort that? – markspace Nov 18 '17 at 02:48
  • 1
    Use bit masks, Luke – Iłya Bursov Nov 18 '17 at 02:48
  • Or even just an array of bytes or ints. But it's a weird problem nevertheless. – markspace Nov 18 '17 at 02:50
  • You just need to put some non zero value to `cardValues` array for the index the `Card` value represents minus one (it should be from 0 to 9). For card with value 1 it will go to index 0, for 9 it will go to index 8 and for Ace card put it to both 0 and 9 index. Then iterate over the `cardValues` and make sure that you will have 5 non zero sequences. – tsolakp Nov 18 '17 at 03:20
  • here is my implementation for the same task: https://github.com/brotherla/PokerHandAnalyzer/blob/master/PokerHandAnalyzer/src/com/lwg/poker/HandCategoryAnalyzer.java – Iłya Bursov Feb 15 '18 at 05:38

3 Answers3

1

In your evaluationHelper you forgot to address when an ace is 10 adding:

if (cards[i].getValue() == 10) {
    cardValues[0]++;
}

will make at least your first solution work (I haven't checked the second).

Note that what this method does is still a form of Radix sort so I'm not sure if it satisfies your requirement.

Oleg
  • 6,124
  • 2
  • 23
  • 40
  • I believe the evaluationHelper method was provided to him so that he could use its results to perform his logic. The Ace is already addressed by the increment of cardValues[9]. – robertf Nov 18 '17 at 05:15
  • I don't think so but maybe. It's only partially addressed. – Oleg Nov 18 '17 at 05:22
  • This was the closest answer. I forgot in cardValues I was storing each card value at the index position before the value. Turns out I was storing the high value of the ace (10) in the 10th position instead of the 9th position. – user2837136 Nov 20 '17 at 00:04
0

Rules:

  • max_value - min_value must be 4
  • Other 3 cards' value must be between min and max
  • All 5 cards must have the same suit

Assumes there are no duplicates of a suit-number.

If none of these rules are violate then you have a straight.

The only tricky thing if the Ace. Ace can be a 10? What about the number 10 card? Is Ace 1 or 10, or 1 or 11?

PatrickSJ
  • 510
  • 5
  • 13
0

I assume you understand the purpose of the evaluationHelper() method; if not, I suggest you view the contents of the two arrays after it is run.

You need to find the first non-zero value in the cardValues[] array. If that entry and the subsequent 4 are all one, then return true; else return false.

In the future if you need to find a straight flush, you would first confirm one of the cardSuit[] entries equals 5 (i.e. all 5 must be the same suit); if not, then return false.

robertf
  • 237
  • 1
  • 11
  • 1
    A straight is 5 cards in sequence, not 5 of the same suit. That's a flush. – Herb Nov 19 '17 at 04:38
  • @Herb Good point. I saw the cardSuits array in the evaluationHelper() and somehow got a straight flush in my head. Answer has been updated. – robertf Nov 19 '17 at 18:00