Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Poker
- {
- int flush; //Whether It's a flush or not.
- int straight; //Whether It's a straight, if so highest card val, else zero.
- int pair; //Pair Value = (highest_pair_val)*14^n +
- //(next_highest_pair_val)*14^n-1 and so on.
- int three; //Three value = highest_three_kind_value.
- int four; //Same as above.
- int highval; //High Card Value = (highest_card_value)*13^n +
- //(next_highest_card)*13^n-1 and so on.
- public:
- void setpoker(string &cards); //Fill the contents of the object based on the input string.
- int winorloose(Poker &b); //Determines whether current object wins or looses.
- };
- void Poker::setpoker(string &a)
- {
- vector <int> hands, cval;
- hands.assign(4, 0); //each array element gives the number of cards of
- //these type: club, or, diamond, or, heart, or spade.
- cval.assign(13, 0); //each array element gives the number of cards of
- //having value: 2, 3 ...
- //(indexing starts at 0, so 2 is at 0 and Ace is at 12)
- /*
- **Code of filling of hands and cval comes here.
- */
- flush = 0; straight = 0; highval = 0;
- three = 0; pair = 0; four = 0;
- for (int i = 0; i < 4; i++)
- if (hands[i] == 5)
- flush = 1;
- int stchk = 0; //checks how many cards we have seen straight yet.
- int HN = 1, PN = 1;
- for (int i = 0; i < 13; i++)
- {
- if (cval[i] > 0) {
- highval += i*HN;
- HN *= 13;
- if (cval[i] == 2) {
- pair += (i+1)*PN;
- PN *= 14; //I chosed it to be 14 to be able to distinguish
- //between pair of 2(at cval[0]) and no pair.
- }
- if (cval[i] == 3)
- three = i;
- if (cval[i] == 4)
- four == i;
- if (stchk == 4)
- straight = i;
- else
- stchk++;
- }
- else
- stchk = 0;
- }
- }
- int Poker::winorloose(Poker &b)
- {
- //royal flush
- if (flush && straight == 12)
- return 1;
- if (b.flush && b.straight == 12)
- return 0;
- //straight flush
- if (flush && straight)
- if (flush > b.flush || straight > b.straight)
- return 1;
- if (b.flush && b.straight)
- if (b.flush > flush || b.straight > straight)
- return 0;
- //four of kind
- if (four > b.four)
- return 1;
- if (b.four > four)
- return 0;
- //full house
- if (three && pair)
- if (!(b.three && b.pair) || three > b.three)
- return 1;
- if (b.three && b.pair)
- if (!(three && pair) || b.three > three)
- return 0;
- //Flush
- if (flush > b.flush)
- return 1;
- if (b.flush > flush)
- return 0;
- //straight
- if (straight > b.straight)
- return 1;
- if (b.straight > straight)
- return 0;
- //three of kind
- if (three > b.three)
- return 1;
- if (b.three > three)
- return 0;
- //pair
- if (pair > b.pair)
- return 1;
- if (b.pair > pair)
- return 0;
- //highest value card
- if (highval > b.highval)
- return 1;
- if (b.highval > highval)
- return 0;
- }
Add Comment
Please, Sign In to add comment