You are welcome to report any futher bugs
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...
You are welcome to report any futher bugs
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 */
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
http://www.youtube.com/watch?v=FczbS7FiWSM
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!!
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
To be or not to be == true
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!!!
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:
and to find the suit do:Code:value = (card % 4) + 1;
Either that or create a struct that contains the card value & suit...Code:suit = card / 13;
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
How about something like this?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; }
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.
By the way, how comes you have 7 cards in poker?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 %d and index %d\n", cards[straight], straight); } return 0; }
Last edited by anon; 01-08-2008 at 03:36 PM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.