# Finding a 'straight' in poker.

This is a discussion on Finding a 'straight' in poker. within the C Programming forums, part of the General Programming Boards category; You are welcome to report any futher bugs...

1. You are welcome to report any futher bugs

2. Originally Posted by esbo
You are welcome to report any futher bugs
Why to bother if you do not bother to fix the already found?

3. Final version:-
Code:
```#include <stdio.h>
int c[7];
int a,b,x,d,e;
main(argc,argv)	int  argc;	char *argv[]; {
c[0]=4;c[1]=12;c[2]=3;c[3]=2;c[4]=1;c[5]=5;c[6]=9;
for (a=0; a<7; a++){
if (c[a]>10) continue;
for (b=0; b<7; b++){//first card in straight
if (c[b]==c[a]+1) {//second
for (x=0; x<7; x++){
if (c[x]==(c[b])+1) {//third
for (d=0; d<7; d++){
if (c[d]==c[x]+1) {//fourth
for (e=0; e<7; e++){
if ((c[e]==c[d]+1) || (c[d]==12 && c[d]+1==1)) {//fifth
printf("\n Straight up"); //fifth includes 9,10,11,12,1
}
}
}
}
}
}
}
}
}
for (a=0; a<7; a++){
if ((c[a]<5 ) && (c[a]!=1)) continue;
if (c[a]==1) c[a]=13;
for (b=0; b<7; b++){//first
if (c[b]==c[a]-1) {//second
for (x=0; x<7; x++){
if (c[x]==c[b]-1) {//third
for (d=0; d<7; d++){
if (c[d]==c[x]-1) {//fourth
for (e=0; e<7; e++){
if (c[e]==c[d]-1)  {//fifth
printf("\n Straight down");
}
}
}
}
}
}
}
}
}
}/*main */```

4. Originally Posted by vart
Why to bother if you do not bother to fix the already found?
Well I have fixed 90% of them

5. Originally Posted by vart
Have you ever played cards? Because if yes - it seems to me you have something different from the regular human mind

Yes I play poker a lot. I used to make a good profit but it gets harder as the players get better. You have to be 5% better than the opposition to make a profit, which is not easy
when a large part of the game is luck.
Also you might actually be playing against a computer program.
Futhermore players at the table may be cheating or 'insiders' maybe cheating.

http://www.associatedcontent.com/art..._cheating.html

And he is just the guy who got caught, how many other sites are cheating??

At the moment I can just about break even depending upon how hard I try.

If you are playing against other good players you might as well forget it, you will never
be more than 5% better than them, the poker site is the only winner then as it takes
5% of every pot.

I used to make \$200 a month regular as clockwork - not so easy now!!!

Most of the bad player have probaly realised they are wasting their money and quit!!

6. c[d]==12 && c[d]+1==1
you still do not understand what you are doing. if c[d] is 12 then c[d]+1 is 13 and cannot be 1

7. Originally Posted by vart
c[d]==12 && c[d]+1==1
you still do not understand what you are doing. if c[d] is 12 then c[d]+1 is 13 and cannot be 1
OK, thank you, point taken, I will sort it out later.

Should be
c[d]==12 && c[e]=1
or something like that I guess.
It did seem to work though!!

You can never do enough testing

Thanks!!

8. Originally Posted by vart
Have you ever played cards? Because if yes - it seems to me you have something different from the regular human mind
Actually I think when the human mind looks for a straight what if does first is to look
for a card between 5 an 10.
If there is no such card there is no straight!!!

Which is really clever don't you think!!

I mean I can see almost instantly if there is a straight but I know my mind does not
actually sort the cards first.
It is amazing what your mind can do without you realising it!!

Then it works outwards from those cards I think.

I would also say the way the mind does it is in the most effiecient way possible, although
I cannot prove that (yet!).

It probabaly has a few more tricks up it's sleeve that I am not aware of yet!!!

9. Originally Posted by esbo
Actually I think when the human mind looks for a straight what if does first is to look
for a card between 5 an 10.
If there is no such card there is no straight!!!

Which is really clever don't you think!!

I mean I can see almost instantly if there is a straight but I know my mind does not
actually sort the cards first.
It is amazing what your mind can do without you realising it!!

Then it works outwards from those cards I think.

I would also say the way the mind does it is in the most effiecient way possible, although
I cannot prove that (yet!).

It probabaly has a few more tricks up it's sleeve that I am not aware of yet!!!
To find those mind tricks, you have to add more double, triple, and quadruple exclamation points, to your posts. That's the secret.

