10315 - Poker Hands

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

reggaeguitar
New poster
Posts: 3
Joined: Fri Jan 18, 2013 1:35 am

Re: 10315 - Poker Hands

Post by reggaeguitar »

In this test case, the data posted above states that "White wins."

AH 2H 3H 4H 5H 6H 6D 6C 7C 6S

The uva judge toolkit site also states that white wins. Correct me if I'm wrong but black has a straight flush which beats four of a kind. The answer should be "Black wins."
Also in this test case, the data above and uva judge toolkit site say "Tie", white should win this hand because white has a 6 versus black's 6.

7C 7C 7D 2H 5S 2D 6D 7S 7S 7D
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10315 - Poker Hands

Post by brianfry713 »

Only consider an ace as high in this problem, so A2345 is not a straight. 4 of a kind beats a flush.

Yes white should win the second case, but you can still get AC if you print tie or white wins, so the judge must not have a case like that in it's input.
Check input and AC output for thousands of problems on uDebug!
reggaeguitar
New poster
Posts: 3
Joined: Fri Jan 18, 2013 1:35 am

Re: 10315 - Poker Hands

Post by reggaeguitar »

So how do you know when to count an ace as low in a straight? Should I never count it, or just not against four of a kind? I wish they would fix the results.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10315 - Poker Hands

Post by brianfry713 »

Only consider an ace as high in this problem, so A2345 is never a straight.
Check input and AC output for thousands of problems on uDebug!
vsha041
New poster
Posts: 35
Joined: Wed Feb 12, 2014 10:04 am

Re: 10315 - Poker Hands

Post by vsha041 »

The problem described there is very very different from the real world poker game so don't make any assumptions. Here are the differences:

(By the way all 10 cards are different in the UVA Judge input, I checked that by throwing exceptions)

In real world poker AH 2C 3S 4C 5C is a straight but for this problem it's just Ace high.
In real world poker it is possible for the 2 players to have hands like these - AH AC AS KH KC and AH AC AS TS TC (due to common 5 cards in front of them) and player 1 will win but this input is invalid for this problem.
In real world poker it is possible for the 2 players to have hands like these - AH AC AS AD KC and AH AC AS AD 2C (due to common 5 cards in front of them) and player 1 will win but this input is invalid for this problem.

Same thing applies for "three of a kind", when a player has a higher "three of a kind" then it wins, don't check the remaining two cards to decide the winner because they will never have same "three of a kind" card. In real world poker you will need to but not for this problem. In this problem you cannot have 2 players with the same three of a kind card as that will make 6 cards of 1 rank which is not possible as there can be only 4 cards of a particular rank.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10315 - Poker Hands

Post by brianfry713 »

You're referring to "real world poker" as Texas hold'em, which is just one of many poker variations.
This problem would apply to straight poker, which is another variation of poker also played in the real world - two different five card poker hands drawn from one 52 card deck.
Check input and AC output for thousands of problems on uDebug!
gg004
New poster
Posts: 4
Joined: Mon Feb 17, 2014 9:25 am

Re: 10315 - Poker Hands

Post by gg004 »

In case

Code: Select all

7C 7C 7D 2H 5S 2D 6D 7S 7S 7D
It's sure that White wins.

Why Tie. is accpected?
vsha041
New poster
Posts: 35
Joined: Wed Feb 12, 2014 10:04 am

Re: 10315 - Poker Hands

Post by vsha041 »

brianfry713 wrote:You're referring to "real world poker" as Texas hold'em, which is just one of many poker variations.
This problem would apply to straight poker, which is another variation of poker also played in the real world - two different five card poker hands drawn from one 52 card deck.
Oh okay. Thanks Bryan. I didn't knew that there is a game called Straight Poker. I knew that there are variants of Poker but I assumed that they are talking about Texas Holdem here which they are not. It's clear now :-)
vsha041
New poster
Posts: 35
Joined: Wed Feb 12, 2014 10:04 am

Re: 10315 - Poker Hands

Post by vsha041 »

gg004 wrote:In case

Code: Select all

7C 7C 7D 2H 5S 2D 6D 7S 7S 7D
It's sure that White wins.

