Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <chrono>
- #include <algorithm>
- enum TYPE{
- SQUARE,
- CIRCLE,
- TRIANGLE,
- };
- struct _card
- {
- TYPE type;
- int amount;
- _card( int a, TYPE t) { type = t; amount = a;};
- bool operator <(const _card & lhs) const {
- return (type < lhs.type || amount < lhs.amount) ;
- }
- bool operator <=(const _card & lhs) const {
- return (type <= lhs.type || amount <= lhs.amount) ;
- }
- bool operator ==(const _card & lhs) const {
- return (type == lhs.type || amount == lhs.amount) ;
- }
- };
- inline bool canMatch(_card a, _card b)
- {
- return((a.type == b.type && a.amount != b.amount) || (a.type == b.type && a.amount == b.amount) );
- }
- inline bool canMatch3(_card a, _card b, _card c)
- {
- return((a.type == b.type && a.type == c.type)
- && ((a.amount != b.amount && b.amount != c.amount && a.amount != c.amount) ||
- ( a.amount == b.amount && a.amount == c.amount )));
- }
- inline bool cannotMatch(_card a, _card b)
- {
- return(a.type != b.type && a.amount != b.amount);
- }
- inline bool cannotMatch3(_card a, _card b, _card c)
- {
- return((a.type != b.type && b.type != c.type && a.type != c.type) &&
- (a.amount != b.amount && b.amount != c.amount && a.amount != c.amount));
- }
- inline _card GetgameCard(std::string card) {
- if(card.compare("um quadrado") == 0) return _card(1, SQUARE);
- else if(card.compare("dois quadrados") == 0) return _card(2, SQUARE);
- else if(card.compare("tres quadrados") == 0) return _card(3, SQUARE);
- else if(card.compare("um triangulo") == 0) return _card(1, TRIANGLE);
- else if(card.compare("dois triangulos") == 0) return _card(2, TRIANGLE);
- else if(card.compare("tres triangulos") == 0) return _card(3, TRIANGLE);
- else if(card.compare("um circulo") == 0) return _card(1, CIRCLE);
- else if(card.compare("dois circulos") == 0) return _card(2, CIRCLE);
- else if(card.compare("tres circulos") == 0) return _card(3, CIRCLE);
- return _card(0,(TYPE)0);
- }
- class SetGameManager {
- public:
- SetGameManager() {};
- public:
- std::vector<_card> gameCards;
- public:
- void AddCard(_card gameCard);
- void RemoveCards() { gameCards.clear(); };
- unsigned int CountSet();
- };
- inline void SetGameManager::AddCard(_card gameCard)
- {
- gameCards.push_back(gameCard);
- }
- inline unsigned int SetGameManager::CountSet()
- {
- unsigned int SetMatches = 0;
- unsigned int i1 = 0;
- std::sort(this->gameCards.begin(), this->gameCards.end());
- while(gameCards.size() > 2)
- {
- auto continue_outer_loop(true);
- for(unsigned int i2=1; continue_outer_loop && (i2<gameCards.size()); i2++)
- {
- if(i2 == 2) continue;
- auto const p_match_3_func
- (
- cannotMatch(gameCards.at(i1), gameCards.at(i2))
- ?
- &cannotMatch3
- :
- &canMatch3
- );
- for(unsigned int i3=2; i3<gameCards.size(); i3++)
- {
- if((*p_match_3_func)(gameCards.at(i1), gameCards.at(i2), gameCards.at(i3)))
- {
- SetMatches++;
- gameCards.erase(gameCards.begin()+i3);
- gameCards.erase(gameCards.begin()+i2);
- continue_outer_loop = false;
- break;
- }
- }
- }
- gameCards.erase(gameCards.begin());
- }
- return SetMatches;
- }
- int main()
- {
- SetGameManager Manager;
- int cardAmount;
- // std::cout << "Enter the amount of cards:" << std::endl;
- std::cin >> cardAmount;
- std::string card;
- while(cardAmount != 0)
- {
- std::cin.ignore();
- for(int i=0; i<cardAmount; i++)
- {
- std::getline(std::cin, card);
- // std::cout << GetgameCard(card) << std::endl;
- Manager.AddCard(GetgameCard(card));
- }
- //std::cout << "cannotMatch3: " << cannotMatch3(Manager.gameCards.at(0),Manager.gameCards.at(1), Manager.gameCards.at(2)) << std::endl;
- std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
- std::cout << Manager.CountSet() << std::endl;
- std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
- std::cout << "Miliseconds: " << duration << std::endl;
- Manager.RemoveCards();
- std::cin >> cardAmount;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement