Creating an array to hold a poker hand in C? - c

I have an assignment due in my computer science class (I posted this question a couple weeks ago but the way it was explained doesn't fit the program I'm supposed to do). I already have a program that shuffles and deals a deck right here.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void shuffle( int [][ 13 ] );
void deal ( const int[][ 13 ], const char *[], const char *[] );
int main()
{
const char *suit[4] ={"Hearts","Diamonds","Clubs","Spades"};
const char *face[13] ={"Ace", "Duece", "Three", "Four", "Five", "Six",
"Seven", "Eight","Nine", "Ten", "Jack", "Queen", "King"};
int deck[4][13] = {0};
int row, column, card = 1;
for( row = 0; row <= 3; row++ )
{
for(column = 0; column <= 12; column++)
{
deck[row][column] = card;
card++;
}
}
srand(time(0));
shuffle(deck);
deal(deck, face, suit);
return 0;
}
void shuffle( int wDeck[][13] )
{
int row, column, randomColumn, randomRow, card = 1, counter1, counter2, hold;
for( counter1 = 0; counter1 <= 3; counter1++)
{
for(counter2 = 0; counter2 <= 12; counter2++)
{
randomColumn = rand() % 13;
randomRow = rand() % 4;
hold = wDeck[counter1][counter2];
wDeck[counter1][counter2] = wDeck[randomRow][randomColumn];
wDeck[randomRow][randomColumn] = hold;
}
}
}
void deal( const int wDeck[][13], const char *wFace[], const char *wSuit[] )
{
int card, row, column;
for ( card = 1; card <= 52; card++ )
for (row = 0; row <= 3; row++ )
for ( column = 0; column <= 12; column++ )
if( wDeck[row][column] == card )
{
printf("%5s of %-8s%c",wFace[ column ], wSuit[row], card % 2 == 0 ? '\n' : '\t');
break;
}
}
I'm supposed to modify the deal function to deal a 5 card poker hand, and then later check to see what "rank" poker hand they have(two of a kind, flush). My teacher mentioned creating a separate double scripted array to do this but I could do it a different way. The problem is, I have to use the current deck/shuffle setup to do it. Could anyone explain how to do this? It's okay if it's inefficient, as long as it works.

With the existing deal prototype, there is no way to return information from it - it has no return value and all the arguments are const. Since the deck has already been shuffled (by shuffle()), I assume the idea is for the deal function to look at the first five cards in the deck array, and find the rank of them. That can either be done entirely within the deal function, or (better), by calling a int rank(int hand[5]) function from within the deal function.

Related

how to see if there are 1 or 2 poker pairs in a hand in C

