Detecting Correct Numerical Order For Detecting Straight Flush

I am currently debugging and testing for my card game. The goal of this task is to see if all card number has to be in exact order and the number of cards picked must be at least 3. Not relatively in order but exact order without skipping, regardless what card number started. In case if all numbers picked in the wrong order, I stored it in a List object and using the Sort() method to automatically arranged in order numerically and check again if all the numbers stored are arrange in order exactly.

Let’s say here is what I am expecting the output result when computing the correct order:

Example 1
CARD INPUT CHOSEN: 1, 2, 3, 4
RESULT: true
REASON: All numbers are in exact order and at least 3 cards have met.

Example 2
CARD INPUT: 1, 2, 5, 6
RESULT: false
REASON: All numbers are in order. However, no. 3 and no. 4 is skipped.

Example 3
CARD INPUT: 4, 5
RESULT: false
REASON: All numbers are in exact order. However, player picked only two cards.

Example 4
CARD INPUT CHOSEN: 3, 4, 5
RESULT: true
REASON: Same as "Example 1".

These examples above are my expectations of the result. Now, for reality, when I tried, run, and tested the program and I got this error like this.

CARD INPUT: 3, 4, 5
RESULT: false

Say no. 3, no. 4, and no. 5 and arranged in that order are supposed to be valid. Technically, the reason I got this bug and I’m still figuring out yet for an alternate solution in order to check for exact order. Here is the code I’ve used in C# and see comments for details:

// See if the current card value deducted by next card value always 1 for every index.
for(int i = 0; i < temp.Length; i++) {

	//    The problem with this if-else condition is that the last array of the list  
	// and checking this index beyond the last resulted in an array index error. 
	// To remedy this index out of bounds error, the condition result jumped 
	// from "check _= true" to "check *= false".*_

_ if((temp[i+1] - temp*) == 1) {*_

_ check = true;_

* } else {*

_ check = false;_

* }*

}

// Finally, see if all cards are in correct order.
for(int i = 0; i < check.Length; i++) {

* if(i != check.Length) {*

_ if(check*) {*_

_ isStraight = check*;
print ("[ COMBO CHECK - Straight Flush ] → Combo valid. INDEX " +
i + " - " + isStraight);*_

* } else {*

* isStraight = false;*
* print ("[ COMBO CHECK - Straight Flush ] → Combo invalid. INDEX " +*
* i + " - " + isStraight);*

* }*

* }*

}

You get index range exception because your loop goes up to temp.Length. You should have it to temp.Length-1. After all, once you compare last card to previous card, you don’t need to go to last card and compare it to… well there’s nothing left.

A simple way (only your mentioned constraints) would be:

private bool IsStraight(int[] ints)
{
    // Less than 3 cards cannot form a straight
    if (ints.Length < 3)
        return false;

    // Check that every card is followed by a card exactly 1 rank higher
    for (int i = 0; i < ints.Length - 1; i++)
        if (ints[i + 1] - ints *!= 1)*

return false;
// No problems with card ranking, so we are good
return true;
}
This is exactly the logic you had, but the loop runs 1 less iteration. It’s also a more readable method. This returns:
IsStraight(new int[] { 2, 3, 4, 5 }); // true
IsStraight(new int[] { 2, 3, 5, 6 }); // false
IsStraight(new int[] { 5, 4, 3, 2 }); // false
IsStraight(new int[] { 2, 3, 4 }); // true
IsStraight(new int[] { 2, 3 }); // false

Maybe

public bool isStraight(int[] cards) {
    if (cards.Length<3) return false;

    Array.Sort(cards);
    int straight = 0;
    int previous = cards[0];
    for (int i=1; i < cards.Length; i++){
        if (cards *== previous+1 && previous != 0) {*

if (++straight > 2) return true;
} else {
straight = 0;
}
return false;
}
}
On mobile. Hope that’s right.