10. Originally Posted by esbo
Probably would be better to sort if first but then I also have to consider the suit of each card
held in a seperate array to test for straight flushs, that would also have to sorted in the same
fashion. Hence it might be easier overall if I fo it this way.
Rather than using two arrays for the number & suit, another option is you could define the deck as 0-51, then to find the value of one of the cards do:
Code:
`value = (card % 4) + 1;`
and to find the suit do:
Code:
`suit = card / 13;`
Either that or create a struct that contains the card value & suit...

11. Man, that is some of the most convoluted 'feo' code i ever saw, those indents boggle the mind, good job is only short,
i think you have been overworking this code + problem, you will get a cleaner and more expandable result if you have a good think about the actual algorithm, try just using 2loops to compare the contents of the array with the remaining numbers one at a time, or just use qsort function

12. Originally Posted by rogster001
Man, that is some of the most convoluted 'feo' code i ever saw, those indents boggle the mind, good job is only short,
i think you have been overworking this code + problem, you will get a cleaner and more expandable result if you have a good think about the actual algorithm, try just using 2loops to compare the contents of the array with the remaining numbers one at a time, or just use qsort function

You are welcome to post you own better version........ (holds breath)

13. You are welcome to post you own better version........ (holds breath)

Code:
```#include <stdio.h>

#define NUMBER_OF_FACES 13
#define CARDS_IN_HAND 7

int Rank[NUMBER_OF_FACES];

void CheckForStraight(void)
{
int iNumberConsecutive = 0;
int iRank  = 0;
while (Rank[iRank] == 0) iRank++;
for (; iRank < NUMBER_OF_FACES && Rank[iRank]; iRank++)
iNumberConsecutive++;
if (iNumberConsecutive == CARDS_IN_HAND)
printf("Straight up!\n");
else printf("NO straight!\n");
}

int main(void)
{
// Where iIndex is rank value,
// Zero is an ace, 1 is a deuce, 2 is trey... 12 is a king
for( int iIndex = 0; iIndex < 7; iIndex++)
Rank[iIndex]++;
CheckForStraight();
return 0;
}```

14. Originally Posted by BobS0327

Code:
```#include <stdio.h>

#define NUMBER_OF_FACES 13
#define CARDS_IN_HAND 7

int Rank[NUMBER_OF_FACES];

void CheckForStraight(void)
{
int iNumberConsecutive = 0;
int iRank  = 0;
while (Rank[iRank] == 0) iRank++;
for (; iRank < NUMBER_OF_FACES && Rank[iRank]; iRank++)
iNumberConsecutive++;
if (iNumberConsecutive == CARDS_IN_HAND)
printf("Straight up!\n");
else printf("NO straight!\n");
}

int main(void)
{
// Where iIndex is rank value,
// Zero is an ace, 1 is a deuce, 2 is trey... 12 is a king
for( int iIndex = 0; iIndex < 7; iIndex++)
Rank[iIndex]++;
CheckForStraight();
return 0;
}```
That does not seem to work at all.
All you are doing is seting the first 7 cards to ace
then counting the number you set and seeing if
still 7, which it is unsurprisingly.

15. How about this one? As a bonus you get to know where in the array the straight starts so you can look up the card value and - for example - compare the result against another hand containing a straight. The function returns the highest value if there are several straights.

In addition your O(n!) or whatever complexity and gazillion nested loops are reduced nicely to O(n).

Sorting the hand before is trivial.

Code:
```#include <stdio.h>
#include <assert.h>

#define CARD_COUNT 7
#define ACE 1
#define MAX_CARD 13
/*
expects cards to be in ascending order
returns the index from which the straight starts
or -1 if no straight
*/
int check_for_straight(const int* cards, int straight_size)
{
int i = CARD_COUNT - 2;
int consequtives = 1;

/*sanity checks for debugging*/
assert(straight_size > 1);
assert(CARD_COUNT >= straight_size);

for (; i >= 0; --i ) {
if (cards[i] == cards[i+1] - 1) {
++consequtives;
}
else if (cards[i] != cards[i+1]) {
/* i.e difference is more than 1 */
consequtives = 1;
}
if (
/* if upper straight(?) -
special case: ace is considered largest */
(consequtives == straight_size - 1
&& cards[0] == ACE
&& cards[i] == MAX_CARD - straight_size + 2)
/* or simple straight */
|| consequtives == straight_size) {
return i;
}
}
return -1; /*no success*/
}

int main(void)
{
int cards[CARD_COUNT] = {3, 3, 4, 5, 6, 7, 12};
int straight = check_for_straight(cards, 5);
if (straight == -1) {
printf("No straight\n");
}
else {
printf("Straight starting from card "
"with value &#37;d and index %d\n", cards[straight], straight);
}
return 0;
}```
By the way, how comes you have 7 cards in poker?

Page 2 of 4 First 1234 Last