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

Ivan Goroun
New poster
Posts: 10
Joined: Sun Dec 20, 2009 12:01 am

Re:

Post by Ivan Goroun » Thu Dec 24, 2009 3:54 am

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

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..
I guess this shows that the judge for the problem doesn't have a test case for whatever error you made. The answer your code gives is wrong, as you yourself stated :)

Ivan Goroun
New poster
Posts: 10
Joined: Sun Dec 20, 2009 12:01 am

Re: 10315 - Poker Hands

Post by Ivan Goroun » Thu Dec 24, 2009 11:31 am

Ok, finally got accepted. I had one bug people could also be struggling with for which there was no test case. No worries - I added it ;) It was the case where in the Two Pairs ranking I was giving too much weight to the last card. So, a very high single card was beating out the pair where the second pair should win :lol:

Use the input below to test - it consists of everything in this thread and MORE! I know that it STILL doesn't test all cases because I fixed another bug with which it was passing the below just fine.

Input:

Code: Select all

TD TC 2D 2H AC TS TH 3S 3D 2S
4S 4D 2D 2H AC 4H 4C 3S 3C 2C
AD AC QD QH KD AH AS KS KC 2C
2D 2C 3D 3C AH 2H 2S 3S 3D KD
2D 2C 3D 3C 4D 2H 2S 3S 3D 6S
5D 2D 2C 3D 3C 2H 2S 3S 3D 5S
4C 4C 8C 7C 5C 3D 3D 8D 7D 6D
3H 5H AH AH 3H 2H 2C AC AD 2D
2H 3D 5S 9C KD 2C 3H 4S 8C AH
2H 4S 4C 2D 4H 2S 8S AS QS 3S
2H 3D 5S 9C KD 2C 3H 4S 8C KH
2H 3D 5S KD 9C 2D 3H 5C 9S KH
AH JH TH QH KH AD QD KD TD JD
AH JH TH QH KH KD 9D JD TD QD
KD 9D JD TD QD AH JH TH QH KH
AH JH TH QH KH AH JH TH QH KD
9H 7H 3H 8H KH KD 9D 3D 7D 2D
KD 9D 3D 7D 2D 9H 7H 3H 8H KH
KD 9D 3D 7D 2D 9H 7H 3H 2H KH
TH TD TS TC KD 9H 9D 9S 9C JD
8H 8S 8D 8C AC AC KD KH KC KS
AH AS AC 2D 2H KH KS 2S 2C KH
TS TC JH 2H TH TD 9D 9H 9C 3C
TD TC KD KC 2H TS TH 2H KH KS
TD TC KD 4C 2H TS TH 2S 4H KS
TD TC 2D 4C 2H TS TH 2S 2S KS
TD TC 2D 4C 2H TS TH 3S 3D KS
TD TC 2D 4C 2H JS JH 2S 2C 4S
TD TS KD JS 2S TH TC KC JD 3C
TD JH TS 3S 5D TH JS TC 3S 4C
TD JH TS 3S 5D TH QS TC 3S 5C
TD TS QH KH AH JH JD 2H 3S 4C
AH 2H 3H 4H 5H 6H 6D 6C 7C 6S
3H 5H AH AH 3H 2H 2C AC AD 2D
3H 3D 6C 3S 3D 2H 2C AC AD 2D
3H 3D 6C 3S 3C 2H 2D 6D 2S 2C
2H 3H 4H 5H 6H AC AS AD AH KH
4H 5H 6H 7H 8H 8C 7C 6C 5C 4C
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 
Output:

Code: Select all

White wins.
White wins.
White wins.
Black wins.
White wins.
Tie.
White wins.
White wins.
White wins.
Black wins.
Black wins.
Tie.
Tie.
Black wins.
White wins.
Black wins.
Black wins.
White wins.
Tie.
Black wins.
White wins.
Black wins.
Black wins.
Tie.
Tie.
White wins.
White wins.
White wins.
White wins.
Black wins.
White wins.
White wins.
White wins.
White wins.
Black wins.
Black wins.
Black wins.
Tie.
White wins.
Tie.
Black wins.
Black wins.
Black wins.
Black wins.
Tie.
White wins.
Black wins.
Tie.
Tie.
Black wins.
Black wins.
Black wins.
Tie.
Tie.
Black wins.
Black wins.
Tie.
Black wins.
Black wins.
Tie.
Last edited by Ivan Goroun on Thu Dec 24, 2009 10:46 pm, edited 1 time in total.