I am trying to develop a C program that checks if there are 1 or 2 pairs in a 5 card poker hand.
I am using a 5x3 array where every line is a card (the 3rd column being for the \0 character). Every time I execute the code it always shows the "two pairs" print.
I want to make sure that each letter (i, j, a, b) representing each line is different. Any help?
P.S.: This is for a university/college project, I have only started programming a few months ago from absolute scratch, so any detailed explanations on my mistakes would be very much appreciated :)
#include <stdio.h>
#include <stdlib.h>
char (cards[5][3])=
{
"5S", "6D", "4H", "KD", "5C"
};
int main ()
{
pair (cards[5][3]);
return 0;
}
void pair (char (arg[n][0]))
{
int i,j,a,b;
if (i!=j!=a!=b)
{
if ((arg[i][0]==arg[a][0])&&(arg[b][0]!=arg[j][0]))
{
printf("2 -> pair");
}
if ((arg[i][0]==arg[a][0])&&(arg[b][0]==arg[j][0]));
{
printf("3 -> two pairs");
}
if ((arg[i][0]!=arg[a][0])&&(arg[b][0]!=arg[j][0]))
{
printf("there is no pair");
}
}
else
{
printf("there is no pair");
}
}
The posted code has several issues, both logical and syntactical, some have been pointed out in the comments.
Just to pick one, consider this line
if ((arg[i][0]==arg[a][0])&&(arg[b][0]==arg[j][0]));
{
// This body will never be executed ^
}
I'd suggest to restart from scratch and to proceed in small steps. See, for instance, the following minimal implementation
// Include all the needed header files, not the unneeded ones.
#include <stdio.h>
// Declare the functions prototype before their use, they will be defined after.
int count_pairs(int n, char const cards[][3]);
// Always specify the inner size, ^ when passing a multidimensional array
void show_score(int n_pairs);
int have_the_same_value(char const *a, char const *b);
int main (void)
{
char hand[5][3] = {
// ^^^^^^ You could omit the 5, here
"5S", "6D", "4H", "KD", "5C"
};
int n_pairs = count_pairs(5, hand);
// Always pass the size ^ if there isn't a sentinel value in the array
show_score(n_pairs);
return 0;
}
// This is a simple O(n^2) algorithm. Surely not the best, but it's
// a testable starting point.
int count_pairs(int n, char const cards[][3])
{
// Always initialize the variables.
int count = 0;
// Pick every card...
for (int i = 0; i < n; ++i)
{
// Compare (only once) with all the remaining others.
for (int j = i + 1; j < n; ++j)
{ // ^^^^^
if ( have_the_same_value(cards[i], cards[j]) ) {
++count;
}
}
}
return count;
}
int have_the_same_value(char const *a, char const *b)
{
return a[0] == b[0];
}
// Interpret the result of count_pairs outputting the score
void show_score(int n_pairs)
{
switch (n_pairs)
{
case 1:
printf("one pair.\n");
break;
case 2:
printf("two pairs.\n");
break;
case 3:
printf("three of a kind.\n");
break;
case 4:
printf("full house.\n");
break;
case 6:
printf("four of a kind.\n");
break;
default:
printf("no pairs.\n");
}
}
Note that my count_pairs function counts every possible pair, so if you pass three cards of the same kind, it will return 3 (given AC, AS, AD, all the possible pairs are AC AS, AC AD, AS AD).
How to correctly calculate all the poker ranks is left to the reader.
Major improvements can be made to the pair function to make it slimmer. However, this answers your questions and solves several corner cases:
#include <stdio.h>
#include <stdlib.h>
void pairCheck(char hand[][2])
{
int pairCount = 0;
int tmpCount = 0;
char tmpCard = '0';
char foundPairs[2] = {0};
// Check Hand One
for(int i =0; i < 5; i++)
{
tmpCard = hand[i][0];
for(int j = 0; j < 5; j++)
{
if(tmpCard == hand[j][0] && i != j)
{
tmpCount++;
}
if(tmpCount == 1 && (tmpCard != foundPairs[0] && tmpCard != foundPairs[1]))
{
foundPairs[pairCount] = tmpCard;
pairCount++;
}
tmpCount = 0;
}
}
printf("Pair Count Hand One: %i\r\n",pairCount);
//Reset Variables
foundPairs[0] = 0;
foundPairs[1] = 0;
tmpCard = '0';
pairCount = 0;
// Check Hand One
for(int i =0; i < 5; i++)
{
tmpCard = hand[i][1];
for(int j = 0; j < 5; j++)
{
if(tmpCard == hand[j][1] && i != j)
{
tmpCount++;
}
if(tmpCount == 1 && (tmpCard != foundPairs[0] && tmpCard != foundPairs[1]))
{
foundPairs[pairCount] = tmpCard;
pairCount++;
}
tmpCount = 0;
}
}
printf("Pair Count Hand Two: %i",pairCount);
}
int main ()
{
char cards[5][2] = { {'5','H'},{'6','D'},{'4','H'},{'K','D'},{'5','C'}};
pairCheck(cards);
return 0;
}
This function will treat three, four, or five of a kind as a single pair. If you want a different behavior the change should be easy.

Changing the contents of an array in a recursive function

