Moderator note: This thread was moved as it was an inapproprioate reply that resurrected Finding a 'straight' in poker.
I'm reviving this thread as I'm in the process of trying to find straights and straight draws on a board.
The latest code in this thread works great if I want to find a straight in any way, but what I really want to find is straight draws. Both inside and outside straightdraws.
Eg. a board consisting of: "2h3c4h" is a straightdraw, and this obviously I can find. But if the board looks like: "2h4c5h" (an inside straightdraw) I'm not sure how to handle it.
My mind is blank and I can not seem to come up with a solution.
Anyone who has any ideas?
Code:
/* straight.c */
#define MAXN 13
#define CARDSINHAND 7
#define IN_A_ROW 5
int is_straight( int* cards, int ncards ) {
int seen[ MAXN ];
int in_a_row;
int i;
for (i = 0; i < MAXN; i++) seen[ i ] = 0; /* zero "seen" array */
for (i = 0; i < ncards; i++) /* set values seen to non-zero */
seen[ cards[ i ] - 1 ]++;
in_a_row = 0; /* see if there are IN_A_ROW in a row */
for (i = 0; i < MAXN; i++) {
if (seen[ i ]) {
if (++in_a_row >= IN_A_ROW)
return 1;
}
else
in_a_row = 0;
}
return 0;
}
int main() {
int c[] = { 4, 6, 3, 5, 7, 9, 11 };
int d[] = { 4, 6, 2, 5, 7, 9, 11 };
printf( "c: " );
printf( is_straight( c, CARDSINHAND ) ? "yes" : "no" );
printf( "\nd: " );
printf( is_straight( d, CARDSINHAND ) ? "yes" : "no" );
printf( "\n" );
}