Correct problem 54 solution

This commit is contained in:
daniele 2019-09-27 10:25:55 +02:00
parent 88a5886ead
commit a0c7f5c6f7
Signed by: fuxino
GPG Key ID: 6FE25B4A3EE16FDA

132
C/p054.c
View File

@ -239,7 +239,7 @@ int eval(char *hands)
/* If player 1 has a Royal Flush, player 1 wins.*/ /* If player 1 has a Royal Flush, player 1 wins.*/
if(cards1[4]->value == ace && cards1[3]->value == king && cards1[2]->value == queen && if(cards1[4]->value == ace && cards1[3]->value == king && cards1[2]->value == queen &&
cards1[1]->value == jack && cards1[0]->value == ten && cards1[0]->suit == cards1[1]->suit &&
cards1[0]->suit == cards1[2]->suit && cards1[0]->suit == cards1[3]->suit && cards1[0]->suit == cards1[2]->suit && cards1[0]->suit == cards1[3]->suit &&
cards1[0]->suit == cards1[4]->suit) cards1[0]->suit == cards1[4]->suit)
{ {
@ -322,16 +322,15 @@ int eval(char *hands)
} }
} }
/* Check if player 1 has a four of a kind.*/ /* Check if player 1 has four of a kind. Since cards are ordered, it is sufficient
if((cards1[0]->value == cards1[1]->value && cards1[0]->value == cards1[2]->value && cards1[0]->value == cards1[3]->value) || * to check if the first card is equal to the fourth or if the second is equal to the fifth.*/
(cards1[1]->value == cards1[2]->value && cards1[1]->value == cards1[3]->value && cards1[1]->value == cards1[4]->value)) if(cards1[0]->value == cards1[3]-> value || cards1[1]->value == cards1[4]->value)
{ {
four1 = 1; four1 = 1;
} }
/* Check if player 2 has a four of a kind.*/ /* Check if player 2 has four of a kind.*/
if((cards2[0]->value == cards2[1]->value && cards2[0]->value == cards2[2]->value && cards2[0]->value == cards2[3]->value) || if(cards2[0]->value == cards2[3]->value || cards2[1]->value == cards2[4]->value)
(cards2[1]->value == cards2[2]->value && cards2[1]->value == cards2[3]->value && cards2[1]->value == cards2[4]->value))
{ {
four2 = 1; four2 = 1;
} }
@ -349,45 +348,29 @@ int eval(char *hands)
} }
/* If both players have four of a kind, check who has the highest value for /* If both players have four of a kind, check who has the highest value for
* those four cards. If they are equal, check the higest value for the different * those four cards.*/
* card.*/
if(four1 && four2) if(four1 && four2)
{ {
if(cards1[1]->value > cards2[1]->value) if(cards1[1]->value > cards2[1]->value)
{ {
return 1; return 1;
} }
else if(cards1[1]->value < cards2[1]->value) if(cards1[1]->value < cards2[1]->value)
{ {
return -1; return -1;
} }
else
{
for(i = 4; i <= 0; i--)
{
if(cards1[i]->value > cards2[i]->value)
{
return 1;
}
if(cards1[i]->value < cards2[i]->value)
{
return -1;
}
}
}
} }
/* Check if player 1 has a full house.*/ /* Check if player 1 has a full house.*/
if(cards1[0]->value == cards1[1]->value && cards1[1]->value == cards1[2]->value && if(cards1[0]->value == cards1[1]->value && cards1[3]->value == cards1[4]->value &&
cards1[1]->value == cards1[3]->value && cards1[1]->value == cards1[4]->value) (cards1[1]->value == cards1[2]->value || cards1[2]->value == cards1[3]->value))
{ {
full1 = 1; full1 = 1;
} }
/* Check if player 2 has a full house.*/ /* Check if player 2 has a full house.*/
if(cards2[0]->value == cards2[1]->value && cards2[1]->value == cards2[2]->value && if(cards2[0]->value == cards2[1]->value && cards2[3]->value == cards2[4]->value &&
cards2[1]->value == cards2[3]->value && cards2[1]->value == cards2[4]->value) (cards2[1]->value == cards2[2]->value || cards2[2]->value == cards2[3]->value))
{ {
full2 = 1; full2 = 1;
} }
@ -406,8 +389,7 @@ int eval(char *hands)
/* If both players have a full house, check who has the highest value /* If both players have a full house, check who has the highest value
* for the three equal cards (the third card in the array will be part * for the three equal cards (the third card in the array will be part
* of the set of three). If they are equal, check the highest value * of the set of three).*/
* for the other two cards.*/
if(full1 && full2) if(full1 && full2)
{ {
if(cards1[2]->value > cards2[2]->value) if(cards1[2]->value > cards2[2]->value)
@ -419,19 +401,6 @@ int eval(char *hands)
{ {
return -1; return -1;
} }
for(i = 4; i >= 0; i--)
{
if(cards1[i]->value > cards2[i]->value)
{
return 1;
}
if(cards1[i]->value < cards2[i]->value)
{
return -1;
}
}
} }
/* Check if player 1 has a flush.*/ /* Check if player 1 has a flush.*/
@ -460,24 +429,6 @@ int eval(char *hands)
return -1; return -1;
} }
/* If both players have a flush, check who has the highest card, if
* the highest is equal check the second highest and so on.*/
if(flush1 && flush2)
{
for(i = 4; i <= 0; i--)
{
if(cards1[i]->value > cards2[i]->value)
{
return 1;
}
if(cards1[i]->value < cards2[i]->value)
{
return -1;
}
}
}
/* Check if player 1 has a straight.*/ /* Check if player 1 has a straight.*/
value = cards1[0]->value; value = cards1[0]->value;
@ -522,19 +473,6 @@ int eval(char *hands)
return -1; return -1;
} }
/* If both players have a straight, check who has the highest card.*/
if(straight1 && straight2)
{
if(cards1[4]->value > cards2[4]->value)
{
return 1;
}
else
{
return -1;
}
}
/* Check if player 1 has three of a kind.*/ /* Check if player 1 has three of a kind.*/
if((cards1[0]->value == cards1[1]->value && cards1[0]->value == cards1[2]->value) || if((cards1[0]->value == cards1[1]->value && cards1[0]->value == cards1[2]->value) ||
(cards1[1]->value == cards1[2]->value && cards1[1]->value == cards1[3]->value) || (cards1[1]->value == cards1[2]->value && cards1[1]->value == cards1[3]->value) ||
@ -564,8 +502,7 @@ int eval(char *hands)
} }
/* If both players have three of a kind, check who has the highest value for /* If both players have three of a kind, check who has the highest value for
* these three cards. If it's equal, check who has the highest value for the * these three cards.*/
* other cards.*/
if(three1 && three2) if(three1 && three2)
{ {
if(cards1[2]->value > cards2[2]->value) if(cards1[2]->value > cards2[2]->value)
@ -577,19 +514,6 @@ int eval(char *hands)
{ {
return -1; return -1;
} }
for(i = 4; i <= 0; i--)
{
if(cards1[i]->value > cards2[i]->value)
{
return 1;
}
if(cards1[i]->value < cards2[i]->value)
{
return -1;
}
}
} }
/* Check if player 1 has two pairs.*/ /* Check if player 1 has two pairs.*/
@ -628,23 +552,20 @@ int eval(char *hands)
{ {
return 1; return 1;
} }
else if(cards1[3]->value < cards2[3]->value)
if(cards1[3]->value < cards2[3]->value)
{ {
return -1; return -1;
} }
else if(cards1[1]->value > cards2[1]->value)
if(cards1[1]->value > cards2[1]->value)
{ {
return 1; return 1;
} }
else if(cards1[1]->value < cards2[1]->value)
if(cards1[1]->value < cards2[1]->value)
{ {
return -1; return -1;
} }
for(i = 4; i <= 0; i--) for(i = 4; i >= 0; i--)
{ {
if(cards1[i]->value > cards2[i]->value) if(cards1[i]->value > cards2[i]->value)
{ {
@ -658,33 +579,33 @@ int eval(char *hands)
} }
} }
/* Check if player 1 has a pair.*/ /* Check if player 1 has a pair of cards*/
if(cards1[0]->value == cards1[1]->value || cards1[1]->value == cards1[2]->value || if(cards1[0]->value == cards1[1]->value || cards1[1]->value == cards1[2]->value ||
cards1[2]->value == cards1[3]->value || cards1[3]->value == cards1[4]->value) cards1[2]->value == cards1[3]->value || cards1[3]->value == cards1[4]->value)
{ {
pair1 = 1; pair1 = 1;
} }
/* Check if player 2 has a pair.*/ /* Check if player 2 has a pair of cards.*/
if(cards2[0]->value == cards2[1]->value || cards2[1]->value == cards2[2]->value || if(cards2[0]->value == cards2[1]->value || cards2[1]->value == cards2[2]->value ||
cards2[2]->value == cards2[3]->value || cards2[3]->value == cards2[4]->value) cards2[2]->value == cards2[3]->value || cards2[3]->value == cards2[4]->value)
{ {
pair2 = 1; pair2 = 1;
} }
/* If player 1 has a pair and player 2 doesn't, player 1 wins.*/ /* If player 1 has a pair of cards and player 2 doesn't, player 1 wins.*/
if(pair1 && !pair2) if(pair1 && !pair2)
{ {
return 1; return 1;
} }
/* If player 2 has a pair and player 1 doesn't, player 2 wins.*/ /* If player 2 has a pair of cards and player 1 doesn't, player 2 wins.*/
if(!pair1 && pair2) if(!pair1 && pair2)
{ {
return -1; return -1;
} }
/* If both players have a pair, check who has the highest pair. Since the cards are /* If both players have a pair of cards, check who has the highest pair. Since the cards are
* ordered by value, either card[1] will be part of the pair (card[0]=card[1] or * ordered by value, either card[1] will be part of the pair (card[0]=card[1] or
* card[1]=card[2]) or card[3] will be part of the pair (card[2]=card[3] or * card[1]=card[2]) or card[3] will be part of the pair (card[2]=card[3] or
* card[3]=card[4]). */ * card[3]=card[4]). */
@ -736,8 +657,7 @@ int eval(char *hands)
return -1; return -1;
} }
} }
else if(cards2[2]->value == cards2[3]->value || cards2[3]->value == cards2[4]->value)
if(cards2[2]->value == cards2[3]->value || cards2[3]->value == cards2[4]->value)
{ {
if(value > cards2[3]->value) if(value > cards2[3]->value)
{ {
@ -752,8 +672,7 @@ int eval(char *hands)
} }
} }
/* If the players have the same pair, or if both don't have a pair, check who has /* If all other things are equal, check who has the highest card, if it's equal check the second highest and so on.*/
* the highest card, if it's equal check the second highest and so on.*/
for(i = 4; i >= 0; i--) for(i = 4; i >= 0; i--)
{ {
if(cards1[i]->value > cards2[i]->value) if(cards1[i]->value > cards2[i]->value)
@ -767,5 +686,6 @@ int eval(char *hands)
} }
} }
/* If everything is equal, return 0.*/
return 0; return 0;
} }