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

liangchene
New poster
Posts: 12
Joined: Thu May 19, 2005 6:07 am

Post by liangchene »

Thanks a lot.

it got accepted.

there's this bug in my program.

when you do the big shift, you have to have brackets around it

for example:

1+(2<<2) is different from 1+2<<2.

Strange.

anywayz, thx
Marcin Panasiuk
New poster
Posts: 1
Joined: Fri Feb 24, 2006 5:51 pm

10315 Interpretation problemas with straight

Post by Marcin Panasiuk »

Pablo:

Your program works quite good but only for cards from one deck. Unfortunatelly, there are tests when one hands has two cards, which are exactly the same. Problem appears for example when you have a flush, and simultanously a pair or two pairs... and so on. The value returning by function gamePoints() is much more higher than for example full house, four of a house or even flush straight.

Try this input:
3H 5H AH AH 3H 2H 2C AC AD 2D
Your output is "Black wins.", but there should be "White wins."

PS. Btw, this problem is tricky, or rather tests are tricky, since by "cards dealt from the deck" one can think there are no duplicates.
"A designer knows he had achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
devious
New poster
Posts: 13
Joined: Wed Feb 22, 2006 1:26 am

10315: Poker Hands (clarification questions)

Post by devious »

So....

Four of a Kind:

It says to rank by value of four cards if both have a four of a kind. Can there be more than one deck? Because it doesn't say there can't, and if so, would I rank the hand with the 5th card being the kicker(deciding card)?

Full House:

More or less the same as above, but if They are same three cards, do I rank by the other 2 cards? so is a QQQKK higher than a QQQJJ? Or are they the same?

Flush:

It says to use rule sof highest card. That would mean a 3579A flush is higher than a 2579A flush, right?

Straight:

It says it ranks it by its highest card, is an Ace ever considered a 1 in this program? If so, an A2345 Straight loses to a 23456 straight. But if it is not considered a 1, then it would win



Thanks ^_^
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko »

IIRC, there is only one deck.

For the flush - yes. Sort hands in descending order and compare cards one by one.

For the straight - A2345 is not a straight in this problem (as described, 2 is lowest, A is highest)

Darko
nev4
New poster
Posts: 15
Joined: Sun Apr 30, 2006 10:19 am
Location: Lithuania

Post by nev4 »

Thx, Jan for sample input. It revealed that i have used > instead of < in one place of code.
Germano
New poster
Posts: 2
Joined: Sat Oct 16, 2004 9:15 am

more tests case

Post by Germano »

i did some more test cases to test my problem:
2S 3S 4S 5S 6S 7H 8H 9H TH JH
5C 5C 2H 4H 6H 5C 5C 2H 4H 6H
AS KS JH TC 9S AH KS JS 9H 8C
5C 5C 2H 4H 6H 4H 4H 2C 5H 6H
7C 7C 2H 5H 6H 7H 7H 2C 4H 6H
7C 7C 3H 5H 6H 7H 7H 2C 5H 6H
7C 7C 3H 5H 6H 7H 7H 3C 5H 6H
2C 2C 4H 4H 5S 4H 4H 3S 3S 5S
2C 2C 4H 4H 6S 4H 4H 2S 2S 5S
2C 2C 4H 4H 5S 4H 4H 2S 2S 5S
7C 7C 7D 2H 5S 2D 6D 7S 7S 7D
8C 8C 8D 2H 5S 2D 6D 7S 7S 7D
7C 3D 4H 5S 6C 2C 3D 4H 5S 6C
3C 4C 6C 8C TC 2C 4C 6C 8C TC
3C 4C 6C 8C TC 3C 4C 6C 8C TC
3C 3D 4H 4S 4C 3C 3D 4H 4S 4C
3C 3D 5H 5S 5C 3C 3D 4H 4S 4C
2C 2D 5H 5S 5C 3C 3D 4H 4S 4C
9C 9D 9H 9S 2C 9C 9D 9H 9S 2C
TC TD TH TS 2C 9C 9D 9H 9S 2C
3S 4S 5S 6S 7S 2S 3S 4S 5S 6S
2S 3S 4S 5S 6S 2S 3S 4S 5S 6S