diego_engcomp
New poster
Posts: 9
Joined: Sat Jul 18, 2009 1:18 am

Re: 10315 - Poker Hands

Post by diego_engcomp » Thu Dec 24, 2009 3:03 pm

Ivan, I tested my program with your input and get the same output. I posted my code here before you. I've already tested my program with all inputs on this topic and they match. If someone can help me I appreciate.

Ivan Goroun
New poster
Posts: 10
Joined: Sun Dec 20, 2009 12:01 am

Re: 10315 - Poker Hands

Post by Ivan Goroun » Thu Dec 24, 2009 10:47 pm

diego_engcomp wrote:Ivan, I tested my program with your input and get the same output. I posted my code here before you. I've already tested my program with all inputs on this topic and they match. If someone can help me I appreciate.
I updated my previous post with a couple of extra cases - try it again!

diego_engcomp
New poster
Posts: 9
Joined: Sat Jul 18, 2009 1:18 am

Re: 10315 - Poker Hands

Post by diego_engcomp » Sat Dec 26, 2009 12:49 am

Ivan, they match again. Could you take a look at my code please? It is a few topics above. I'm really stuck. Thanks.

Andrew1981bl
New poster
Posts: 1
Joined: Sat Aug 27, 2011 11:41 am

Re: 10315 - Poker Hands

Post by Andrew1981bl » Sat Aug 27, 2011 4:01 pm

Hi!
I have a question. All ten cards are from the same deck?
2C 2D 2H 2S AS 2C 2D 2S 2H KH //not possible ???

asif.mist
New poster
Posts: 3
Joined: Wed Jan 26, 2011 4:43 pm

Re: 10315 - Poker Hands

Post by asif.mist » Wed Dec 28, 2011 10:45 am

Hi! Andrew,
yes All ten cards are from the same deck.
2C 2D 2H 2S AS 2C 2D 2S 2H KH is not possible..

Sharkest
New poster
Posts: 5
Joined: Thu Apr 26, 2012 1:47 am

Re: 10315 - Poker Hands

Post by Sharkest » Mon Apr 30, 2012 9:07 am

Does the judge really only use 1 deck?
My code is generating the correct output for the test cases except for the ones which have more than one deck in it.Getting WA.
My code:

Code: Select all

#include <iostream>
#include <stdio.h>
#include <queue>
#include <math.h>

using namespace std;

int rankValor(char carta);

int rankNaipe(char carta);

