• Basic set solution by synx
  • 5 years ago
  • Download | Raw
// progquiz.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <assert.h>

typedef enum
{
    HIGH_CARD, ONE_PAIR, TWO_PAIR, TRIPS, STRAIGHT, FLUSH, FULL_HOUSE, FOUR_OF_A_KIND, STRAIGHT_FLUSH
} HAND_TYPES;


class card
{
public:
    unsigned int rank;
    char suit;
    card() {}
    card(unsigned int r, char s) : rank(r), suit(s) { }
    card(std::string s);
    void print();
    bool operator<(const card&);
};

bool card::operator<(const card &other)
{
	if (this->rank == other.rank)
	{
		return this->suit < other.suit;
	}

    return this->rank < other.rank;
}

card::card(std::string s)
{
    unsigned int r;
    switch (s[0])
    {
    case 'T':
        r = 10;
        break;
    case 'J':
        r = 11;
        break;
    case 'Q':
        r = 12;
        break;
    case 'K':
        r = 13;
        break;
	case 'A':
		r = 14;
		break;
    default:
        r = s[0] - '0';
        break;
    }

	this->rank = r;
    this->suit = s[1];
}

void card::print()
{
    std::cout << this->rank << this->suit;
}

class hand
{
private:
    std::vector<card> cards;
    unsigned long long rankcheck();
    HAND_TYPES getHandType();
public:
	unsigned long long rank;
	bool isStraightFlush();
    bool isFourOfKind();
    bool isFullHouse();
    bool isFlush();
    bool isStraight();
    bool isTrips();
    bool isTwoPair();
    bool isPair();

//public:
    hand(std::vector<card> c) : cards(c), rank(0) { }
	hand(std::string c);
    void addPlayerHand(std::vector<card> c);
    void sortCards();
    void sortCards(std::vector<card>);
    void addHand(card c);
    void calcRank();
    void print();
};

hand::hand(std::string c)
{
	std::string c1 = c.substr(0, 2);
	std::string c2 = c.substr(3, 2); 
	std::string c3 =  c.substr(6, 2);
	std::string c4 =  c.substr(9, 2);
	std::string c5 = c.substr(12, 2);

	card card1(c1);
	card card2(c2);
	card card3(c3);
	card card4(c4);
	card card5(c5);
	
	this->cards.push_back(card1);
	this->cards.push_back(card2);
	this->cards.push_back(card3);
	this->cards.push_back(card4);
	this->cards.push_back(card5);

	this->sortCards();
}


/*
int _tmain(int argc, _TCHAR* argv[])
{
	hand h1("6s 6c Ts Jc Kd");

	assert(h1.isPair());
	assert(!h1.isFlush());
	assert(!h1.isStraight());
	
	hand h2("6s 6c Ts Jc Jd");

	assert(h2.isTwoPair());
	assert(!h2.isFourOfKind());
	assert(!h2.isStraight());

	hand h3("6s 7d 8s 9h Tc");

	assert(h3.isStraight());
	assert(!h3.isFlush());
	assert(!h3.isPair());


} */


card readCard(std::ifstream &a)
{
    std::string s;
    a >> s;
    card c(s);
    return c;
}

std::vector<card> getBoard(std::ifstream &cin)
{
    std::vector<card> ret;
    for(int i=0; i<5; i++)
        ret.push_back(readCard(cin));
    return ret;
}