Why Tie. is accpected?
Your input is invalid as there are duplicates present in your test case (7c, 7d, 7s are repeated). This kind of test case will never appear in the judge's input.
ngdik
New poster
Posts: 4
Joined: Mon Jun 23, 2014 1:27 pm

10315 Poker Hands

Post by ngdik »

Hi,
I write the code and I test some data and that can be solovd,but i can not get the Accepted.

Code: Select all

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <sstream>
using namespace std;

bool isFlush(char *suit);
bool isStraight(int *card);
int charToNumber(char word);
void info(int *card1,int *card2,char *suit1,char *suit2);
int analysisCards(int *card);

bool cmp(int n,int m)
{
	return n>m;
}

int main()
{

	string line;
	
	int card1[5],card2[5];
	char suit1[5],suit2[5];
	string str;

	while (getline(cin,line))
	{
		istringstream is(line);
		for (int i = 0 ;i<5 ;i++)
		{
			is>>str;
			card1[i] = charToNumber(str[0]);
			suit1[i] = str[1];
		}

		for (int i = 0 ; i<5;i++)
		{
			is>>str;
			card2[i] = charToNumber(str[0]);
			suit2[i] = str[1];
		}
		info(card1,card2,suit1,suit2);
	}

}



void info(int *card1,int *card2,char *suit1,char *suit2)
{
	sort(card1,card1+5,cmp);
	sort(card2,card2+5,cmp);
	bool flush1;
	bool flush2;
	bool straight1;
	bool straight2;


	int result1,result2;
	result1 = analysisCards(card1);
	if (result1<0x1000000)
	{
		flush1 = isFlush(suit1);
		straight1 = isStraight(card1);
		if (flush1 && straight1)
		{
			result1 += 0x8000000;
		}else if(flush1)
		{
			result1+=0x5000000;
		}else if(straight1)
		{
			result1+=0x4000000;
		}
	}

	result2 = analysisCards(card2);

	if (result2<0x1000000)
	{
		flush2 = isFlush(suit2);
		straight2 = isStraight(card2);
		if (flush2 && straight2)
		{
			result2 +=0x8000000;
		}else if(flush2)
		{
			result2 +=0x5000000;
		}else if (straight2)
		{
			result2+=0x4000000;
		}
	}
	if (result1>result2)
	{
		printf("Black wins.\n");
	}else if (result1==result2)
	{
		printf("Tie.\n");
	}else
	{
		printf("White wins\n");
	}

	


}
int analysisCards(int *card)
{
	int tmp = 0;
	int result=0;

	char str[6] = "00000";
	char *p = str;


	tmp = card[0];
	(*p)++;
	
	
	for (int i = 1 ; i < 5;i++)
	{
		if (tmp == card[i])
		{
			(*p)++;
		}
		else
		{
			tmp = card[i];
			p++;
			(*p)++;
			
		}
	}

	string temp = str;

	for(int i = 0 ;i<5;++i)
	{
	
		if (card[i] >=0xA)
		{
			str[i]= card[i] + '7';
		}
		else
		{
			str[i] = card[i] + '0';
		}
	}

	if (temp == "41000")
	{
		result = strtol(str,NULL,16);
		result += 0x7000000;

	}else if(temp == "14000")
	{
		str[4] = str[0]; //47777 to 47774
		str[0] = str[1]; //47774 to 77774
		result = strtol(str,NULL,16);
		result +=0x7000000;

	}else if(temp == "23000")
	{
		str[3] = str[0];
		str[4] = str[0];
		str[0] = str[2]; 
		str[1] = str[2]; 
		result = strtol(str,NULL,16);
		result +=0x6000000;
	}else if(temp == "32000")
	{
		result = strtol(str,NULL,16); 
		result+=0x6000000;
	}else if(temp == "31100")
	{

		result = strtol(str,NULL,16);
		result+=0x3000000;
	}else if(temp== "11300")
	{
		str[3] = str[0]; //87666 to 87686
		str[4] = str[1]; //87686 to 87687
		str[0] = str[2];//87687 to 67687
		str[1] = str[2];//67687 to 66687

		result = strtol(str,NULL,16);
		result+=0x3000000;
	}else if(temp == "13100")
	{

		str[3] = str[0]; //98887 to 98897
		str[0] = str[1]; //98897 to 88897
		result = strtol(str,NULL,16);
		result+=0x3000000;

	}else if(temp == "22100")
	{
		result = strtol(str,NULL,16);
		result +=0x2000000;

	}else if(temp== "21200")
	{
		int a;
		str[4] = str[2];//88767
		str[2] = str[3];//88667
		result = strtol(str,NULL,16);
		result +=0x2000000;

	}else if (temp == "12200")
	{
		str[4] = str[0]; //87768
		str[0] = str[1];//77768
		str[2] = str[3];//77668
		result = strtol(str,NULL,16);
		result+=0x2000000;
	}else if (temp == "21110")
	{
		result = strtol(str,NULL,16);
		result+=0x1000000;
	}else if(temp=="12110")
	{
		str[2] = str[0];
		str[0] = str[1];
		result = strtol(str,NULL,16);
		result+=0x1000000;
	}else if(temp == "11210")
	{
		str[2] = str[1];
		str[1] = str[0];
		str[0] = str[3];
		str[3] = str[2];
		str[2] = str[1];
		str[1] = str[0];
		result = strtol(str,NULL,16);
		result+=0x1000000;
	}else if(temp == "11120")
	{
		int asd;
		str[4] =str[2];//43212
		str[2] = str[3];//43112
		str[3] = str[1];//43132
		str[1] = str[0];//44132
		str[1] = str[2];//41132
		str[2] = str[0];//41432
		str[0] = str[1];//11432
		result = strtol(str,NULL,16);
		result+=0x1000000;
	}
	else
	{
		result = strtol(str,NULL,16);
	}
	return result;
}

