patterncMinor
Small Poker program rewrite
Viewed 0 times
pokerprogramrewritesmall
Problem
I had already put up code for review a small poker program but I have rewrote it utilizing a more appropriate data structure.
How would I compare Poker hands accordingly?
Full source repo
```
enum poker_hands check_hand(vector hand) / check what kind of poker hand player has and return a score from 1 to 10 */
{
sort_hand(hand);
struct Card_s *temp[MAXHAND];
for (int t = 0; t nSuit != temp[i]->nSuit) { / check if card 1 is the same suit as the rest /
flushsuit = false;
break;
}
}
if ( flushsuit ) {
if ( temp[0]->nType == (temp[1]->nType-1) / check if the cards increment one after the other /
&& temp[1]->nType == (temp[2]->nType-1)
&& temp[2]->nType == (temp[3]->nType-1)
&& temp[3]->nType == (temp[4]->nType-1) )
{
if ( temp[4]->nType == ace )
return royal_flush; / found royal flush /
else
return straight_flush; / found straight flush /
}
return normal_flush; / it was a normal flush /
}
}
int group[MAXHAND+1] = { 0 } ;
int rankcount[MAXTYPES] = { 0 } ;
for(int i = 0; i nType; // Get each card's type
rankcount[rank]++; // Increment each rank index
group[rankcount[rank]]++; // increment each rank's count index
}
if ( group[4] )
return four_kind;
if ( group[3] && group[2] > 1 ) // Found 3 of a kind and found another pair
return fullhouse;
if ( temp[0]->nType == (temp[1]->nType-1)
&& temp[1]->nType == (temp[2]->nType-1)
&& temp[2]->nType == (temp[3]->nType-1)
&& temp[3]->nType == (temp[4]->nType-1) ) {
return straight; / found straight /
}
if ( group[3] )
return three_kind;
if ( group[2] > 1 ) // If the 2nd index of the group[] array is more than 1, two pairs are found
retu
How would I compare Poker hands accordingly?
Full source repo
```
enum poker_hands check_hand(vector hand) / check what kind of poker hand player has and return a score from 1 to 10 */
{
sort_hand(hand);
struct Card_s *temp[MAXHAND];
for (int t = 0; t nSuit != temp[i]->nSuit) { / check if card 1 is the same suit as the rest /
flushsuit = false;
break;
}
}
if ( flushsuit ) {
if ( temp[0]->nType == (temp[1]->nType-1) / check if the cards increment one after the other /
&& temp[1]->nType == (temp[2]->nType-1)
&& temp[2]->nType == (temp[3]->nType-1)
&& temp[3]->nType == (temp[4]->nType-1) )
{
if ( temp[4]->nType == ace )
return royal_flush; / found royal flush /
else
return straight_flush; / found straight flush /
}
return normal_flush; / it was a normal flush /
}
}
int group[MAXHAND+1] = { 0 } ;
int rankcount[MAXTYPES] = { 0 } ;
for(int i = 0; i nType; // Get each card's type
rankcount[rank]++; // Increment each rank index
group[rankcount[rank]]++; // increment each rank's count index
}
if ( group[4] )
return four_kind;
if ( group[3] && group[2] > 1 ) // Found 3 of a kind and found another pair
return fullhouse;
if ( temp[0]->nType == (temp[1]->nType-1)
&& temp[1]->nType == (temp[2]->nType-1)
&& temp[2]->nType == (temp[3]->nType-1)
&& temp[3]->nType == (temp[4]->nType-1) ) {
return straight; / found straight /
}
if ( group[3] )
return three_kind;
if ( group[2] > 1 ) // If the 2nd index of the group[] array is more than 1, two pairs are found
retu
Solution
-
Naming problem or design issue. The first thing
-
Code to detect a straight is redundant in flush strait and normal straight calculate. Need only one.
-
Group detection. Some poke like games find the best hand out of 5 cards (usual) or more (like 7 in 7 card stud) or betting order based on incomplete hands (1 to 9) cards. A hand assessment that does not depend on 5 cards is more useful.
-
Rating. Returning info as what kind of full house (Jacks over fives) is useful info for many games. This code only returns
-
Minor: Code for understanding. By decrementing the smaller group count,
Naming problem or design issue. The first thing
check_hand() does is to re-order the data. I'd expect check_hand() to not alter hand, base on its name - it is a check. Alternatively just re-name it sort_check_hand(). Yet later code make a copy in temp[]. Better to copy into temp[] and then sort temp[], leaving hand[] alone.//enum poker_hands check_hand(vector *hand) {
// sort_hand(hand);
enum poker_hands sort_check_hand(vector *hand) {
sort_hand(hand);-
Code to detect a straight is redundant in flush strait and normal straight calculate. Need only one.
-
Group detection. Some poke like games find the best hand out of 5 cards (usual) or more (like 7 in 7 card stud) or betting order based on incomplete hands (1 to 9) cards. A hand assessment that does not depend on 5 cards is more useful.
-
Rating. Returning info as what kind of full house (Jacks over fives) is useful info for many games. This code only returns
fullhouse, three_kind etc, yet only needs a little more code to report the hand details.-
Minor: Code for understanding. By decrementing the smaller group count,
group[2] only reports the numbers of pairs, not the sum of numbers of pairs, triples and 4-of-a kind.// original
for(int i = 0; i nType; // Get each card's type
rankcount[rank]++; // Increment each rank index
group[rankcount[rank]]++; // increment each rank's count index
}
if ( group[3] && group[2] > 1 ) // Found 3 of a kind and found another pair
return fullhouse;
// suggested revision
for(int i = 0; i nType;
group[rankcount[rank]]--; // add
rankcount[rank]++;
group[rankcount[rank]]++;
}
if (group[3] > 0 && group[2] > 0) {
return fullhouse;Code Snippets
//enum poker_hands check_hand(vector *hand) {
// sort_hand(hand);
enum poker_hands sort_check_hand(vector *hand) {
sort_hand(hand);// original
for(int i = 0; i < MAXHAND; ++i) {
int rank = temp[i]->nType; // Get each card's type
rankcount[rank]++; // Increment each rank index
group[rankcount[rank]]++; // increment each rank's count index
}
if ( group[3] && group[2] > 1 ) // Found 3 of a kind and found another pair
return fullhouse;
// suggested revision
for(int i = 0; i < MAXHAND; ++i) {
int rank = temp[i]->nType;
group[rankcount[rank]]--; // add
rankcount[rank]++;
group[rankcount[rank]]++;
}
if (group[3] > 0 && group[2] > 0) {
return fullhouse;Context
StackExchange Code Review Q#132353, answer score: 2
Revisions (0)
No revisions yet.