int main()
{
    char cartas[2][5][2],  validInput, entrada;

    unsigned long long pontuacao[2];

    int maoValor[2][13], maoNaipe[2][4], n, temp, fullhouse1, fullhouse2, sflush1, sflush2, flushCheck[2];//lembrar de zerar depois


    do
    {
        pontuacao[0] = 0;
        pontuacao[1] = 0;

        fullhouse1 = 0;
        fullhouse2 = 0;

        for(int i = 0; i < 2; i++)
            for(int j = 0; j  < 13; j++)
                maoValor[i][j] = 0;

        for(int i = 0; i < 2; i++)
            for(int j = 0; j < 4; j++)
                maoNaipe[i][j] = 0;

        validInput = cin.peek();
        if((validInput != EOF) && (validInput != '\n') )
        {
            for(int k = 0; k < 2; k++)
                for(int i = 0; i < 5; i++)
                    for(int j = 0; j < 2; j++)
                    {
                        if(scanf("%c",&entrada) == 1)
                        {
                            if((entrada != ' ') && (entrada != '\n') && (entrada != EOF))
                                cartas[k][i][j] = entrada;
                            else
                                j--;
                        }
                    }

            getchar();

            for(int i = 0; i < 2; i++)
                for(int j = 0; j < 13; j++)
                    for(int k = 0; k < 5; k++)
                        if(rankValor(cartas[i][k][0]) == j)
                            maoValor[i][j]++;


            for(int i = 0; i < 2; i++)
                for(int j = 0; j < 4; j++)
                    for(int k = 0; k < 5; k++)
                        if(rankNaipe(cartas[i][k][1]) == j)
                            maoNaipe[i][j]++;

            for(int i = 0; i < 2; i++)
            {

                temp = 4;
                for(int j = 12; (j >= 0) && (temp >= 0); j--)//soma na pontuação aquela referente a quem tem a carta de maior valor
                {
                    n = maoValor[i][j];
                    while(n > 0)
                    {
                        pontuacao[i] += (j+1)*pow(14, temp);
                        temp --;
                        n--;
                    }
                }

                temp = 0;
                for(int j = 0; (j < 13) && (!temp); j++)//soma na pontuação aquela referente ao valor do menor par
                {
                    n = maoValor[i][j];
                    if(n == 2)
                    {
                        pontuacao[i] += (j+1)*pow(14, 5);
                        temp = 1;
                    }

                }

                temp = 0;
                for(int j = 12; (j >= 0) && (!temp); j--)//soma na pontuação aquela referente ao valor do maior par
                {
                    n = maoValor[i][j];
                    if(n == 2)
                    {
                        pontuacao[i] += (j+1)*pow(14, 6);
                        temp = 1;
                    }

                }

                temp = 0;
                for(int j = 12; (j >= 0) && (!temp); j--)//soma na pontuação aquela referente ao valor do trio
                {
                    n = maoValor[i][j];
                    if(n == 3)
                    {
                        pontuacao[i] += (j+1)*pow(14, 7);
                        temp = 1;
                    }

                }

                temp = 0;
                for(int j = 12; (j >= 4) && (!temp); j--)//soma na pontuação aquela referente ao valor do straght
                {
                    n = maoValor[i][j];
                    if( (n == 1) && (maoValor[i][j-1] == 1) && (maoValor[i][j-2] == 1) && (maoValor[i][j-3] == 1) && (maoValor[i][j-4] == 1) )
                    {
                        pontuacao[i] += (j+1)*pow(14, 8);
                        temp = 1;
                    }

                }

                temp = 0;
                flushCheck[i] = 0;
                for(int j = 3; (j >= 0) && (!temp); j--)//soma na pontuação aquela referente ao valor do flush
                {
                    n = maoNaipe[i][j];
                    if(n == 5)
                    {
                        pontuacao[i] += pow(14, 9);
                        temp = 1;
                        flushCheck[i] = 1;
                    }

                }


                fullhouse1 = 0;
                fullhouse2 = 0;

                temp = 0;
                for(int j = 12; (j >= 0) && (temp != -1); j--)//soma na pontuação aquela referente ao valor do full house
                {
                    n = maoValor[i][j];
                    if(n == 3)
                    {
                        fullhouse1 = 1;
                        temp = j;
                    }
                    else if(n == 2)
                        fullhouse2 = 1;

                    if(fullhouse1 && fullhouse2)
                    {
                        pontuacao[i] += (temp + 1)*pow(14,10);
                        temp = -1;
                    }
                }


                temp = 0;
                for(int j = 12; (j >= 0) && (!temp); j--)//soma na pontuação aquela referente ao valor do four of a kind
                {
                    n = maoValor[i][j];
                    if(n == 4)
                    {
                        pontuacao[i] += (j+1)*pow(14,11);
                        temp = 1;
                    }

                }

                sflush1 = 0;
                sflush2 = 0;
                temp = 0;
                for(int j = 12; (j >= 4) && (!temp); j--)//condição referente ao straght
                {
                    n = maoValor[i][j];
                    if( (n == 1) && (maoValor[i][j-1] == 1) && (maoValor[i][j-2] == 1) && (maoValor[i][j-3] == 1) && (maoValor[i][j-4] == 1) )
                        sflush1 = 1;
                }
                for(int j = 3; (j >= 0) ; j--)//condição referente ao flush
                {
                    n = maoNaipe[i][j];
                    if(n == 5)
                        sflush2 = 1;
                }

                if( (sflush1 != 0) && (sflush2 != 0) )//soma da pontuação referente ao straight flush
                {
                    pontuacao[i] += pow(14,12);
                    temp = 1;
                }

            }


            if( (pontuacao[0] < pow(14,10) ) && (pontuacao[1] < pow(14,10) ) )
                if(flushCheck[0] && flushCheck[1])
                {
                    for(int i = 0; i < 2; i++)
                    {


                        temp = 4;
                        pontuacao[i] = 0;
                        for(int j = 12; (j >= 0) && (temp >= 0); j--)//soma na pontuação aquela referente a quem tem a carta de maior valor
                        {
                            n = maoValor[i][j];
                            while(n > 0)
                            {
                                pontuacao[i] += (j+1)*pow(14, temp);
                                temp --;
                                n--;
                            }
                        }
                    }

                }



            if(pontuacao[0] > pontuacao[1])
                printf("Black wins.");
            else if(pontuacao[0] < pontuacao[1])
                printf("White wins.");
            else
                printf("Tie.");

            if(  (cin.peek() != '\n') && (cin.peek() != EOF) )
                printf("\n");
        }
    }while(validInput != EOF);
   
    printf("\n");

}

int rankValor(char carta)
{
    char cartas[13] = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};

    for(int i = 0; i < 13; i++)
        if(carta == cartas[i])
            return i;

    return 0;
}

int rankNaipe(char carta)
{
    char naipes[4] = {'C', 'D', 'H', 'S'};

    for(int i = 0; i < 4; i++)
        if(carta == naipes[i])
            return i;
    return 0;
}
Last edited by Sharkest on Tue May 01, 2012 3:11 am, edited 1 time in total.

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

Re: 10315 - Poker Hands

Post by brianfry713 » Mon Apr 30, 2012 10:04 pm

You're not printing a newline at the end.
Check input and AC output for thousands of problems on uDebug!

Sharkest
New poster
Posts: 5
Joined: Thu Apr 26, 2012 1:47 am

Re: 10315 - Poker Hands

Post by Sharkest » Tue May 01, 2012 12:35 am

Still getting WA.

added a printf("\n"); after while(validInput != EOF);

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

Re: 10315 - Poker Hands

Post by brianfry713 » Wed May 02, 2012 1:19 am

Don't use double. Counting on the pow() function to convert correctly to an integer could cause precision errors.
Check input and AC output for thousands of problems on uDebug!

Neil
New poster
Posts: 8
Joined: Fri Jun 22, 2012 7:04 am

Re: 10315 - Poker Hands

Post by Neil » Sat Jun 23, 2012 10:32 pm

I've been testing my solution with the UVa toolkit, and I found a couple cases that I don't understand.
1) 2H 3H TH 5H KH 3D JD KD 2D 2D
Output is "Black wins." Shouldn't it be white? Both are flushes, the highest card is a king, then white has a higher second-highest card (jack vs ten).
2) 8C 9C TC 2C 7C 8S 9S TS 7S 7S
Output is "Black wins." But they are tied until the last card where white has a 7 and black has 2, so white wins.
What am I missing?

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

Re: 10315 - Poker Hands

Post by brianfry713 » Mon Jun 25, 2012 11:52 pm

Both of your inputs have the same card repeated twice. A standard deck will only have one of each card.
Check input and AC output for thousands of problems on uDebug!

Bob
New poster
Posts: 3
Joined: Fri Sep 14, 2012 3:15 pm

Re: 10315 - Poker Hands

Post by Bob » Sat Oct 20, 2012 5:07 pm

Thanks to the posted test code I managed to fix a ittle buy but elsewise I'm stuck.
Maybe a clearer eye can find what I keep missing?
Sorry for the indentation, I used the indent program and it kind of messed some things up a bit.

Do mind that for now it's written to write to a file, easily edited though.

Code: Select all

/*
 * PokerHands.cpp
 *
 *  Created on: Oct 14, 2012
 *      Author: ruben
 */

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <fstream>

using std::vector;

const int handSize = 5;

enum Suit {
    Diamonds,
    Hearts,
    Clubs,
    Spades
};

enum Value {
    two = 2,
    three,
    four,
    five,
    six,
    seven,
    eight,
    nine,
    ten,
    jack,
    queen,
    king,
    ace
};

enum Combo {
    HighCard,
    Pair,
    TwoPairs,
    ThreeofaKind,
    Straight,
    Flush,
    FullHouse,
    FourofaKind,
    StraightFlush
};

enum Outcome {
    notfound,
    blackwin,
    whitewin,
    tie
};


class Card {
  public:

    Card();
    Card(Value value, Suit suit);
    virtual ~ Card();


    // Data members
    Value m_value;
    Suit m_suit;

};

typedef vector < Card > Hand;

Card::Card():m_value(two), m_suit(Clubs)
{

}

Card::Card(Value value, Suit suit):
m_value(value), m_suit(suit)
{
}

Card::~Card()
{
}

std::ostream & operator<<(std::ostream & out, Card & card)
{
    out << "Value : " << card.m_value << "\nSuit : " << card.
        m_suit << std::endl;
    return out;
}

bool operator==(const Card & first, const Card & second)
{
    return first.m_value == second.m_value;
}

bool operator>(const Card & first, const Card & second)
{
    return first.m_value > second.m_value;
}

bool operator<(const Card & first, const Card & second)
{
    return first.m_value < second.m_value;
}


void sortCards(Hand & hand)
{
    // Currently implemented using bubble sort
    bool swapped = true;

    while (swapped) {
        int swapLength = handSize - 1;
        swapped = false;

        for (int i = 0; i < swapLength; i++) {
            if (hand[i].m_value <= hand[i + 1].m_value)
                continue;
            else {
                Card reserve = hand[i];
                hand[i] = hand[i + 1];
                hand[i + 1] = reserve;
                swapped = true;
            }
        }
        swapLength--;
    }
}


Value determineValue(const char pres)
{
    switch (pres) {
    case '2':
        return two;
    case '3':
        return three;
    case '4':
        return four;
    case '5':
        return five;
    case '6':
        return six;
    case '7':
        return seven;
    case '8':
        return eight;
    case '9':
        return nine;
    case 'T':
        return ten;
    case 'J':
        return jack;
    case 'Q':
        return queen;
    case 'K':
        return king;
    case 'A':
        return ace;
    }

    return ace;
}

Suit determineSuit(const char pres)
{
    switch (pres) {
    case 'C':
        return Clubs;
    case 'D':
        return Diamonds;
    case 'H':
        return Hearts;
    case 'S':
        return Spades;
    }

    return Spades;
}

inline bool isFourofaKind(const Hand & hand)
{
    return ((hand[0] == hand[3]) || (hand[1] == hand[4]));
}

inline bool isFullHouse(const Hand & hand)
{
    return (((hand[0] == hand[1]) && (hand[2] == hand[4])) ||
            ((hand[0] == hand[2]) && (hand[3] == hand[4])));
}

inline bool isFlush(const Hand & hand)
{
    Suit compareSuit = hand[0].m_suit;
    for (int i = 1; i < 5; i++)
        if (hand[i].m_suit != compareSuit)
            return false;
    return true;
}

inline bool isStraight(const Hand & hand)
{
    for (int i = 0; i < 4; i++)
        if (hand[i].m_value != hand[i + 1].m_value - 1)
            return false;
    return true;
}

inline bool isThreeofaKind(const Hand & hand)
{
    return ((hand[0] == hand[2]) ||
            (hand[1] == hand[3]) || (hand[2] == hand[4]));
}

