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();
}