How to I get my twoPair method to work? I am not sure how to check for two separ
ID: 3569166 • Letter: H
Question
How to I get my twoPair method to work? I am not sure how to check for two separate pairs. I bolded the portion that method.
Also, how do I edit my code to write a program that deals two five-card poker hands, evaluates each, and determines which is the better hand.
Thanks.
Here is my code:
#include
#include
#include
#define SUITS 4
#define FACES 13
#define CARDS 52
//prototypes
void shuffle(unsigned int deck[][FACES]);
void deal(unsigned int deck[][FACES], unsigned int hand[][2], char *wSuit[], char *wFace[]);
void onePair(unsigned int hand[][2], char *suit[], char *face[]);
void twoPair(unsigned int hand[][2], char *suit[], char *face[]);
void threeOfAKind(unsigned int hand[][2], char *suit[], char *face[]);
void fourOfAKind(unsigned int hand[][2], char *suit[], char *face[]);
void flush(unsigned int hand[][2], char *suit[], char *face[]);
void straight(unsigned int hand[][2], char *suit[], char *face[]);
int main()
{
//initialize suit array
char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
//initialize face array
char *face[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
//initialize hand array
unsigned int hand[5][2];
//initialize deck array
unsigned int deck[SUITS][FACES] = {0};
srand(time(NULL)); //random-number generator
shuffle(deck);
deal(deck, hand, suit, face);
onePair(hand, suit, face);
twoPair(hand, suit, face);
threeOfAKind(hand, suit, face);
fourOfAKind(hand, suit, face);
flush(hand, suit, face);
straight(hand, suit, face);
}
void shuffle(unsigned int deck[][FACES])
{
int row; //row #
int column; //column #
int 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(deck[row][column] != 0);
//place card # in chosen slot of deck
deck[row][column] = card;
}
}
//deal cards in deck
void deal(unsigned int deck[][FACES], unsigned int hand[][2], char *wSuit[], char *wFace[])
{
int i = 0;//hand counter
printf("The hand is: ");
//deal each of the cards
for(int card = 1; card <= 5; ++card)
{
for(int row = 0; row < SUITS; ++row)
{
for(int column = 0; column < FACES; ++column)
{
//if slot contains current card, display card
if (deck[row][column] == card)
{
hand[i][0] = row;
hand[i][1] = column;
printf("%5s of %-8s ", wFace[column], wSuit[row]);
++i;
}
}
}
}
printf(" ");
}
//find one pair
void onePair(unsigned int hand[][2], char *suit[], char *face[])
{
int count[13] = {0};
//find # of cards of each rank in hand
for (int i = 0; i < 5; i++)
{
count[hand[i][1]]++;
}
//if pair exists, print it
for (int j = 0; j < 13; j ++)
{
if (count[j] == 2)
printf("The hand contains a pair of %ss. ", face[j]);
}
}
//find two pairs
void twoPair(unsigned int hand[][2], char *suit[], char *face[])
{
int count[13] = {0};
//find # of cards of each rank in hand
for (int i = 0; i < 5; i++)
{
count[hand[i][1]]++;
}
//if two pairs exists, print it
for (int j = 0; j < 13; j++)
{
if (count[j] == 2)
printf("The hand contains two pairs of %ss and %ss. ", face[j], face[j]);
}
}
//finds three of a kind in hand
void threeOfAKind(unsigned int hand[][2], char *suit[], char *face[])
{
int count[13] = {0};
//find # of cards of each rank in hand
for (int i = 0; i < 5; i++)
{
count[hand[i][1]]++;
}
//if three of a kind exists, print it
for (int j = 0; j < 13; j++)
{
if (count[j] == 3)
printf("The hand contains three %ss. ", face[j]);
}
}
//finds four of a kind in hand
void fourOfAKind(unsigned int hand[][2], char *suit[], char *face[])
{
int count[13] = {0};
//find # of cards of each rank in hand
for (int i = 0; i < 5; i++)
{
count[hand[i][1]]++;
}
//if three of a kind exists, print it
for (int j = 0; j < 13; j++)
{
if (count[j] == 4)
printf("The hand contains four %ss. ", face[j]);
}
}
//finds flush in hand
void flush(unsigned int hand[][2], char *suit[], char *face[])
{
int count[4] = {0};
//find # of cards of each rank in hand
for (int i = 0; i < 5; i++)
{
count[hand[i][0]]++;
}
//if flush exists, print it
for (int j = 0; j < 4; j++)
{
if (count[j] == 5)
printf("The hand contains a flush of %ss. ", suit[j]);
}
}
//finds straight in hand
void straight(unsigned int hand[][2], char *suit[], char *face[])
{
int straight[5] = {0};
//copy column location for sorting
for (int i = 0; i < 5; i++)
{
straight[i] = hand[i][1];
}
//bubble sort to sort column locations
for (int pass = 1; pass < 5; pass++)
{
for(int j = 0; j < 4; j++)
{
if (straight[j] > straight[j + 1])
{
int temp = straight[j];
straight[j] = straight[j + 1];
straight[j + 1] = temp;
}
}
}
//check if columns contain a straight
if (straight[4] - 1 == straight[3] && straight[3] - 1 == straight[2] && straight[2] - 1 == straight[1] && straight[1] - 1 == straight[0])
printf("The hand contains a straight from %s to %s. ", face[straight[0]], face[straight[4]]);
}
Explanation / Answer
public static void main(String[] args) { int N = Integer.parseInt(args[0]); String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; // initialize cards in a deck String[] deck = new String[suit.length * rank.length]; for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.