inline bool isTwoPairs(const Hand & hand)
{
    return (((hand[0] == hand[1]) && (hand[2] == hand[3])) ||
            ((hand[0] == hand[1]) && (hand[3] == hand[4])) ||
            ((hand[1] == hand[2]) && (hand[3] == hand[4])));
}

inline bool isPair(const Hand & hand)
{
    for (int i = 0; i < 4; i++)
        if (hand[i] == hand[i + 1])
            return true;
    return false;
}

Card getHighCard(const Hand & hand)
{
    return hand[4];
}

Combo determineCombo(const Hand & hand)
{
    if (isStraight(hand) && isFlush(hand))
        return StraightFlush;
    else if (isFourofaKind(hand))
        return FourofaKind;
    else if (isFullHouse(hand))
        return FullHouse;
    else if (isFlush(hand))
        return Flush;
    else if (isStraight(hand))
        return Straight;
    else if (isThreeofaKind(hand))
        return ThreeofaKind;
    else if (isTwoPairs(hand))
        return TwoPairs;
    else if (isPair(hand))
        return Pair;
    else
        return HighCard;
}

Outcome determineOutcome(const Hand & black, const Hand & white)
{
    Combo blackCombo = determineCombo(black);
    Combo whiteCombo = determineCombo(white);

    if (blackCombo == whiteCombo) {
        switch (blackCombo) {
        case StraightFlush:
            {
                if (getHighCard(black) > getHighCard(white))
                    return blackwin;
                else if (getHighCard(black) < getHighCard(white))
                    return whitewin;
                else
                    return tie;
                break;
            }
        case FourofaKind:
            {
                if (black[2] > white[2])
                    return blackwin;
                else if (black[2] < white[2])
                    return whitewin;
                else
                    return tie;
                break;
            }
        case FullHouse:
            {
                if (black[2] > white[2])
                    return blackwin;
                else if (black[2] < white[2])
                    return whitewin;
                else
                    return tie;
                break;
            }
        case Flush:
            {
                for (int i = 4; i >= 0; i--) {
                    if (black[i] == white[i])
                        continue;
                    else if (black[i] > white[i])
                        return blackwin;
                    else
                        return whitewin;
                }
                return tie;
                break;
            }
        case Straight:
            {
                if (getHighCard(black) > getHighCard(white))
                    return blackwin;
                else if (getHighCard(black) < getHighCard(white))
                    return whitewin;
                else
                    return tie;
                break;
            }
        case ThreeofaKind:
            {
                if (black[2] > white[2])
                    return blackwin;
                else if (black[2] < white[2])
                    return whitewin;
                else
                    return tie;
                break;
            }
        case TwoPairs:
            {
                int blackHigh, whiteHigh, blackLow, whiteLow;
                int blackRemaining, whiteRemaining;
                if ((black[0] == black[1]) && (black[2] == black[3])) {
                    if (black[1] > black[3]) {
                        blackHigh = 1;
                        blackLow = 3;
                    } else {
                        blackHigh = 3;
                        blackLow = 1;
                    }
                    blackRemaining = 4;
                } else if ((black[0] == black[1])
                           && (black[3] == black[4])) {
                    if (black[1] > black[4]) {
                        blackHigh = 1;
                        blackLow = 4;

                    } else {
                        blackHigh = 4;
                        blackLow = 1;
                    }
                    blackRemaining = 2;
                } else {
                    if (black[2] > black[4]) {
                        blackHigh = 2;
                        blackLow = 4;

                    } else {
                        blackHigh = 4;
                        blackLow = 2;
                    }
                    blackRemaining = 0;
                }
                if ((white[0] == white[1]) && (white[2] == white[3])) {
                    if (white[1] > white[3]) {
                        whiteHigh = 1;
                        whiteLow = 3;

                    } else {
                        whiteHigh = 3;
                        whiteLow = 1;
                    }
                    whiteRemaining = 4;
                } else if ((white[0] == white[1])
                           && (white[3] == white[4])) {
                    if (white[1] > white[4]) {
                        whiteHigh = 1;
                        whiteLow = 4;

                    } else {
                        whiteHigh = 4;
                        whiteLow = 1;
                    }
                    whiteRemaining = 2;
                } else {
                    if (white[2] > white[4]) {
                        whiteHigh = 2;
                        whiteLow = 4;

                    } else {
                        whiteHigh = 4;
                        whiteLow = 2;
                    }
                    whiteRemaining = 0;
                }
                if (black[blackHigh] == white[whiteHigh]) {
                    if (black[blackLow] == white[whiteLow]) {
                        if (black[blackRemaining] == white[whiteRemaining])
                            return tie;
                        else if (black[blackRemaining] >
                                 white[whiteRemaining])
                            return blackwin;
                        else
                            return whitewin;
                    } else if (black[blackLow] > white[whiteLow])
                        return blackwin;
                    else
                        return whitewin;
                } else if (black[blackHigh] > white[whiteHigh])
                    return blackwin;
                else
                    return whitewin;
                break;
            }
        case Pair:
            {
                int blackPair[2];
                int whitePair[2];

                for (int i = 0; i < 4; i++) {
                    if (black[i] == black[i + 1]) {
                        blackPair[0] = i;
                        blackPair[1] = i + 1;
                    }
                    if (white[i] == white[i + 1]) {
                        whitePair[0] = i;
                        whitePair[1] = i + 1;
                    }
                }

                Card blackHigh = black[blackPair[0]];
                Card whiteHigh = white[whitePair[0]];

                if (blackHigh == whiteHigh) {
                    vector < int >blackRemaining;
                    vector < int >whiteRemaining;

                    for (int i = 0; i < 5; i++) {
                        if (!((i == blackPair[0]) || (i == blackPair[1])))
                            blackRemaining.push_back(i);
                        if (!((i == whitePair[0]) || (i == whitePair[1])))
                            whiteRemaining.push_back(i);
                    }

                    for (int i = 2; i >= 0; i--) {
                        if (black[blackRemaining[i]] == white[whiteRemaining[i]])
                            continue;
                        else if (black[blackRemaining[i]] > white[whiteRemaining[i]])
                            return blackwin;
                        else
                            return whitewin;
                    }
                    return tie;
                } else if (blackHigh > whiteHigh)
                    return blackwin;
                else
                    return whitewin;

                break;
            }
        case HighCard:
            {
                for (int i = 4; i >= 0; i--) {
                    if (black[i] == white[i])
                        continue;
                    else if (black[i] > white[i])
                        return blackwin;
                    else
                        return whitewin;
                }
                return tie;
                break;
            }
        }
    } else if (blackCombo > whiteCombo)
        return blackwin;
    else
        return whitewin;

    return notfound;
}