int charToNumber(char word)
{
	switch (word)
	{
	case 'T':
		return 0xA;
	case 'J':
		return 0xB;
	case 'Q':
		return 0xC;
	case 'K':
		return 0xD;
	case 'A':
		return 0xE;
	default:
		return word-'0';
	}
}
bool isFlush(char *suit)
{
	char k = suit[0];
	for (int i = 1 ; i < 5 ; i++)
	{
		if (suit[i]!= k)
		{
			return false;
		}
	}
	return true;
}

bool isStraight(int *card)
{
	int k = card[0];
	for (int i = 1 ; i< 5;i++)
	{
		if (k != card[i]+1)
		{
			return false;
		}
		k = card[i];
	}
	return true;
}
Last edited by brianfry713 on Mon Jun 23, 2014 10:11 pm, edited 1 time in total.
Reason: Wrong problem number
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10315 Poker Hands

Post by brianfry713 »

White wins should have a period at the end of the line.
Check input and AC output for thousands of problems on uDebug!
ngdik
New poster
Posts: 4
Joined: Mon Jun 23, 2014 1:27 pm

Re: 10315 Poker Hands

Post by ngdik »

brianfry713 wrote:White wins should have a period at the end of the line.
Very Thanks!!!!
I'm Accepted!!!
fanya
New poster
Posts: 1
Joined: Tue Dec 02, 2014 12:22 am

10315 - Poker Hands

Post by fanya »

Could someone please explain why White wins this:
TH TD TS TC KD 9H 9D 9S 9C JD

Both are Four of a Kind and black has 10 while white has 9. Shouldn't Black win?
I also need help with this one also:
TS TC JH 2H TH TD 9D 9H 9C 3C

Thank you.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10315 - Poker Hands

Post by brianfry713 »

Both of your inputs should print Black Wins.
I fixed:
http://www.udebug.com/UVa/10315

Also it appears the judge's input does have hands with repeated cards.
Check input and AC output for thousands of problems on uDebug!
mohdali231993
New poster
Posts: 11
Joined: Sun Nov 09, 2014 6:46 pm

Re: 10315 - Poker Hands

Post by mohdali231993 »

Code: Select all