my acc code returns:
Black wins.
Black wins.
White wins.
Tie.
Black wins.
Black wins.
Black wins.
Black wins.
Tie.
Tie.
Black wins.
Tie.
Tie.
Black wins.
Black wins.
Black wins.
Tie.
Tie.
Black wins.
Black wins.
Tie.
Black wins.
Black wins.
Tie.
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja »

It's really tricky... I can't solve this problem though all of Jan's test case
passed...

If anyone have critical test case for this problem, plz tell me about it.
And I have another question.
Full House: 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
Four of a kind: 4 cards with the same value. Ranked by the value of the 4 cards.
That was problem statements. According to this script, Full House or Four
of a Kind would not count remaining card for rank. Is it right or wrong??
I love Problem Solving!!! :) :) :)
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

soyoja wrote:
Full House: 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
Four of a kind: 4 cards with the same value. Ranked by the value of the 4 cards.
That was problem statements. According to this script, Full House or Four
of a Kind would not count remaining card for rank. Is it right or wrong??
Its obviously right. All of the cards are from a single set, so a card can't occur twice. Now suppose you have a Full-House. 5H 5D 5S 3D 3H. If there is another full house with three '5' and a pair, then you would check the pair. But there is only one '5' left, so you cant get it. And same reason for 'Four of a Kind'.

However, here are some cases. If your code passes then you can post your code.

Input:

Code: Select all

3H 4S 8S KD AS QC QH 4H 6D 5S
9S 3C 7D 2S JH AD 4D JS 6H 6D
KH 6C 7S TH 5S 3S 7C 5H AC 9D
6H KD 3D 6D QS 4C 5C 9D 6S KH
9D JS AS 9S 6H 5H KD QD JC JH
QD 7D 3D 6D 4C 2S 5C JC 2D TS
9H 9D 2S QD 3S 5D QH 3C AH 9C
9D 3S AD QD 8S 4H KC 8H TC 2D
9S 2S 8C TH KS AD 2H 3D JS 6H
JC AH QD TS 8H 9C TH JS 8C AD
JC KH KS 4D 2S 5C QD 8C JD AS
KS QC 7H JC 3C TD 5C 5S 8C 9C
3S 2H 3C 6S QS 9C 7H 4S 8C QH
9S QC TD 2D 6C KH 2H JH 3S KC
JS 8D KD AS 5D 6C JD 7D 7H 3S
QD AC 5C 3D JD 3C 6H JC TD 7S
7D JH 4D KC 5C 3D 9S 7C 3H 6H
9H TH 9C TD KD 7C 9S AH 8S 5C
8S JD QS 5D QH TS KH 8C 4S 9D
QD JD AH KD 5H 5D 8S TD QS KC
TD JS AC KS 3S 7D KD 2H 2D QS
TD 5S 7H KC 4C TC 9S QC JC QD
JD KD QH 4C 9S TS 7S 8H JC QD
KH AH QH 2H 3H AD 2D 3D 4D 5D
Output:

Code: Select all

White wins.
White wins.
White wins.
Black wins.
White wins.
White wins.
Black wins.
Black wins.
White wins.
Black wins.
Black wins.
White wins.
Black wins.
White wins.
White wins.
Black wins.
White wins.
Black wins.
Black wins.
Black wins.
White wins.
White wins.
Black wins.
Black wins.
Hope these help.
Ami ekhono shopno dekhi...
HomePage
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja »

My solution still received WA..
Thx, Jan. My program passed all of your test case, but it cannot pass the
OJ's test. I found one critical input, so I modified my code. But I recieved
WA..

My critical Input
4C 4C 8C 7C 5C 3D 3D 8D 7D 6D
In this test case, both hands are flush. According to the problem statement,
hands which are both flushes are ranked using the rules for High Card.(Do not consider remaining pairs of cards! )
So my answer is
White wins.

