Using the code below, write a program that deals two five-card poker hands, eval
ID: 3569398 • Letter: U
Question
Using the code below, write a program that deals two five-card poker hands, evaluates each, and determines which is the better hand.
Code:
#include
#include
#include
#define SUITS 4
#define FACES 13
#define CARDS 52
int duplicate;
//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]);
duplicate = 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 && j != duplicate)
printf("The hand contains a two pair of %ss and %ss. ", face[duplicate], 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
A=0;
x=0;
n=input('How many variables=');
disp('Enter the coefficients along with constants For instance if x+y+3z=-5 then enter 1 1 3 -5 each number followed by an enter not space');
for i=1:n
for j=1:n+1
A(i,j)=input('');
end
end
%pivoting
for i=1:n-1
for j=i+1:n
if abs(A(j,i))>abs(A(i,i))
T=A(j,:);
A(j,:)=A(i,:);
A(i,:)=T;
end
end
end
disp('After pivoting');
disp(A);
for k=1:n-1
for i=k+1:n
m=A(i,k)/A(k,k);
for j=k:n+1
A(i,j)=A(i,j)-m*A(k,j);
end
end
end
disp('Triangularize Form ');
disp(A);
if A(n,n)==0
disp('No unique solution');
end
x(n)=A(n,n+1)/A(n,n);
for j=n-1:-1:1
sum=0;
for i=1:n
sprintf('x%.0f=%.10f',i,x(i))
end
for i=1:n-j
sum=sum+A(j,n+1-i)*x(n+1-i);
end
x(j)=(A(j,n+1)-sum)/A(j,j);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.