void printAllHands(std::vector<hand> hands)
{
    for(int i=0; i<hands.size(); i++)
    {
        hands[i].print();
        std::cout << std::endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{

	std::ifstream cin("in.txt");
    int numCases;
    cin >> numCases;
    while(numCases--)
    {
        std::vector<hand> hands;
        int numHands;
        cin >> numHands;
        std::vector<card> board = getBoard(cin);
        while(numHands--)
        {
            hand playerHand(board);
            playerHand.addHand(readCard(cin));
            playerHand.addHand(readCard(cin));
            hands.push_back(playerHand);
        }

        for(int i=0; i<hands.size(); i++)
        {
            hands[i].sortCards();
            hands[i].calcRank();
        }

		std::vector<int> winningIndex;
		unsigned long long winningValue = 0;

		for(int i=0; i<hands.size(); i++)
		{
			if (hands[i].rank > winningValue)
			{
				winningValue = hands[i].rank;
			//	winningIndex.push_back(i);
			}
		}

		for(int i=0; i<hands.size(); i++)
		{
			if (hands[i].rank == winningValue)
			{
				//winningValue = hands[i].rank;
				winningIndex.push_back(i);
			}
		}

		for(int i=0; i<winningIndex.size(); i++)
		{
			std::cout << winningIndex[i] << " ";
		}
		std::cout << std::endl;
    }
	
	system("PAUSE");

} 
                                                                                                                                                                  
void hand::sortCards(std::vector<card> cards)
{
    card temp;
    unsigned int indexMin;
    for(int i=0; i<cards.size()-1; i++)
    {
        indexMin = i;
        for(int j=i+1; j<cards.size(); j++)
        {
            if (this->cards[j].rank < this->cards[indexMin].rank)
            {
                indexMin = j;
            }
        }

		if (indexMin != i)
        {
            std::swap(this->cards[i], this->cards[indexMin]);
        }
    }
}

void hand::sortCards() { sortCards(this->cards); }

void hand::addPlayerHand(std::vector<card> c)
{
    this->cards.insert(this->cards.end(), c.begin(), c.end());
}

void hand::addHand(card c)
{
    this->cards.push_back(c);
}

void hand::print()
{
    for(int i=0; i<cards.size(); i++)
        cards[i].print();
    std::cout << std::endl;
}

bool hand::isStraightFlush()
{
    return this->isFlush() && this->isStraight();
}

bool hand::isFourOfKind()
{                                                                                                                                       
    return (this->cards[0].rank == this->cards[1].rank &&
			this->cards[0].rank == this->cards[2].rank &&
			this->cards[0].rank == this->cards[3].rank) || 
			(this->cards[1].rank == this->cards[2].rank &&
			this->cards[1].rank == this->cards[3].rank  &&
			this->cards[1].rank == this->cards[4].rank);
}

bool hand::isFullHouse() 
{
	
	return (this->cards[0].rank == this->cards[1].rank &&
            this->cards[1].rank == this->cards[2].rank &&
            this->cards[3].rank == this->cards[4].rank) ||
			(this->cards[0].rank == this->cards[1].rank &&
			this->cards[2].rank == this->cards[3].rank &&
			this->cards[2].rank == this->cards[4].rank);
}

bool hand::isFlush() 
{ 
	return (this->cards[0].suit == this->cards[1].suit &&
			this->cards[0].suit == this->cards[2].suit &&
			this->cards[0].suit == this->cards[3].suit &&
			this->cards[0].suit == this->cards[4].suit);
}

bool hand::isStraight()
{ 
	return this->cards[0].rank == this->cards[1].rank - 1 &&
		this->cards[1].rank == this->cards[2].rank - 1 &&
		this->cards[2].rank == this->cards[3].rank - 1 &&
		this->cards[3].rank == this->cards[4].rank - 1;
}

bool hand::isTrips() 
{ 
	return (this->cards[0].rank == this->cards[1].rank &&
		this->cards[0].rank == this->cards[2].rank) ||
		(this->cards[1].rank == this->cards[2].rank &&
		this->cards[1].rank == this->cards[3].rank) ||
		(this->cards[2].rank == this->cards[3].rank &&
		this->cards[2].rank == this->cards[4].rank);
}

bool hand::isTwoPair() 
{ 
	if(this->cards[0].rank == this->cards[1].rank)
	{
		if (this->cards[2].rank == this->cards[3].rank) return true;
		if (this->cards[3].rank == this->cards[4].rank) return true;
	}

	if(this->cards[1].rank == this->cards[2].rank)
	{
		if (this->cards[3].rank == this->cards[4].rank) return true;
	}

	return false;
}

bool hand::isPair() 
{
	return this->cards[0].rank == this->cards[1].rank ||
		this->cards[1].rank == this->cards[2].rank ||
		this->cards[2].rank == this->cards[3].rank ||
		this->cards[3].rank == this->cards[4].rank;
}

HAND_TYPES hand::getHandType()
{                                                                                    
    if (this->isStraightFlush()) return STRAIGHT_FLUSH;
    if (this->isFourOfKind()) return FOUR_OF_A_KIND;
    if (this->isFullHouse()) return FULL_HOUSE;
    if (this->isFlush()) return FLUSH;
    if (this->isStraight()) return STRAIGHT;
    if (this->isTrips()) return TRIPS;
    if (this->isTwoPair()) return TWO_PAIR;
    if (this->isPair()) return ONE_PAIR;
    return HIGH_CARD;
}


unsigned long long hand::rankcheck()
{
    unsigned long long baseValues[] =
    {
      //  1, 23294461, 81922261, 113355661, 119817281, 125997301, 130044945, 133518129, 163518129
		1, 50000000, 500000000, 5000000000, 50000000000, 500000000000, 5000000000000, 50000000000000, 50000000000000
    };

	HAND_TYPES ht = this->getHandType();
	unsigned long long baseVal = baseValues[ht];

    for(int i=0; i<5; i++)
	{
		//baseVal += this->cards[i].rank;
		baseVal += (this->cards[i].rank * this->cards[i].rank);
	}

	return baseVal;

}

void hand::calcRank()
{
	std::vector<card> temp;
    do {

        unsigned long long currentHandRank = this->rankcheck();
		//this->print();
        if (currentHandRank > this->rank) {
			temp.clear();
            this->rank = currentHandRank;
			temp.push_back(this->cards[0]);
			temp.push_back(this->cards[1]);
			temp.push_back(this->cards[2]);
			temp.push_back(this->cards[3]);
			temp.push_back(this->cards[4]);
        }
    } while(std::next_permutation(this->cards.begin(), this->cards.end()));

	/*std::cout << "Selecting best hand from: ";
	this->cards[0].print(); 
	std::cout << " ";
	this->cards[1].print();
	std::cout << " ";
	this->cards[2].print();
	std::cout << " ";
	this->cards[3].print();
	std::cout << " ";
	this->cards[4].print();
	std::cout << " ";
	this->cards[5].print();
	std::cout << " ";
	this->cards[6].print();
	std::cout << std::endl;

	std::cout << "Best: ";
	temp[0].print(); 
	std::cout << " ";
	temp[1].print(); 
	std::cout << " ";
	temp[2].print(); 
	std::cout << " ";
	temp[3].print(); 
	std::cout << " ";
	temp[4].print(); 
	std::cout << std::endl;*/
}



Scoreboard

lzm bas adv bon 200
sirpengi bas adv bon 200
sixthgear bas adv bon 190
mserrano bas adv bon 190
robbinsr bas adv bon 190
synx bas adv bon 50

What do?

  1. Write your program according to the problem description
  2. Download the basic input set. The timer will start.
  3. Feed the input file to your program and save the output.
  4. Upload the output file along with your source code. If it is correct, you will receive points. If not you may try again until the timer expires.
  5. If the timer expires. You may try again, but a new input file will be generated.
  6. Repeat fot each set.

Note: The input sets use unix line endings (\n) and the verifier expects them as well.


Come Chat!

Join channel ##proggit on freenode.

Bugs

Having issues with the the site? Let us know.