Determines the outcome of comparing self hand with other hand. Returns the output as 'Win', 'Loss', 'Tie' according to the rules of Texas Hold'em. Here are some examples: >>> player = PokerHand("2H 3H 4H 5H 6H") # Stright flush >>> opponent = PokerH
(self, other: PokerHand)
| 139 | return self._hand |
| 140 | |
| 141 | def compare_with(self, other: PokerHand) -> str: |
| 142 | """ |
| 143 | Determines the outcome of comparing self hand with other hand. |
| 144 | Returns the output as 'Win', 'Loss', 'Tie' according to the rules of |
| 145 | Texas Hold'em. |
| 146 | |
| 147 | Here are some examples: |
| 148 | >>> player = PokerHand("2H 3H 4H 5H 6H") # Stright flush |
| 149 | >>> opponent = PokerHand("KS AS TS QS JS") # Royal flush |
| 150 | >>> player.compare_with(opponent) |
| 151 | 'Loss' |
| 152 | |
| 153 | >>> player = PokerHand("2S AH 2H AS AC") # Full house |
| 154 | >>> opponent = PokerHand("2H 3H 5H 6H 7H") # Flush |
| 155 | >>> player.compare_with(opponent) |
| 156 | 'Win' |
| 157 | |
| 158 | >>> player = PokerHand("2S AH 4H 5S 6C") # High card |
| 159 | >>> opponent = PokerHand("AD 4C 5H 6H 2C") # High card |
| 160 | >>> player.compare_with(opponent) |
| 161 | 'Tie' |
| 162 | """ |
| 163 | # Breaking the tie works on the following order of precedence: |
| 164 | # 1. First pair (default 0) |
| 165 | # 2. Second pair (default 0) |
| 166 | # 3. Compare all cards in reverse order because they are sorted. |
| 167 | |
| 168 | # First pair and second pair will only be a non-zero value if the card |
| 169 | # type is either from the following: |
| 170 | # 21: Four of a kind |
| 171 | # 20: Full house |
| 172 | # 17: Three of a kind |
| 173 | # 16: Two pairs |
| 174 | # 15: One pair |
| 175 | if self._hand_type > other._hand_type: |
| 176 | return "Win" |
| 177 | elif self._hand_type < other._hand_type: |
| 178 | return "Loss" |
| 179 | elif self._first_pair == other._first_pair: |
| 180 | if self._second_pair == other._second_pair: |
| 181 | return self._compare_cards(other) |
| 182 | else: |
| 183 | return "Win" if self._second_pair > other._second_pair else "Loss" |
| 184 | return "Win" if self._first_pair > other._first_pair else "Loss" |
| 185 | |
| 186 | # This function is not part of the problem, I did it just for fun |
| 187 | def hand_name(self) -> str: |