int main()
{
    char cardBuffer[3];
    Hand black(5);
    Hand white(5);

    std::ifstream filein;
    std::ofstream fileout;

    filein.open("PokerHandsInput.txt");
    fileout.open("PokerHandsTestOutput.txt");

    while (std::cin) {

        // Read in cards
        for (int i = 0; i < 10; i++) {
            std::cin >> cardBuffer;

            Value cardValue = determineValue(cardBuffer[0]);
            Suit cardSuit = determineSuit(cardBuffer[1]);

            if (i < 5)
                black[i] = Card(cardValue, cardSuit);
            else
                white[i - 5] = Card(cardValue, cardSuit);
        }

        // Sort hands
        sortCards(black);
        sortCards(white);

        Outcome outcome = determineOutcome(black, white);

        if (outcome == blackwin)
            fileout << "Black wins." << std::endl;
        else if (outcome == whitewin)
            fileout << "White wins." << std::endl;
        else
            fileout << "Tie." << std::endl;
    }

    fileout.close();
    filein.close();
}


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

Re: 10315 - Poker Hands

Post by brianfry713 » Tue Oct 23, 2012 10:36 pm

Post the code you'd actually submit. Don't read and write to a file. Your code won't properly terminate if there is additional whitespace (such as a newline) at the end of the input.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 103 (10300-10399)”