HenryRLee / PokerHandEvaluator

Poker-Hand-Evaluator: An efficient poker hand evaluation algorithm and its implementation, supporting 7-card poker and Omaha poker evaluation
Apache License 2.0
373 stars 80 forks source link

Rank operators bug #25

Closed arcnotch closed 2 years ago

arcnotch commented 4 years ago

Hi!

Me and my friends are working on a project together and we are required to use poker evaluation as part of the project. We used PokerHandEvaluator library to compare poker hands (5vs5). It works well, but there was a case on our Unitests that we notice a bug in the library, rank.h, <,>,<=,>= operators (https://github.com/HenryRLee/PokerHandEvaluator/blob/master/cpp/include/phevaluator/rank.h). We notice that the rank object returns true when we compare two poker hands that supposed to be marked as a tie. For example: Hand 1: 10DIAMOND, 11HEART, 12SPADE, 13CLUB, 1DIAMOND Hand 2: 10HEART, 11DIAMOND, 12DIAMOND 13HEART, 1CLUB We used the operator < to compare the two and the returned value was true. Source code:

bool operator<(const Rank& other) const {
    return value_ >= other.value_;
  }

  bool operator<=(const Rank& other) const {
    return value_ > other.value_;
  }

  bool operator>(const Rank& other) const {
    return value_ <= other.value_;
  }

  bool operator>=(const Rank& other) const {
    return value_ < other.value_;
  }

Our fix is:

bool operator<(const Rank& other) const {
    return value_ > other.value_;
  }

  bool operator<=(const Rank& other) const {
    return value_ >= other.value_;
  }

  bool operator>(const Rank& other) const {
    return value_ < other.value_;
  }

  bool operator>=(const Rank& other) const {
    return value_ <= other.value_;
  }
HenryRLee commented 4 years ago

Hi @arcnotch, thanks for reporting the issue. I believe you're right. A pull request is welcome if you are interested.

fbrunodr commented 4 years ago

You could make a function that checks for a tie before calling a comparison.