My solution received WA repetedly.... Here is my code...
Sorry. It's so dirty and complicated..

Code: Select all


#include <iostream>
#include <algorithm>
#include <string>
#include <memory>

using namespace std;

struct data
{
    int same;		// count same card numbers
    int pair;		// count the numbers of pair
	bool straight;	// count it is straight or not
    bool equal;		// count it is flush or not
};

data Rank[9];

int comp(string dat1, string dat2 )
{
	if( dat1[0] > dat2[0] ) return 1;
	else return 0;
}

int check(string *card, int *numbers, int *pa)
{
    int a, cnt = 0, same = 0, grade = 0;		
	bool isStraight;
    data score;
	score.equal = score.straight = false, score.pair = score.same = 0; // initializing

	isStraight = true;
	sort( &card[0], &card[5], comp );

	for( a = 0; a < 5; a++ ) 
	{
		if( card[a][0] >= 'A' && card[a][0] <= 'Z' ) numbers[a] = card[a][0] - 'A' + 10;
		else numbers[a] = card[a][0] - '0';

		if( a >= 1 )
		{
			if( pa[0] != 0 )	// pa saved pairs number. 
			{
				if( numbers[a] != pa[0] ) 
				{
					if( numbers[a] == numbers[a-1] ) same++, pa[1] = numbers[a];
				}
				else 
				{
					same++;
				}
			}
			else 
			{
				if( numbers[a] == numbers[a-1] ) same++, pa[0] = numbers[a];
			}

			if( numbers[a-1] - numbers[a] != 1 ) isStraight = false;
		}
	}
	if( isStraight ) score.straight = true;

    for( a = 0; a < 5; a++ )	// for check flush
    {
        if( card[0][1] == card[a][1] ) cnt++;
    }

    score.same = same;
	if( pa[1] ) score.pair = 2;
	else if( pa[0] ) score.pair = 1;           
	if( cnt == 5 ) score.equal = true; 

	if( score.straight ) grade = 4;		
	if( score.equal ) grade = 5;		
	for( a = 1; a < 9; a++ ) 	// decide the rank; 
	{
		if( score.straight == Rank[a].straight && score.equal == Rank[a].equal &&
			score.same == Rank[a].same && score.pair == Rank[a].pair && (grade < a ) )
			grade = a; 
	}

	return grade;
}

