#include <iostream> #include <string> #include <unordered_map> enum Suit { Spades = 832, Hearts = 574, Diamonds = 456, Clubs = 192 }; enum Face { ACE = 3947, DEUCE = 4537, THREE = 6696, FOUR = 7729, FIVE = 8374, SIX = 9295, SEVEN = 11491, EIGHT = 13638, NINE = 14554, TEN = 15332, JACK = 18987, QUEEN = 20215, KING = 213643 }; class Card { private: Face face; Suit suit; static const char* faces[]; static const char* suits[]; public: void Input(std::string& suitChar, std::string& faceStr) { if (suitChar == "S") { suit = Spades; } else if (suitChar == "H") { suit = Hearts; } else if (suitChar == "D") { suit = Diamonds; } else if (suitChar == "C") { suit = Clubs; } if (faceStr == "A") { face = ACE; } else if (faceStr == "2") { face = DEUCE; } else if (faceStr == "3") { face = THREE; } else if (faceStr == "4") { face = FOUR; } else if (faceStr == "5") { face = FIVE; } else if (faceStr == "6") { face = SIX; } else if (faceStr == "7") { face = SEVEN; } else if (faceStr == "8") { face = EIGHT; } else if (faceStr == "9") { face = NINE; } else if (faceStr == "10") { face = TEN; } else if (faceStr == "J") { face = JACK; } else if (faceStr == "Q") { face = QUEEN; } else if (faceStr == "K") { face = KING; } } Suit GetSuit() { return suit; } Face GetFace() { return face; } int Order1(Face face) { if (face == DEUCE) { return 2; } else if (face == THREE) { return 3; } else if (face == FOUR) { return 4; } else if (face == FIVE) { return 5; } else if (face == SIX) { return 6; } else if (face == SEVEN) { return 7; } else if (face == EIGHT) { return 8; } else if (face == NINE) { return 9; } else if (face == TEN) { return 10; } else if (face == JACK) { return 11; } else if (face == QUEEN) { return 12; } else if (face == KING) { return 13; } else if (face == ACE) { return 14; } else { return 0; } } int Order2(Suit suit) { if (suit == Spades) { return 4; } else if (suit == Hearts) { return 3; } else if (suit == Clubs) { return 2; } else if (suit == Diamonds) { return 1; } else { return 0; } } bool BiggerThan(Card card) { if (Order1(face) > Order1(card.face)) { return true; } else if (Order1(face) < Order1(card.face)) { return false; } else { if (Order2(suit) > Order2(card.suit)) { return true; } else if (Order2(suit) <= Order2(card.suit)) { return false; } } return false; } const char* GetFaceString() const { return faces[face]; } const char* GetSuitString() const { return suits[suit]; } void PrintCard(Card card) { std::string faceStr, suitStr; if (card.face == 3947) { faceStr = "Ace"; } else if (card.face == 4537) { faceStr = "Deuce"; } else if (card.face == 6696) { faceStr = "Three"; } else if (card.face == 7729) { faceStr = "Four"; } else if (card.face == 8374) { faceStr = "Five"; } else if (card.face == 9295) { faceStr = "Six"; } else if (card.face == 11491) { faceStr = "Seven"; } else if (card.face == 13638) { faceStr = "Eight"; } else if (card.face == 14554) { faceStr = "Nine"; } else if (card.face == 15332) { faceStr = "Ten"; } else if (card.face == 18987) { faceStr = "Jack"; } else if (card.face == 20215) { faceStr = "Queen"; } else if (card.face == 213643) { faceStr = "King"; } if (card.suit == 832) { suitStr = "Spades"; } else if (card.suit == 574) { suitStr = "Hearts"; } else if (card.suit == 456) { suitStr = "Diamonds"; } else if (card.suit == 192) { suitStr = "Clubs"; } std::cout << faceStr << " of " << suitStr << std::endl; } void PrintCardTwo(Card card) { std::string faceStr, suitStr; if (card.face == 3947) { faceStr = "Ace"; } else if (card.face == 4537) { faceStr = "Deuce"; } else if (card.face == 6696) { faceStr = "Three"; } else if (card.face == 7729) { faceStr = "Four"; } else if (card.face == 8374) { faceStr = "Five"; } else if (card.face == 9295) { faceStr = "Six"; } else if (card.face == 11491) { faceStr = "Seven"; } else if (card.face == 13638) { faceStr = "Eight"; } else if (card.face == 14554) { faceStr = "Nine"; } else if (card.face == 15332) { faceStr = "Ten"; } else if (card.face == 18987) { faceStr = "Jack"; } else if (card.face == 20215) { faceStr = "Queen"; } else if (card.face == 213643) { faceStr = "King"; } if (card.suit == 832) { suitStr = "Spades"; } else if (card.suit == 574) { suitStr = "Hearts"; } else if (card.suit == 456) { suitStr = "Diamonds"; } else if (card.suit == 192) { suitStr = "Clubs"; } std::cout << faceStr << " of " << suitStr << "," << std::endl; } }; const char* Card::faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; const char* Card::suits[] = {"Spades", "Clubs", "Hearts", "Diamonds"}; class Hand { private: Card hand[5]; public: bool InputHand() { std::string suit, face; for (int i = 0; i < 6; i++) { std::cin >> suit; if (suit == "-1") { break; } std::cin >> face; hand[i].Input(suit, face); } return true; } Card& operator[](int index) { return hand[index]; } void Print() const { for (int i = 0; i < 5; ++i) { std::cout << hand[i].GetFaceString() << " of " << hand[i].GetSuitString() << std::endl; } } Card FindLargest() { for (int i = 0; i < 4; ++i) { hand[i].BiggerThan(hand[i + 1]); } return hand[4]; } bool IsFlush() { for (int i = 0; i < 4; i++) { if (hand[i].GetSuit() != hand[i + 1].GetSuit()) { return false; } } return true; } bool IsStraight() { for (int i = 0; i < 4; i++) { if (hand[i].Order1(hand[i].GetFace()) + 1 != hand[i + 1].Order1(hand[i + 1].GetFace())) { return false; } } return true; } bool IsStraightFlush() { if (IsFlush() && IsStraight()) { return true; } return false; } bool IsFourOfAKind() { std::unordered_map<int, int> faceCount; for (int i = 0; i < 5; i++) { faceCount[hand[i].Order1(hand[i].GetFace())]++; } for (auto it : faceCount) { if (it.second == 4) { return true; } } return false; } bool HasThreeOfAKind() { std::unordered_map<int, int> faceCount; for (int i = 0; i < 5; i++) { faceCount[hand[i].Order1(hand[i].GetFace())]++; } for (auto it : faceCount) { if (it.second == 3) { return true; } } return false; } bool HasOnePair() { std::unordered_map<int, int> faceCount; for (int i = 0; i < 5; i++) { faceCount[hand[i].Order1(hand[i].GetFace())]++; } for (auto it : faceCount) { if (it.second == 2) { return true; } } return false; } bool IsTwoPairs() { std::unordered_map<int, int> faceCount; for (int i = 0; i < 5; i++) { faceCount[hand[i].Order1(hand[i].GetFace())]++; } int pairCount = 0; for (auto it : faceCount) { if (it.second == 2) { pairCount++; } } if (pairCount == 2) { return true; } return false; } bool IsFullHouse() { if (HasThreeOfAKind() && HasOnePair()) { return true; } return false; } }; class Player { private: Hand hand; public: Hand GetHand() { return hand; } int Givenum(Card large) { if (hand.IsStraightFlush()) { return 10; } else if (hand.IsFourOfAKind()) { return 9; } else if (hand.IsFullHouse()) { return 8; } else if (hand.IsFlush()) { return 7; } else if (hand.IsStraight()) { return 6; } else if (hand.HasThreeOfAKind()) { return 5; } else if (hand.IsTwoPairs()) { return 4; } else if (hand.HasOnePair()) { return 3; } else { return 2; } } };
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}