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[i] = true" to "check[i] = false".
if((temp[i+1] - temp[i]) == 1) {
check[i] = true;
} else {
check[i] = false;
}
}
// Finally, see if all cards are in correct order.
for(int i = 0; i < check.Length; i++) {
if(i != check.Length) {
if(check[i]) {
isStraight = check[i];
print ("[ COMBO CHECK - Straight Flush ] --> Combo valid. INDEX " +
i + " - " + isStraight);
} else {
isStraight = false;
print ("[ COMBO CHECK - Straight Flush ] --> Combo invalid. INDEX " +
i + " - " + isStraight);
}
}
}
Answer by RudyTheDev · Sep 10, 2015 at 06:30 PM
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[i] != 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
Answer by NoseKills · Sep 10, 2015 at 03:43 PM
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[i] == previous+1 && previous != 0) {
if (++straight > 2) return true;
} else {
straight = 0;
}
return false;
}
}
On mobile. Hope that's right.
Yay! Downvote without any explanation of why this is a bad answer :) constructive and helpful.
The question doesn't mention if it's necessary but this should also handle repeated cards (3,3,4,5) and "missing/unmade selections" ( 0 ) if that's possible (0,0,1,2,3)
Yeah and the sorting isn't needed if it's done already but i wasn't 100% sure reading the question
From what I see, your code won't compile (you don't have a return
statement), and it won't produce the correct results.
The loop exits after the first iteration. So let's say return false
is meant to go after the for
loop.
previous
is never modified after the first assignment, so it always compares to the first value. So let's say there's previous = cards[i];
at the end of each iteration.
Not sure if previous != 0
is relevant in OP's case (and why not count cards from 0? or 2 if we follow ranks?) and that still allows negatives. So let's remove that for now.
You start the count at 'straight = 0;', which ignores the very first value, so 2,3,4 or 2,5,6,7 doesn't match. So let's say it's straight = 1;
because every value is the first in a straight.
Now straight>2
would indeed match 3+ consecutive cards (though OP asked for all cards). But it wouldn't match 2,3,3,4, because cards[i] == previous+1
would fail. So let's add if (cards[i] == previous) continue;
at the start of each iteration.
So this would work for partial straight search:
public bool isStraight(int[] cards)
{
if (cards.Length < 3) return false;
Array.Sort(cards);
int straight = 1;
int previous = cards[0];
for (int i = 1; i < cards.Length; i++)
{
if (cards[i] == previous) continue;
if (cards[i] == previous + 1)
{
if (++straight > 2) return true;
}
else
{
straight = 1;
}
previous = cards[i];
}
return false;
}
That's 4 errors. There's no code explanation for someone who is clearly a beginner. And the code doesn't do what OP asked.
That's more like it. Thanks for the explanation :) shouldn't do program$$anonymous$$g on a cellphone.
Your answer
Follow this Question
Related Questions
Is it possible to copy and paste something from the Hierarchy in one Unity and paste it in another? 1 Answer
Object not at 0 1 Answer
How to sort cards in order from smallest to the largest amount of strength? 0 Answers
PLEASE HELP ACTIVE SELVE NOT WORKING :) 2 Answers
Creating a dynamic animation system for a card game. 0 Answers