int main()
{
    int a, grade1, grade2, numbers1[5], numbers2[5], pa1[2], pa2[2];
    string white[5], black[5];

    Rank[1].straight = false, Rank[1].equal = false, Rank[1].same = 1, Rank[1].pair = 1;  // one pair
    Rank[2].straight = false, Rank[2].equal = false, Rank[2].same = 2, Rank[2].pair = 2;  // two pair
    Rank[3].straight = false, Rank[3].equal = false, Rank[3].same = 2, Rank[3].pair = 1;  // Three of a kind
    Rank[4].straight = true,  Rank[4].equal = false, Rank[4].same = 0, Rank[4].pair = 0;  // straight
    Rank[5].straight = false, Rank[5].equal = true,  Rank[5].same = 0, Rank[5].pair = 0;  // flush
    Rank[6].straight = false, Rank[6].equal = false, Rank[6].same = 3, Rank[6].pair = 2;  // full house
    Rank[7].straight = false, Rank[7].equal = false, Rank[7].same = 3, Rank[7].pair = 1;  // Four of a kind
    Rank[8].straight = true,  Rank[8].equal = true,  Rank[8].same = 0, Rank[8].pair = 0;  // straight flush

    while( cin )
    {
        for( a = 0; a <= 4; a++ ) 
		{
			cin >> black[a];
			if( black[a][0] == 'T' ) black[a][0] = 'A';		// for easy using, convert char.
			else if( black[a][0] == 'J' ) black[a][0] = 'B';
			else if( black[a][0] == 'Q' ) black[a][0] = 'C';
			else if( black[a][0] == 'K' ) black[a][0] = 'D';
			else if( black[a][0] == 'A' ) black[a][0] = 'E';
		}

        for( a = 0; a <= 4; a++ ) 
		{
			cin >> white[a];
			if( white[a][0] == 'T' ) white[a][0] = 'A';
			else if( white[a][0] == 'J' ) white[a][0] = 'B';
			else if( white[a][0] == 'Q' ) white[a][0] = 'C';
			else if( white[a][0] == 'K' ) white[a][0] = 'D';
			else if( white[a][0] == 'A' ) white[a][0] = 'E';
		}

		memset( pa1, 0, sizeof(pa1) );
		memset( pa2, 0, sizeof(pa2) );
		memset( numbers1, 0, sizeof(numbers1) );
		memset( numbers2, 0, sizeof(numbers2) );

		grade1 = check( black, numbers1, pa1 );
		grade2 = check( white, numbers2, pa2 );

		sort( &pa1[0], &pa1[2] );
		sort( &pa2[0], &pa2[2] );

		if( grade1 == 5 && grade2 == 5 )	
		{
			memset( pa1, 0, sizeof(pa1) );
			memset( pa2, 0, sizeof(pa2) );			
		}

		if( grade1 > grade2 ) cout << "Black wins." << endl;
		else if( grade1 < grade2 ) cout << "White wins." << endl;
		else 
		{
			if( pa1[1] > pa2[1] ) cout << "Black wins." << endl;
			else if( pa1[1] < pa2[1] ) cout << "White wins." << endl;
			else 
			{
				if( pa1[0] > pa2[0] ) cout << "Black wins." << endl;
				else if( pa1[0] < pa2[0] ) cout << "White wins." << endl;
				else 
				{
					for( a = 0; a < 5; a++ ) 
					{
						if( numbers1[a] > numbers2[a] ) 
						{
							cout << "Black wins." << endl;
							break;
						}
						else if( numbers1[a] < numbers2[a] ) 
						{
							cout << "White wins." << endl;
							break;
						}
						else if( a == 4 ) cout << "Tie." << endl;
					}
				}
			}
		}
	}

    return 0;
} 
I love Problem Solving!!! :) :) :)
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

I ran your code from an input file and it returned one extra answer at the end.
May be the following line is the reason...

Code: Select all

while( cin )
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
skysbsb
New poster
Posts: 1
Joined: Fri May 11, 2007 12:46 am

Post by skysbsb »

i am trying solvind this problem with C++.. i try hard and not take any accepted submissions...

so, i get this source, in C.. submited and its ok!

Code: Select all

#include <stdio.h>
#define get_value(x) ((x) / 10)
#define get_suit(x) ((x) % 10)

int encode_card(char *card)
{
	int result;
	switch (card[0]) {
	case 'T': result = 100; break;
	case 'J': result = 110; break;
	case 'Q': result = 120; break;
	case 'K': result = 130; break;
	case 'A': result = 140; break;
	default : result = (card[0] - '0') * 10;
	}
	switch (card[1]) {
	case 'H': result += 1; break;
	case 'D': result += 2; break;
	case 'S': result += 3; break;
	case 'C': result += 4; break;
	}
	return result;
}