#include<iostream>
typedef unsigned int uint;
using namespace std;
int main()
{ 
  uint scount=0,s=0;
  uint i=0,maxa=0,maxb=0;
  char c;uint cc=0;
  while(cin>>c)
  { scount=0;cc=0;maxa=0;maxb=0;s=0;i=0;
    uint value [100]={ },vvalue [100]={ }, a [15]={ }, b [15]={ };
    while(true)
    { 
      scount++;
      if(scount<=10)value[c-48]++;
      else vvalue[c-48]++;
      if(scount==20)break;  
      cin>>c;            
    } 
    value[10]=value[36];vvalue[10]=vvalue[36];
    value[11]=value[26];vvalue[11]=vvalue[26];
    value[12]=value[33];vvalue[12]=vvalue[33];
    value[13]=value[27];vvalue[13]=vvalue[27];
    value[14]=value[17];vvalue[14]=vvalue[17];
    i=14;s=0;
    if(value[19]==5 || value[20]==5 || value[35]==5 || value[24]==5) {a[4]++;}//black flush
    if(vvalue[19]==5 || vvalue[20]==5 || vvalue[35]==5 || vvalue[24]==5) {b[4]++;}//white flush
    while(i)
    {     
      if((!value[i] && vvalue[i]) || (value[i] && !vvalue[i])) //assigning highcards
      {
             
       if(!a[0] && value[i]){a[0]=i;}              
       if(!b[0] && vvalue[i]){b[0]=i;}
      }
      if(value[i])
      {  
      s++;
       if(s==5)
       {
        a[3]++;if(maxa<3){maxa=3;}//black straight
       }}
     else s=0;
     if(value[i]==2)
       {a[1]++;a[8+a[1]]=i;if(maxa<1)maxa=1;}//black pair and two pairs
     else if(value[i]==3)
       {a[2]++;a[10]=i;if(maxa<2)maxa=2;}  //black three of a kind
     else if(value[i]>=4)
      { a[6]++;a[11]=i;if(maxa<6)maxa=6;} //black four of a kind
     i--;           
    }
    if(a[1] && a[2]) {a[5]++;if(maxa<5)maxa=5;}//black full house
    if(a[3] && a[4]) {a[7]++;if(maxa<7)maxa=7;}//black straight flush
      i=14;s=0;
      while(i)
    {     
     
     if(vvalue[i])
     {  
       s++;
       if(s==5)
       {
        b[3]++;if(maxb<3){maxb=3;} //white straight
       } }
     else s=0;
     if(vvalue[i]==2)
       {b[1]++;b[8+b[1]]=i;if(maxb<1)maxb=1;}//white pair and two pairs
     else if(vvalue[i]==3)
       {b[2]++;b[10]=i;if(maxb<2)maxb=2;}  //white three of a kind
     else if(vvalue[i]>=4)
      { b[6]++;b[11]=i;if(maxb<6)maxb=6;}//white four of a kind
     i--;           
    }
    if(b[1] && b[2])
      {b[5]++;if(maxb<5)maxb=5;}//white full house
    if(b[3] && b[4])
      {b[7]++;if(maxb<7)maxb=7;}//white straight flush
      
      if(maxa>maxb)
      cout<<"Black wins."<<endl;
      else if(maxb>maxa)
      cout<<"White wins."<<endl;
      else
      {
        if(maxa==7) cc=0;
        else if(maxa==6) cc=11;
        else if(maxa==3 || maxa==4) cc=0;
        else if(maxa==5) cc=10;          
        else if(maxa==2) cc=10;
        else if(maxa==1) cc=9;
        else cc=0;
      
        if(a[cc]>b[cc])
          cout<<"Black wins."<<endl;
        else if(b[cc]>a[cc])
          cout<<"White wins."<<endl;
        else
        {
          if(maxa==1)
            {
              if(a[8]>b[8])cout<<"Black wins."<<endl;
              else if(b[8]>a[8])cout<<"White wins."<<endl;
              else{ 
                    
                    
                    if(a[0]>b[0])cout<<"Black wins."<<endl;
              else if(b[0]>a[0])cout<<"White wins."<<endl;
                   else cout<<"Tie."<<endl;
              }                      
            }
          else  
            cout<<"Tie."<<endl;
         }
      }
  }             
  return 0;
}     

Post Reply

Return to “Volume 103 (10300-10399)”