# 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 have 7 cards you need to determine if there is a 5 card straight there, that is 5 cards ...

1. ## Finding a 'straight' in poker.

You have 7 cards you need to determine if there is a 5 card straight there, that is 5 cards
in a sequence eg 45678, of any suit. You cannot have QKA12 for example as the ace can
only be at start or the end.

What is the quickest way to do it?

You can assume the cards are in an array cards[7] where 2
1=Ace
2=2
3=3
...
10=10
11=Jack
12=Queen
13=King

So you have

int c[7]; // array of unsorted cards.

How would you do it?

2. sort it

3. Sort the cards, then check for 5 consecutive numbers in a row.
If the 2nd card != 1st card + 1, strike one.
If the 3rd card != 2nd card + 1, strike two.
If the 1st card is not an ace or the other 4 cards aren't consecutive, strike three.

4. OK I was thinking I could do something like this:-
There are two types of straights, ascending ones which start at 10 or lower, the first 'bit',
and descending ones which start at 5 or above.
Not sure if it compiles or work yet, but probably not too far off.
I will try that later.
Can't be too far off and it does the fiddly testing without sorting it first.
May be quicker than sorting first.
I have just realised, there could be several straights eg 1,2,3,4,5 and 2,3,4,5,6 etc...

Code:
```int c[7];
int a,b,c,d,e;

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 (c=0; c<7; c++){
if (c[c]]==c[b]+1) {//third

for (d=0; d<7; d++){
if (c[d]]==c[c]+1) {//fourth

for (e=0; e<7; e++){
if ((c[e]]==c[d]+1) || (c[d]==12 && c[d]=1)) {//fifth
printf("\n Straight"); //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 (c=0; c<7; c++){
if (c[c]==c[b]-1) {//third

for (d=0; d<7; d++){
if (c[d]==c[c]-1) {//fourth

for (e=0; e<7; e++){
if (c[e]==c[d]-1)  {//fifth
printf("\n Straight");
}
}
}
}
}
}
}
}
}```

5. of course it will not compile

6. 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.

7. Originally Posted by vart
of course it will not compile
If it compiles first time I know I have compiled the wrong program!!!

8. Originally Posted by vart
of course it will not compile

No but this does
Might be the odd bug in it though

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

#include <io.h>
#include <fcntl.h>
#include <errno.h>
int c[7];
int a,b,x,d,e;

main(argc,argv)
int  argc;
char *argv[];
{

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)) {//fifth
printf("\n Straight"); //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");
}
}
}
}
}
}
}
}
}

}/*main */```

9. only #include <stdio.h> is used

main should be declared as

int main(int argc, char* argv[])

variables should be declared locally and initialized

Code:
`(c[d]==12 && c[d]==1)`
Do you understand your own code?

etc

10. Sorry ,i am too stupid to be able understand your code.i dont know what these two FOR loop doing in your main.Is some sorting is done as asked before?
For little i could understand was you should start checking the for loop with previous values.confused!! huh
Code:
```for (a=0; a<7; a++)
{
if (c[a]>10)
continue;
for (b=a+1; b<7; b++)```

11. Originally Posted by vart
only #include <stdio.h> is used

main should be declared as

int main(int argc, char* argv[])

variables should be declared locally and initialized

Code:
`(c[d]==12 && c[d]==1)`
Do you understand your own code?

etc
Well my working program seems to think so!!
4 straights found, 2 going up and two going down.
Which is correct with the data I put in the array.
Might be a problem with some of the end straights containing aces.
Not tested yet.
You don't have my compiler.
Code:
`(c[d]==12 && c[d]==1)`
Well spotted ty.

Code:
`(c[d]==12 && c[d]+1==1)`

I think this is not far off!!

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

#include <io.h>
#include <fcntl.h>
#include <errno.h>
int c[7];
int a,b,x,d,e;

main(argc,argv)
int  argc;
char *argv[];
{

printf("\n Start");

c[0]=3;
c[1]=7;
c[2]=3;
c[3]=12;
c[4]=5;
c[5]=4;
c[6]=6;

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 */```
Output:-
Code:
``` Start
Straight up
Straight up
Straight down
Straight down```

12. It finds this 'tricky one'
Code:
```c[0]=3;
c[1]=10;
c[2]=3;
c[3]=11;
c[4]=1;
c[5]=12;
c[6]=9;

Start
Straight down```

13. And this one:-
It correctly misses 12,1,2,3,4. (well I hope so anyway!!)
Code:
```c[0]=4;
c[1]=12;
c[2]=3;
c[3]=2;
c[4]=1;
c[5]=4;
c[6]=9;

Start
End```
I quite like doing it this way without a sort, it is more the way the human mind does it.
I don't know it it is quicker than a sort first, sorts can be very fast.
This way might be quicker for some arrangements of cards, but I prefer to keep
then in the original order.

14. I also need to change the 'ace' back to a 1 if I change it to 13.

15. is more the way the human mind does it
Have you ever played cards? Because if yes - it seems to me you have something different from the regular human mind

Page 1 of 4 1234 Last