long get_hand_value(int hand[5])
{
	int i, j, max, temp;
	int value[5], suit[5];
	long result;

	for (i = 0; i < 4; i++) {
		max = i;
		for (j = i + 1; j < 5; j++)
			if (hand[j] > hand[max])
				max = j;
		temp = hand[i];
		hand[i] = hand[max];
		hand[max] = temp;
	}
	for (i = 0; i < 5; i++)
		{
		value[i] = get_value(hand[i]);
		suit[i] = get_suit(hand[i]);
	}
	/* straight flush */
	if (value[1] + 1 == value[0] && suit[1] == suit[0]
		&& value[2] + 2 == value[0] && suit[2] == suit[0]
		&& value[3] + 3 == value[0] && suit[3] == suit[0]
		&& value[4] + 4 == value[0] && suit[4] == suit[0])
		result = (9 << 20) + (value[0] << 16);
	/* four of a kind.*/
	else if (value[0] == value[3] || value[1] == value[4])
		result = (8 << 20) + (value[1] << 16);
	/* full house. */
	else if (value[0] == value[2] && value[3] == value[4]) 
		result = (7 << 20) + (value[0] << 16);
	else if (value[0] == value[1] && value[2] == value[4]) 
		result = (7 << 20) + (value[2] << 16);
	/* flush */
	else if (suit[1] == suit[0] && suit[2] == suit[0]
		&& suit[3] == suit[0] && suit[4] == suit[0])
		result = (6 << 20) + (value[0] << 16) + (value[1] << 12) + (value[2] << 8) + (value[3] << 4) + value[4];
	/* straight */
	else if (value[1] + 1 == value[0] && value[2] + 2 == value[0]
		&& value[3] + 3 == value[0] && value[4] + 4 == value[0])
		result = (5 << 20) + (value[0] << 16);
	/* three of a kind */
	else if (value[0] == value[2] || value[1] == value[3]
		|| value[2] == value[4])
		result = (4 << 20) + (value[2] << 16); 
	/* two pairs*/
	else if (value[0] == value[1] && value[2] == value[3])
		result = (3 << 20) + (value[1] << 16) + (value[3] << 12) + (value[4] << 8);
	else if (value[0] == value[1] && value[3] == value[4])
		result = (3 << 20) + (value[1] << 16) + (value[3] << 12) + (value[2] << 8);
	else if (value[1] == value[2] && value[3] == value[4])
		result = (3 << 20) + (value[1] << 16) + (value[3] << 12) + (value[0] << 8);
	/* pair */
	else if (value[0] == value[1])
		result = (2 << 20) + (value[2] << 16) + (value[3] << 12) + (value[4] << 8);
	else if (value[1] == value[2])
		result = (2 << 20) + (value[0] << 16) + (value[3] << 12) + (value[4] << 8);
	else if (value[2] == value[3])
		result = (2 << 20) + (value[0] << 16) + (value[1] << 12) + (value[4] << 8);
	else if (value[3] == value[4])
		result = (2 << 20) + (value[0] << 16) + (value[1] << 12) + (value[2] << 8);

	/* high card */
	else
		result = (1 << 20) + (value [0] << 16) + (value[1] << 12) + (value[2] << 8) + (value[3] << 4) + value[4];

	return result;
}


int main(void)
{
	char line[100];
	int hand[2][5];
	long hand_value[2];
	int i, j, t;
	while (gets(line) && *line) {
	t = 0;
	for (i = 0; i < 2; i++) {
	for (j = 0; j < 5; j++) {
	while (line[t] == ' ')
	t++;
	hand[i][j] = encode_card(line + t);
	t += 2;
	}
	hand_value[i] = get_hand_value(hand[i]);
	}
	if (hand_value[0] > hand_value[1])
	puts("Black wins.");
	else if (hand_value[0] < hand_value[1])
	puts("White wins.");
	else
	puts("Tie.");
	}
}

/* @END_OF_SOURCE_*/


i make a program to gen some combinations of hands..
2S 5S 5S KS TD 3C 5D KH KH QD

who win this input?
black = 2S 5S 5S KS TD pair of 5
white = 3C 5D KH KH QD pair of K

? white have the biger pair (K), but this C source says black wins.. why UVA judge accepted this source? in case of pair, the biger pair have to win?

i just don't understood..
Codemonkey2000
New poster
Posts: 9
Joined: Sun Dec 09, 2007 6:46 am

Post by Codemonkey2000 »

Hi, I'm wondering, is there a more efficient way of determining a hand. I was thinking about looking at the probability of getting a hand, and seeing if it matches the probability of getting say a flush.
CMG
New poster
Posts: 37
Joined: Sat Dec 08, 2007 5:01 am
Location: ...

Post by CMG »

