-1

my problem is that this is sound logic but the execution is incorrect (the submit server won't take it). So I'm trying to check if my hand of 5 cards has a straight (that's 2, 3, 4, ,5 6, etc. numbers in consecutive order) and then if the fifth card is an ace i want it to evaluate as the value 10 so it'd be like 6 7 8 9 A(A has a card value of 1) and this is my current code

public static boolean hasStraight(Card [] cards) {
    boolean isTrue = false; 
    for(int atPos =0; atPos<cards.length-1; atPos++){ 
        Card ogCard = cards[atPos];  
        Card notOgCard = cards[atPos+1];

        if (ogCard.getValue() == (notOgCard.getValue()-1)){
            if ((cards[3]).getValue()==9){ 
                if (cards[4].getValue() ==1); 
                isTrue = true; //accounting for ace in last position
            }
            else if(ogCard.getValue() == (notOgCard.getValue()-1)){ 
                isTrue = true; //accounting for ace not in first position
            }
        }

    }
    return isTrue;
}

this is what I have so far not sure what's next.

emmynaki
  • 157
  • 2
  • 10
  • Is `cards` always sorted by "card value" ? – Spotted Nov 22 '16 at 06:33
  • Yes, you must sort by rank first. Then check for 23456...10JQKA (the easy cases), then do a special-case check for 2345A, which is also a straight. Where you got the idea of A being 10 is beyond me. – Lee Daniel Crocker Nov 22 '16 at 17:13

1 Answers1

1

Your code seems to be going the wrong way.

First you set isTrue to false, but then set it to true any time the array is in strictly increasing order. Thus, it will resolve as true if just the first two are 1,2. I would set it to true in the beginning, then make it false if they array is ever not in increasing order.

Your constructions of ifs and else ifs are also...interesting. The if ((cards[3]).getValue()==9){ line will most likely never run as you want it to, as ogCard.getValue() == (notOgCard.getValue()-1) will not be true (and thus the second if statement will never run) when the ace is in the last position. I would just remove the wrapping if statement, as it doesn't really test anything useful.

Your described method also doesn't handle valid aces not in the last position.

My recommendation would look something like this:

public static boolean hasStraight(Card [] cards) {
boolean isTrue = true; 
for(int atPos =0; atPos<cards.length-1; atPos++){ 
    Card ogCard = cards[atPos];  
    Card notOgCard = cards[atPos+1];

     if (! (ogCard.getValue() == (notOgCard.getValue()-1) || (ogCard.getValue()==9&&notOgCard.getValue()==1) ) ) {
       isTrue=false;
     }

}
return isTrue;
}
Ash Pera
  • 158
  • 1
  • 10