I am having trouble understanding something regarding recursion and arrays.
basically, what the program does is to check what is the maximum weights of items that can be placed in two boxes. I know it's far from perfect as it is right now, but this is not the point.
Generally everything is working properly, however, now I decided that I want to see the contents of each box when the weight is maximal. For this purpose I tried using arr1 and arr2.
I don't understand why I get different results for arr1 and arr2 (the first options gives me what I want, the second does not).
This is the program:
#define N 5
int help(int items[N][2], int box1[N], int box2[N], int rules[N][N],
int optimal,int current_weight,int item,int arr1[],int arr2[])
{
if (item == N)
{
if(current_weight>optimal) //This is the first option
{
memcpy(arr1,box1,sizeof(int)*N);
memcpy(arr2,box2,sizeof(int)*N);
}
return current_weight;
}
int k = items[item][1]; int sol;
for (int i = 0; i <= k; i++)
{
for (int j = 0; i+j <= k; j++)
{
box1[item] += i; box2[item] += j;
if (islegal(items, box1, box2, rules))
{
sol = help(items, box1, box2, rules, optimal,
current_weight + (i + j)*items[item][0],item+1,arr1,arr2);
if (sol > optimal)
{
optimal = sol;
memcpy(arr1,box1,sizeof(int)*N); //This is the second option
memcpy(arr2,box2,sizeof(int)*N);
}
}
box1[item] -= i; box2[item] -= j;
}
}
return optimal;
}
int insert(int items[N][2], int rules[N][N])
{
int box1[N] = { 0 }; int arr1[N] = { 0 };
int box2[N] = { 0 }; int arr2[N] = { 0 };
int optimal = 0;
int x = help(items, box1, box2, rules,0, 0,0,arr1,arr2);
print(arr1, N);
print(arr2, N);
return x;
}
Can anyone explain what causes the difference? Why the first option is correct and the second is not? I couldn't figure it out by my own.
Thanks a lot.
This doesn't work because when you pass box1 and box2 to help, they are mutated by help. It's pretty obvious from the algorithm that you want them to not be mutated. So, we can do as follows:
int help(int items[N][2], int box1in[N], int box2in[N], int rules[N][N],
int optimal,int current_weight,int item,int arr1[],int arr2[])
{
int box1[N];
int box2[N];
memcpy(box1, box1in, sizeof(int)*N);
memcpy(box2, box2in, sizeof(int)*N);
Your algorithm may still have problems but that problem is now removed.

Editing an array of strings in c

I'm writing code for a poker game and in my main function I have:
const char *suits[4] = { "Spades", "Clubs", "Hearts", "Diamonds" };
const char *faces[13] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
int deck[4][13] = { 0 };
srand((unsigned)time(NULL));
char *hand[5] = { "\0" };
shuffle(deck);
deal(deck, faces, suits, hand);
for (int i = 0; i < 5; i++) {
printf("%s", hand[i]);
}
This is where my general problem is. hand wont print out the values given to it in deal, which are 5 cards.
shuffle() simply shuffles the deck, no errors there so I am not going to include it in this question.
deal() has the following code (ignore the curly-bracket/whitespace discrepancies, I'm still adjusting to the formatting of this site):
void deal(const int wDeck[][13], const char *wFace[], const char *wSuit[],
char *hand[]) {
int row = 0; /* row number */
int column = 0; /*column number */
int card = 0; /* card counter */
/* deal 5 of the 52 cards */
for (card = 1; card <= 5; card++)
{
/* loop through rows of wDeck */
for (row = 0; row <= 3; row++)
{
/* loop through columns of wDeck for current row */
for (column = 0; column <= 12; column++)
{
/* if slot contains current card, deal card */
if (wDeck[row][column] == card)
{
char str1[10];
strcpy(str1, wFace[column]);
char str2[10];
strcpy(str2, wSuit[row]);
char str3[6] = " of ";
char str[26] = "";
strcat(str, str1);
strcat(str, str3);
strcat(str, str2);
puts(str);
hand[card - 1] = str;
printf("%s\n", hand[card - 1]);
}
}
}
}
}
The code in the if statement works just fine.
The problem arises in main() when I try to print the values given to hand, however in deal() the values in hand print fine. I assume that I am not passing hand into the function correctly, but no matter the different methods I've tried to get the program to run correctly, nothing works.
An example of the program as is can be seen here:
Example of program running
in you deal() function:
hand[card - 1] = str;
str is local character array whose address will get invalidated once you return from deal()
right way to do it will be allocate memory to each element(max number of elements being 5) of hand, then copy value of str using strcpy into element of hand
e.g.
hand[card - 1] = malloc(26);
strcpy(hand[card - 1],str);

Poker Game Results Between Two Players

I'm at the point where I'm trying to rank my players in the game, and determine the winner. You can read my code below, as I think I've documented the logic pretty well throughout.
What I need to do now is determine which player wins, given that there are two players. I've laid out functions for shuffling, dealing, and determining which type of hand it is.
//
// main.c
// Created by gixx88 on 7/22/15.
// Copyright (c) 2015 gixx88. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
#define HAND 5
#define PLAYERS 2
//prototypes for shuffle and deal
void shuffle( size_t wDeck[][FACES] ); //shuffling modifies wDeck
void deal( size_t wDeck[][FACES], const char *wFace[],
const char *wSuit[] ); //dealing doesn't modify the arrays
void dealHand(size_t wHands[][HAND], size_t wDeck[][FACES]);
//prototypes for determing suit and face
size_t determineSuit(size_t wCard);
size_t determineFace(size_t wCard);
//prototypes for determing poker hand
unsigned int findAPair(size_t wHand[HAND]);
unsigned int findTwoPairs(size_t wHand[HAND]);
unsigned int findThreeOfAKind(size_t wHand[HAND]);
unsigned int findFourOfAKind(size_t wHand[HAND]);
unsigned int findFlush(size_t wHand[HAND]);
unsigned int findFullHouse(size_t wHand[HAND]);
unsigned int findStraightFlush(size_t wHand[HAND]);
unsigned int findStraight (size_t wHand[HAND]);
//prototypes for determing player outcomes
unsigned int determingPlayerHand(size_t wHand[HAND]);
void playerRank( size_t wHands[PLAYERS][HAND]);
int main(void) {
//initialize suit array
const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades" };
//initilize face array
const char *face[ FACES ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
//initilize deck array
size_t deck[ SUITS ][FACES] = {{0}};
size_t hands[PLAYERS][HAND] = {{0}};
srand(time(NULL)); //random seed generator
shuffle(deck); //shuffle the deck
dealHand(hands, deck);
playerRank(hands);
} //end main
//determine suit
size_t determineSuit(size_t wCard){
return wCard / SUITS;
} // end determine suit
//determine face
size_t determineFace(size_t wCard){
return wCard % FACES;
}//end determine face
//if four of a kind
//shuffle cards in deck
void shuffle( size_t wDeck[][FACES] ){
size_t row; //row number
size_t column; //column number
size_t card; //counter
for (card = 0; card <= CARDS; ++card){
//choose a random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
} //end do
while (wDeck[row][column] != 0); //end do while
//place card number in chosen slot of deck
wDeck[row][column] = card;
}// end for
} //end shuffle function
//deal cards in deck
void deal( size_t wDeck[][FACES], const char *wFace[],
const char *wSuit[] ){
size_t card; //card counter
size_t row; //row counter
size_t column; //column counter
//deal each of the cards
for (card = 0; card <= CARDS; ++card){
//loop through rows of wDeck
for (row = 0; row < SUITS; ++row){
//loop through columns in wDeck for current row
for(column = 0; column < FACES; ++column){
//if slot contians current card, display card
if (wDeck[row][column] == card){
printf("%5s of %-8s%c", wFace[column], wSuit[row],
card % 2 == 0 ? '\n' : '\t'); //two column format
} //end if
} //end column for
} //end row for
} //end card for
} //end function deal
void dealHand(size_t wHands[][HAND], size_t wDeck[][FACES]){
size_t cards;
size_t players;
unsigned int current;
unsigned int current_suit;
unsigned int current_face;
for (cards = 0; cards <= HAND; cards ++){
for (players = 0; players < PLAYERS; players++){
current = cards * PLAYERS + players;
current_suit = current / SUITS;
current_face = current % FACES;
wHands[players][cards] = wDeck[current_suit][current_face];
} // end players for
} // end cards for
} // end deal hand
//PRINT THE CARD
void printCard(unsigned int wCard, const char wFaces[FACES], const char wSuit[SUITS]){
//find suit and face
size_t suit;
size_t face;
suit = determineSuit(wCard);
face = determineFace(wCard);
printf("%s or %s", wFaces[face], wSuit[suit]);
} //end print card
//******************************
//BEGIN HAND TYPE DETERMINATION
//******************************
//find A PAIR
unsigned int findAPair(size_t wHand[HAND]){
size_t countOfFaces[FACES] = { 0 };
unsigned int foundAPair = 1;
size_t card;
for (card = 0; card < HAND; card++)
{
size_t face = determineFace(wHand[card]);
countOfFaces[face]++;
if (countOfFaces[face] == 3)
{
return 1;
}
else if (countOfFaces[face] == 2)
{
if (foundAPair)
{
return 1;
}
foundAPair = 0;
}
}
return foundAPair;
} // end find a pair
//find TWO PAIRS
unsigned int findTwoPairs(size_t wHand[HAND]){
size_t countOfFaces[FACES] = { 0 };
unsigned int numberOfPairs = 0;
size_t card;
for (card = 0; card < HAND; card++){
size_t face = determineFace(wHand[card]);
countOfFaces[face]++;
if (countOfFaces[face] == 3)
{
return 1;
}
else if (countOfFaces[face] == 2)
{
numberOfPairs++;
}
}
return numberOfPairs == 2;
} //end find two pairs
//find THREE OF A KIND
unsigned int findThreeOfAKind(size_t wHand[HAND]){
size_t countOfFaces[FACES] = {0};
size_t card;
unsigned int foundThree = 1;
for (card = 0; card < HAND; card++){
size_t face = determineFace(wHand[card]);
countOfFaces[face]++;
if (countOfFaces[face] == 3){
foundThree = 0;
} //end if
else if (countOfFaces[face] == 4){
return 1;
} //end else if
else if (countOfFaces[face] == 2 && foundThree){
return 1;
} //end else if
} //end card for loop
return 1;
} //end find three of a kind
//find FOUR OF A KIND
unsigned int findFourOfAKind(size_t wHand[HAND]){
size_t countOfFaces[FACES] = {0};
size_t card;
for (card = 0; card < HAND; card++){
size_t face = determineFace(wHand[card]);
countOfFaces[face]++;
if (countOfFaces[face] == 4){
return 0;
} //end if
} //end card for loop
return 1;
} //end for of a kind
unsigned int findStraight (size_t wHand[HAND]){
size_t card;
size_t lowFace;
size_t highFace;
size_t firstSuit;
unsigned int foundSecondSuit = 1;
for (card = 0; card < HAND; card++)
{
size_t suit = determineSuit(wHand[card]);
size_t face = determineFace(wHand[card]);
// first card
if (card == 0)
{
lowFace = face;
highFace = face;
firstSuit = suit;
}
// all other cards
// check for two equal faces first
else if (face == lowFace || face == highFace)
{
return 1;
}
// update low and high face, if necessary
else
{
if (suit != firstSuit)
{
foundSecondSuit = 0;
}
// an Ace can only go low if the low face is at most a Five
if (face == 0 && lowFace > HAND - 1)
{
face = 13;
}
if (face < lowFace)
{
lowFace = face;
}
if (face > highFace)
{
highFace = face;
}
if ((highFace - lowFace + 1) != HAND)
{
return 1;
}
}
}
return foundSecondSuit;
}
//find FLUSH
unsigned int findFlush(size_t wHand[HAND]){
size_t card;
size_t firstSuit = 5;
for (card = 0; card < HAND; card++){
size_t suit = determineSuit(wHand[card]);
//determing if all cards are the same suit
if (card == 0){
firstSuit = suit;
} //end if
else if (suit != firstSuit){
return 1;
} //end else if
} //end card for loop
return 0;
} //end find flush
//find FULL HOUSE
unsigned int findFullHouse(size_t wHand[HAND]){
if (findAPair(wHand) && findThreeOfAKind(wHand)){
return 0;
} //end if
return 1;
} //end full house
//find STRAIGHT FLUSH
unsigned int findStraightFlush(size_t wHand[HAND]){
size_t lowFace;
size_t highFace;
size_t card;
size_t firstSuit;
for (card = 0; card < HAND; card++){
size_t suit = determineSuit(wHand[card]);
size_t face = determineFace(wHand[card]);
//determing if all cards are the same suit
if (card == 0){
firstSuit = suit;
lowFace = face;
highFace = face;
} //end if
else if (suit != firstSuit || face == lowFace || face == highFace){
return 1;
} //end else if
//determination for ace being high or low, which depends on hand
else
{
// an Ace can only go low if the low face is at most a Five
if (face == 0 && lowFace > HAND - 1)
{
face = 13;
}
if (face < lowFace)
{
lowFace = face;
}
if (face > highFace)
{
highFace = face;
}
if ((highFace - lowFace + 1) != HAND)
{
return 1;
}
}
}
return 0;
} //end straight flush
//****************************
//DETERMINING PLAYER OUTCOMES
//****************************
//determing PLAYER HAND
unsigned int determingPlayerHand(size_t wHand[HAND]){
if (findStraightFlush(wHand) == 0){
return 8;
} //end if
else if (findFourOfAKind(wHand) == 0){
return 7;
} //end else if
else if (findFullHouse(wHand) == 0){
return 6;
} //end else if
else if (findFlush(wHand) == 0){
return 5;
} //end else if
else if (findStraight(wHand) == 0){
return 4;
} //end else if
else if (findThreeOfAKind(wHand) == 0){
return 3;
} //end else if
else if (findTwoPairs(wHand) == 0){
return 2;
} //end else if
else if (findAPair(wHand) == 0){
return 1;
}
return 0;
} //end determing player hand
//determing PLAYER RANK
void playerRank( size_t wHands[PLAYERS][HAND]){
unsigned int player1 = determingPlayerHand(wHands);
unsigned int player2 = determingPlayerHand(wHands);
if (player1 > player2){
printf("%s", "Player 1 Wins!");
}//end if
else if (player2 > player1){
printf("%s", "Player 2 Wins!");
}
} //end player rank
I'm loathe to post this, but sadly, I've got to finish this by tomorrow and I've already been working on it 12 hours straight. My concern is that I set up my poker hand type function to only take one dimension of the two dimensional array. Is there a way to effectively build a ranking system for players with what I have?
Here are some notes I made to myself elsewhere that may also be useful: determingPlayerHand only takes one subscript of the two dimensional array. I need playerRanking to take both subscripts. How can I make this happen so that my rankings will return properly to each player? The ranking returning values should be fine, I think. I initially had it as just true or false, 1 or 0, but then realized that wasn't specific enough
Please let me know what other information I can provide if I need to make this more clear or relevant.
After M Oehm's comment, I want to update this to reflect some other information that I realized would be useful.
Part of the parameters of the problem I'm solving require certain things to be the way they are. If I had my choice in changing them, I think there are better ways to do this as well. However, the deck being as it is, the shuffling algorithm, and the deal algorithm are built to specification of the problem. As for hands evaluating to what they should, so far they do, and I wrote it based on the idea that ace needs to go high at a certain point, which is part of why it's so complicated. The two dimensional array with [PLAYERS][HAND] is meant to make it so that cards are dealt in alternating order to each player from the top of the deck, as would be done in real life.
You have gotten off to a bad start when chosing how to represent your deck and the hands.
You represent the cards as integers from 0 to 51 in the deck's factory order from Ace of Hearts to King of Clubs. That's fine. You can determine the rank and suit from the card number:
rank = card % 13;
suit = card / 13;
That's not what you do in your determineFace/Suit functions. Next, the deck ist a one-dimensional array of cards:
int deck[52];
You can shuffle this array with one of the well-known shuffling algorithms, e.g. Fisher-Yates. You represent the deck as two-dimensional array, which doesn't make the shuffling any easier. It it also not clear what the two dimensions of the deck are. (Okay, they are suit and rank, but after shuffling, that doesn't make sense any longer.)
In a one-dimensional array, the first player's hand are the first five cards and the second player's hand are cards 5 to 9.
Your algorithms to determine the hands are too complicated and have some copy-and-paste errors. Evaluating poker hands boils down to three criteria:
Flush: Are all cards of a hand of the same suit?
Straight: Are all ranks consecutive? (With the special rule that the ace can be played high or lowas appropriate)
Multiples: Most hands have pairs, triplets or quadruplets of cards or combinations thereof, so you need a "histogram" of ranks, sorted by occurrence.
With that data, you can determine your hands. You also need secondary data to distinguish between two equal hands, e.g. a pair of Kings and a pair of Nines.
I suggest you start afresh, switch on compiler warnings and implement shuffling, dealing and the three hand criteria step by step with verification between the steps. (You can call these steps "Milestones", if you like.)

Poker Program in c Programming

I've put together a program that deals out a hand poker perfectly. Now I want the program to realize when the hand that is dealt is straight, flush, pair, 3 of a kind, and 4 of kind. The program runs but never prints the right condition when needed to, I believe I have some placement or logic error that I can't find. Here's what I have so far.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
#define HAND 5//draw only 5
#define TRUE 1//positive print condition
#define FALSE 0//negative print condition
//prototypes
shuffle( unsigned int wDeck[][FACES]);//shuffling modifies wDeck
deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[] );//dealing doesn't modify the arrays
//true/false conditions
typedef int bool;
bool straight, flush, four, three;
int pairs; //0,1, or 2
int main()
{
//initialize suit array
const char *suit[ SUITS ] =
{
"Hearts", "Diamonds", "Clubs", "Spades"
};
//initialize face array
const char *face[ FACES ] =
{
"Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"
};
int suitInHand[SUITS], facesInHand[FACES];
analyzeHand(suitInHand, facesInHand);
//initialize deck array
unsigned int deck[SUITS][FACES] = { 0 };
srand( time( NULL ) );//seed random-number generator
shuffle( deck );//shuffle the deck
deal( deck, face, suit );//deal the deck
}//end main
//shuffle cards in deck
shuffle( unsigned int wDeck[][FACES])
{
size_t row;//row number
size_t column;//column number
size_t card;//counter
//for each of the cards, choose slot of deck randomly
for( card = 1; card <= CARDS; ++card) {
//choose new random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
}
while( wDeck[ row ][ column ] !=0);
//end do-while
//pace card number in chosen slot of deck
wDeck[ row ][ column ] = card;
}//end for
}//end function shuffle
//deal cards in deck
deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[] )
{
size_t card;//card counter
size_t row;//row counter
size_t column;//column counter
//deal each of the cards
for( card = 1; card <= HAND; ++card) {
//loop through rows of wDeck
for( row = 0; row < SUITS; ++row) {
//loop through column of wDeck for current row
for( column = 0; column < FACES; ++column) {
//if slot contains current card, display card
if( wDeck[ row ][ column ] == card ) {
printf("%5s of %-8s%c", wFace[ column ], wSuit[ row ],
card % 2 == 0 ? '\n' : '\t' );//2 column format
}//end if
}//end for
}//end for
}//end for
}//end function deal
analyzeHand(int suitsInHand[], int facesInHand[])
{
int num_consec = 0;
int rank, suit;
straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;
for (suit = 0; suit < SUITS; suit++)
if ( suitsInHand[suit] == 5)
flush = TRUE;
rank = 0;
while ( facesInHand[rank] == 0)
rank++;
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if(num_consec == 5){
straight = TRUE;
return;
}
for(rank = 0; rank < FACES; rank++) {
if(facesInHand[rank] == 4)
four = TRUE;
if(facesInHand[rank] == 3)
three = TRUE;
if(facesInHand[rank] == 2)
pairs++;
}
if(four)
printf("Four of a kind\n");
else if(straight)
printf("Straight\n");
else if(pairs == 2)
printf("Two Pairs\n");
else if(pairs == 1)
printf("Pair\n");
else
printf("Better Luck Next Time\n");
}
There seems to be a problem with the logic in your main() function :
int suitInHand[SUITS], facesInHand[FACES];
analyzeHand(suitInHand, facesInHand);
You are declaring two arrays of ints without initializing them, and them you use them in your analyzeHand() function while they are empty.
You must populate those 2 arrays first if you want to get any kind of valid result.
EDIT : Depending on what kind of infos will be stored in those 2 arrays, their may be some problems with the logic of your analyzeHand() function.

Resources