Not sure exactly Codemonkey2000.

However I keep getting WA and I have no clue why. I have done all of Jan's inputs and all match. Here is what I have for Java.

Code: Select all

public class Main {
    private static int[] xlat = {0,0,0,1,2,3,4,5,6,7,0,0,0,0,0,0,0,12,0,0,1,0,0,0,2,0,9,11,0,0,0,0,0,10,0,3,8};
    
    public static void main(String args[]) {
        int i,p,c,high,sp,sc,black,white,j;
        int[][] suits;
        int[][] cards;
        int[][] kind;
        while(true) {
            suits = new int[2][4];
            cards = new int[2][13];
            kind = new int[2][2];
            black = 0;
            white = 0;
            try {
                i = 0;
                p = 0;
                while(i<10) {
                    if(i==5) p++;
                    c = System.in.read();
                    while(c>0 && (c<0x32 || c>0x54)) {
                        c = System.in.read();
                    }
                    cards[p][xlat[c-0x30]]++;//determine amount of each number
                    suits[p][xlat[System.in.read()-0x30]]++;//determine amount of each suit
                    i++;
                }
            } catch(Exception e) {
                return;
            }
            high = 0;
            sp = 0;
            sc = 0;
            for(i=12;i>=0;i--) {
                p = cards[0][i];
                c = cards[1][i];
                j = high<0?-high:high; //determine which hand is better
                if(p>c) {
                    if(p>j) {
                        high = p;
                    }
                } else if(c>p) {
                    if(c>j) {
                        high = -c;
                    }
                }
                if(p==1) {//check if black has striaght
                    sp++;
                    if(sp==5) {
                        black += 13;
                    }
                } else {
                    if(sp>0) {
                        sp--;
                    }
                }
                if(c==1) {//check if white has striaght
                    sc++;
                    if(sc==5) {
                        white += 13;
                    }
                } else {
                    if(sc>0) {
                        sc--;
                    }
                }
                if(p>kind[0][0]) {//determine if black has any multiples
                    kind[0][1] = kind[0][0];
                    kind[0][0] = p;
                } else if(p>kind[0][1]) {
                    kind[0][1] = p;
                }
                if(c>kind[1][0]) {//determine if white has any multiples
                    kind[1][1] = kind[1][0];
                    kind[1][0] = c;
                } else if(c>kind[1][1]) {
                    kind[1][1] = c;
                }
            }
            for(i=0;i<4;i++) {
                p = suits[0][i];
                c = suits[1][i];
                if(p==5) {
                    black += 14;
                }
                if(c==5) {
                    white += 14;
                }
            }
            black += (2*kind[0][0]+kind[0][1]-3)*3;//high card=0, pair = 6, two pair = 9, three of a kind = 12, full house = 15, four of a kind = 18;
            white += (2*kind[1][0]+kind[1][1]-3)*3;
            if(white>black) {
                System.out.println("White wins.");
            } else if(black>white) {
                System.out.println("Black wins.");
            } else {
                if(high>0) {
                    System.out.println("Black wins.");
                } else if(high<0) {
                    System.out.println("White wins.");
                } else {
                    System.out.println("Tie.");
                }
            }
        }
    }
}
taz_debian
New poster
Posts: 1
Joined: Sat May 31, 2008 6:02 am

Re: 10315 - Poker Hands

Post by taz_debian »

my question is :
A2345 is straight??
diego_engcomp
New poster
Posts: 9
Joined: Sat Jul 18, 2009 1:18 am

Re: 10315 - Poker Hands

Post by diego_engcomp »

Hi, I don't now why I am getting WA on this problem. My code passed in all test cases on this forum and I have a friend that get AC on this problem and I made 30 test cases and my code and the code of him had the same result. Both codes do practically the same thing but in different ways. Its a big code buts is easy to understand. Can anybody tell me what I do wrong? Thanks and sorry for my english.

Code: Select all

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

