Hello everyone.
I am writing a mini poker evaluation program and it is almost done. I got it to compile and work, but I ran into an issue. My hand comparisons are not working properly. For instance if you had the following
hand one 2clubs, 3clubs, 4clubs, 5clubs and 6clubs; compared against hand two which consists of 10spades, Jspades, QSpades, KSpades and AceSpades. Obviously hand two should be the winning hand, right?
Well, my program tells me that hand one is the winner and that is because hand comparison is not done properly and I can't seem to find the issue. It is a multiple file program, but I will only post the code I think is causing the issue.
Code:
#include <algorithm>
#include "evaluator.h"
Evaluator::Evaluator(void)
{
}
Evaluator::~Evaluator(void)
{
}
SortedEvaluator::SortedEvaluator(void)
{
}
SortedEvaluator::~SortedEvaluator(void)
{
}
inline std::string SortedEvaluator::ToString(std::vector<const Card*> hand)
{
std::sort(hand.begin(), hand.end(), compare);
cout << "\t";
for(vector<const Card*>::const_iterator it = hand.begin(); it != hand.end(); ++it)
{
cout << (*it) -> ToString() << " ";
}
return " ";
}
bool lessThan(const Card* c1, const Card* c2)
{
if(c1 -> GetPip() < c2 -> GetPip())
{
return true;
}
else
{
return false;
}
}
inline int SortedEvaluator::compareHands(std::vector<const Card*> hand1, std::vector<const Card*> hand2)
{
std::sort(hand1.begin(), hand1.end(), compare);
std::sort(hand2.begin(), hand2.end(), compare);
//Straight Flush
if(isFlush(hand1) == true && isStraight(hand1) == true && isFlush(hand2) == true && isStraight(hand2) == true)
{
return compKick(hand1, hand2);
}
else if(isFlush(hand1) == true && isStraight(hand1) == true && isStraight(hand2) == false)
{
return 1;
}
else if((isStraight(hand1) == false) && (isFlush(hand2) == true && isStraight(hand2) == true))
{
return -1;
}
//Four of a Kind
else if(isQuad(hand1) && isQuad(hand2))
{
return compQuad(hand1, hand2);
}
else if(isQuad(hand1) == true && isQuad(hand2) == false)
{
return 1;
}
else if(isQuad(hand1) == false && isQuad(hand2) == true)
{
return -1;
}
//Full House
else if(isFullHouse(hand1) && isFullHouse(hand2))
{
return compFHouse(hand1, hand2);
}
else if(isFullHouse(hand1) == true && isFullHouse(hand2) == false)
{
return 1;
}
else if(isFullHouse(hand1) == false && isFullHouse(hand2) == true)
{
return -1;
}
//Flush
else if(isFlush(hand1) == true && isFlush(hand2) == true)
{
return compFlush(hand1, hand2);
}
else if(isFlush(hand1) == true && isFlush(hand2) == false)
{
return 1;
}
else if(isFlush(hand1) == false && isFlush(hand2) == true)
{
return -1;
}
//Straight
else if(isStraight(hand1) && isStraight(hand2))
{
return compStraight(hand1, hand2);
}
else if(isStraight(hand1) == true && isStraight(hand2) == false)
{
return 1;
}
else if(isStraight(hand1) == false && isStraight(hand2) == false)
{
return -1;
}
//Trips
else if(isTrips(hand1) && isTrips(hand2))
{
return compTrips(hand1, hand2);
}
else if(isTrips(hand1) == true && isTrips(hand2) == false)
{
return 1;
}
else if(isTrips(hand1) == false && isTrips(hand2) == true)
{
return -1;
}
//Two Pairs
else if(isTPair(hand1) && isTPair(hand2))
{
return compTPair(hand1, hand2);
}
else if(isTPair(hand1) == true && isTPair(hand2) == false)
{
return 1;
}
else if(isTPair(hand1) == false && isTPair(hand2) == true)
{
return -1;
}
//Pair
else if(isPair(hand1) && isPair(hand2))
{
return compPair(hand1, hand2);
}
else if(isPair(hand1) == true && isPair(hand2) == false)
{
return 1;
}
else if(isPair(hand1) == false && isPair(hand2) == true)
{
return -1;
}
else
{
compKick(hand1, hand2);
}
return 0;
}
bool SortedEvaluator::isFlush(const vector<const Card*> hand)
{
bool Flush;
cout << "isFlush() called\n";
if(hand[0] -> GetSuit() == hand[1] -> GetSuit() && hand[1] -> GetSuit() == hand[2] -> GetSuit() &&
hand[2] -> GetSuit() == hand[3] -> GetSuit() && hand[3] -> GetSuit() == hand[4] -> GetSuit())
{
Flush = true;
}
else
{
Flush = false;
}
return Flush;
}
bool SortedEvaluator::isStraight(const vector<const Card*> hand)
{
bool Straight = isWStraight(hand);
if(Straight == true)
{
return true;
}
if(hand[0] -> GetSuit() + 4 == hand[1] -> GetSuit() && hand[1] -> GetSuit() + 3 == hand[2] -> GetSuit() &&
hand[2] -> GetSuit() + 2 == hand[3] -> GetSuit() && hand[3] -> GetSuit() + 1 == hand[4] -> GetSuit())
{
Straight = true;
}
else
{
Straight = false;
}
cout << "isStraight() called\n";
return Straight;
}
bool SortedEvaluator::isWStraight(const vector<const Card*> hand)
{
if(hand[0] -> GetPip() == Card::_2 && hand[1] -> GetPip() == Card::_3 &&
hand[2] -> GetPip() == Card::_4 && hand[3] -> GetPip() == Card::_5 &&
hand[4] -> GetPip() == Card::_A)
{
return true;
}
return false;
}
bool SortedEvaluator::isQuad(const vector<const Card*> hand)
{
bool Quad;
if(hand[0] -> GetSuit() == hand[3] -> GetSuit() || hand[1] -> GetSuit() == hand[4] -> GetSuit())
{
Quad = true;
}
else
{
Quad = false;
}
return Quad;
}
bool SortedEvaluator::isFullHouse(const vector<const Card*> hand)
{
bool FHouse;
if((hand[0] -> GetPip() == hand[1] -> GetSuit() && hand[2] -> GetPip() == hand[4] -> GetPip()) ||
(hand[0] -> GetPip() == hand[2] -> GetSuit() && hand[3] -> GetPip() == hand[4] -> GetPip()))
{
FHouse = true;
}
else
{
FHouse = false;
}
return FHouse;
}
bool SortedEvaluator::isTrips(const vector<const Card*> hand)
{
bool Trips;
if(hand[0] -> GetPip() == hand[2] -> GetPip() ||
hand[1] -> GetPip() == hand[3] -> GetPip() ||
hand[2] -> GetPip() == hand[4] -> GetPip())
{
Trips = true;
}
else
{
Trips = false;
}
return Trips;
}
bool SortedEvaluator::isTPair(const vector<const Card*> hand)
{
bool TPair;
if((hand[0] -> GetPip() == hand[1] -> GetPip() && hand[2] -> GetPip() == hand[3] -> GetPip()) ||
(hand[1] -> GetPip() == hand[2] -> GetPip() && hand[3] -> GetPip() == hand[4] -> GetPip()) ||
(hand[0] -> GetPip() == hand[1] -> GetPip() && hand[3] -> GetPip() == hand[4] -> GetPip()))
{
TPair = true;
}
else
{
TPair = false;
}
return TPair;
}
bool SortedEvaluator::isPair(const vector<const Card*> hand)
{
bool Pair;
if((hand[0] -> GetPip() == hand[1] -> GetPip()) || (hand[2] -> GetPip() == hand[3] -> GetPip()) ||
(hand[1] -> GetPip() == hand[2] -> GetPip()) || (hand[3] -> GetPip() == hand[4] -> GetPip()))
{
Pair = true;
}
else
{
Pair = false;
}
return Pair;
}
//Hand Comparisons
int SortedEvaluator::compKick(const vector<const Card*> h1, const vector<const Card*> h2)
{
int ret = 0;
for(int i = 4; i >= 0; i--)
{
if(h1[i] -> GetPip() > h2[i] -> GetPip())
{
ret = 1;
return ret;
}
if(h1[i] -> GetPip() < h2[i] -> GetPip())
{
ret = -1;
return ret;
}
}
return ret;
}
int SortedEvaluator::compFlush(const vector<const Card*> h1, const vector<const Card*> h2)
{
return compKick(h1, h2);
}
int SortedEvaluator::compStraight(const vector<const Card*> h1, const vector<const Card*> h2)
{
int ret = 0;
if(isStraight(h1) == true && isStraight(h2) == true)
{
ret = 0;
return ret;
}
if(isStraight(h1) == true && isStraight(h2) == false)
{
ret = 1;
return ret;
}
if(isStraight(h1) == false && isStraight(h2) == true)
{
ret = -1;
return ret;
}
if(isWStraight(h1) == true && isWStraight(h2) == true)
{
ret = 0;
return ret;
}
if(isStraight(h1) == true && isWStraight(h2) == true)
{
ret = 1;
return ret;
}
if(isWStraight(h1) == true && isStraight(h2) == true)
{
ret = -1;
return ret;
}
return compKick(h1, h2);
}
int SortedEvaluator::compQuad(const vector<const Card*> h1, const vector<const Card*> h2)
{
int ret = 0;
if(h1[2] -> GetPip() > h2[2] -> GetPip())
{
ret = 1;
return ret;
}
if(h1[2] -> GetPip() < h2[2] -> GetPip())
{
ret = -1;
return ret;
}
return compKick(h1, h2);
}
int SortedEvaluator::compFHouse(const vector<const Card*> h1, const vector<const Card*> h2)
{
int ret = 0;
if(h1[2] -> GetPip() > h2[2] -> GetPip())
{
ret = 1;
return ret;
}
if(h1[2] -> GetPip() < h2[2] -> GetPip())
{
ret = -1;
return ret;
}
return compKick(h1, h2);
}
int SortedEvaluator::compTrips(const vector<const Card*> h1, const vector<const Card*> h2)
{
int ret = 0;
if(h1[2] -> GetPip() > h2[2] -> GetPip())
{
ret = 1;
return ret;
}
if(h1[2] -> GetPip() < h2[2] -> GetPip())
{
ret = -1;
return ret;
}
return compKick(h1, h2);
}
int SortedEvaluator::compTPair(const vector<const Card*> h1, const vector<const Card*> h2)
{
int ret = 0;
if(h1[1] -> GetPip() > h2[1] -> GetPip())
{
ret = 1;
return ret;
}
if(h1[1] -> GetPip() < h2[1] -> GetPip())
{
ret = -1;
return ret;
}
if(h1[3] -> GetPip() > h2[3] -> GetPip())
{
ret = 1;
return ret;
}
if(h1[3] -> GetPip() < h2[3] -> GetPip())
{
ret = -1;
return ret;
}
return compKick(h1, h2);
}
int SortedEvaluator::compPair(const vector<const Card*> h1, const vector<const Card*> h2)
{
int PipVal1 = 0;
int PipVal2 = 0;
int ret = 0;
for(unsigned i = 4; h1[i] != h1[--i]; --i)
{
PipVal1 = h1[0] -> GetPip();
}
for(unsigned i = 4; h2[i] != h2[--i]; --i)
{
PipVal2 = h2[0] -> GetPip();
}
if(PipVal1 > PipVal2)
{
ret = 1;
return ret;
}
if(PipVal1 < PipVal2)
{
ret = -1;
return ret;
}
return compKick(h1, h2);
}
Thank you in advance.