void high_card(vector<int> v_black, vector<int> v_white){
     int ncards=0, i=12, j=12;
     while(true){
        for( ; ;i--){
             if(v_black[i]>0){
                v_black[i]--;              
                break;
             }
        }
        for( ; ;j--){
             if(v_white[j]>0){
                v_white[j]--;              
                break;
             }
        }
        ncards++;
        if(i!=j){
           if(i>j)
              cout<<"Black wins."<<endl;
           else
              cout<<"White wins."<<endl;           
           break;
        }
        if(ncards==5){
           cout<<"Tie."<<endl;
           break;
        }
     }
}

bool one_pair(vector<int> &v_black, vector<int> &v_white){
     int b_points=0, w_points=0;
     vector<int>::iterator ite;
     if ((ite=find(v_black.begin(),v_black.end(),2))!=v_black.end())     
           b_points=ite-v_black.begin()+2;
     if ((ite=find(v_white.begin(),v_white.end(),2))!=v_white.end())     
           w_points=ite-v_white.begin()+2;
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           high_card(v_black,v_white);           
        return true;
     }
     return false;
}

bool two_pairs(vector<int> v_black, vector<int> v_white){
    int b_hpair=0, b_lpair=0, b_rcard=0, w_hpair=0, w_lpair=0, w_rcard=0;     
    for(int i=0; i<13; i++){
         if(v_black[i]==2){
            b_lpair=b_hpair;
            b_hpair=i+2;
         }
         if(v_black[i]==1)
            b_rcard=i+2;
    }
    for(int i=0; i<13; i++){         
         if(v_white[i]==2){            
            w_lpair=w_hpair;
            w_hpair=i+2;
         }
         if(v_white[i]==1)
            w_rcard=i+2;
    }
    if((b_hpair!=0 && b_lpair!=0) || (w_hpair!=0 && w_lpair!=0)){
        if(b_hpair>w_hpair)
           cout<<"Black wins."<<endl;
        else if (w_hpair>b_hpair)
           cout<<"White wins."<<endl;
        else{
           if(b_lpair>w_lpair)
              cout<<"Black wins."<<endl;
           else if (w_lpair>b_lpair)
              cout<<"White wins."<<endl;
           else{
              if(b_rcard>w_rcard)
                 cout<<"Black wins."<<endl;
              else if (w_rcard>b_rcard)
                 cout<<"White wins."<<endl;
              else
                 cout<<"Tie."<<endl;
           }   
        }
        return true;
     }
     return false;
}

bool three_of_a_kind(vector<int> &v_black, vector<int> &v_white){
     int b_points=0, w_points=0;
     vector<int>::iterator ite;
     if ((ite=find(v_black.begin(),v_black.end(),3))!=v_black.end())     
           b_points=ite-v_black.begin()+2;
     if ((ite=find(v_white.begin(),v_white.end(),3))!=v_white.end())     
           w_points=ite-v_white.begin()+2;  
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           cout<<"Tie."<<endl;
        return true;
     }
     return false;
}

bool straight(vector<int> &v_black, vector<int> &v_white){
     int b_points=0, w_points=0;     
     for(int i=0; i<9; i++){
        if(v_black[i]==1){
           if((v_black[i+1]==1) && (v_black[i+2]==1) && (v_black[i+3]==1) && (v_black[i+4]==1))
              b_points=i+6;
           break;           
        }
     }     
     for(int i=0; i<9; i++){
        if(v_white[i]==1){
           if((v_white[i+1]==1) && (v_white[i+2]==1) && (v_white[i+3]==1) && (v_white[i+4]==1))
              w_points=i+6;
           break;           
        }
     }
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           cout<<"Tie."<<endl;
        return true;
     }
     return false;
}

bool flush(vector<int> &v_black, vector<int> &s_black, vector<int> &v_white, vector<int> &s_white){
     int b_points=0, w_points=0;
     if (find(s_black.begin(),s_black.end(),5)!=s_black.end())
        b_points=1;
     if (find(s_white.begin(),s_white.end(),5)!=s_white.end())
        w_points=1;
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           high_card(v_black,v_white);
        return true;
     }
     return false;
}

bool full_house(vector<int> &v_black, vector<int> &v_white){
     int b_points=0, w_points=0;
     vector<int>::iterator ite;
     if (((ite=find(v_black.begin(),v_black.end(),3))!=v_black.end()) && ((find(v_black.begin(),v_black.end(),2))!=v_black.end()))     
           b_points=ite-v_black.begin()+2;
     if (((ite=find(v_white.begin(),v_white.end(),3))!=v_white.end()) && ((find(v_white.begin(),v_white.end(),2))!=v_white.end()))     
           w_points=ite-v_white.begin()+2;  
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           cout<<"Tie."<<endl;
        return true;
     }
     return false;
}

bool four_of_a_kind(vector<int> &v_black, vector<int> &v_white){
     int b_points=0, w_points=0;
     vector<int>::iterator ite;
     if ((ite=find(v_black.begin(),v_black.end(),4))!=v_black.end())     
           b_points=ite-v_black.begin()+2;
     if ((ite=find(v_white.begin(),v_white.end(),4))!=v_white.end())     
           w_points=ite-v_white.begin()+2;  
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           cout<<"Tie."<<endl;
        return true;
     }
     return false;
}

bool straight_flush(vector<int> &v_black, vector<int> &s_black, vector<int> &v_white, vector<int> &s_white){
     int b_points=0, w_points=0;
     if (find(s_black.begin(),s_black.end(),5)!=s_black.end()){
        for(int i=0; i<9; i++){
           if(v_black[i]==1){
              if((v_black[i+1]==1) && (v_black[i+2]==1) && (v_black[i+3]==1) && (v_black[i+4]==1))
                 b_points=i+6;
              break;
           }
        }
     }
     if (find(s_white.begin(),s_white.end(),5)!=s_white.end()){
        for(int i=0; i<9; i++){
           if(v_white[i]==1){
              if((v_white[i+1]==1) && (v_white[i+2]==1) && (v_white[i+3]==1) && (v_white[i+4]==1))
                 w_points=i+6;
              break;
           }
        }
     }
     if(b_points!=0 || w_points!=0){
        if(b_points>w_points)
           cout<<"Black wins."<<endl;
        else if (w_points>b_points)
           cout<<"White wins."<<endl;
        else
           cout<<"Tie."<<endl;
        return true;
     }
     return false;
}

int main(){
    map<char,int> values;
    values['2']=0;values['3']=1;values['4']=2;values['5']=3;values['6']=4;
    values['7']=5;values['8']=6;values['9']=7;values['T']=8;values['J']=9;
    values['Q']=10;values['K']=11;values['A']=12;
    map<char,int> suits;
    suits['C']=0;suits['D']=1;suits['H']=2;suits['S']=3;    
    while (true){       
       if (feof(stdin))
          return 0;
       vector<int> v_black(13,0);
       vector<int> s_black(4,0);
       vector<int> v_white(13,0);
       vector<int> s_white(4,0);         
       for(int i=0; i<5; i++){
          string card;
          cin>>card;
          if (card=="")
             return 0;        
          v_black[values[card[0]]]++;
          s_black[suits[card[1]]]++;
       }
       for(int i=0; i<5; i++){
          string card;
          cin>>card;
          v_white[values[card[0]]]++;
          s_white[suits[card[1]]]++;
       }
       if(straight_flush(v_black,s_black,v_white,s_white))
          continue;
       if(four_of_a_kind(v_black,v_white))
          continue;
       if(full_house(v_black,v_white))
          continue;
       if(flush(v_black,s_black,v_white,s_white))
          continue;
       if(straight(v_black,v_white))
          continue;
       if(three_of_a_kind(v_black,v_white))
          continue;
       if(two_pairs(v_black,v_white))
          continue;
       if(one_pair(v_black,v_white))
          continue;
       high_card(v_black,v_white);
    }
}
Post Reply

Return to “Volume 103 (10300-10399)”