Skip to content

Commit a28e5a0

Browse files
committed
Fixed discard logic bug in _check_three_of_a_kind and _check_pair
1 parent 8ace6f6 commit a28e5a0

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

71_Poker/python/game.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def __init__(self) -> None:
2727
"""Initialise game state and allocate both sides' starting stack."""
2828
self.human = Human(PokerGame.INITIAL_MONEY)
2929
self.dealer = Dealer(PokerGame.INITIAL_MONEY)
30-
self.deck: Deck
3130
self.pot = 0
3231

3332
# ------------------------------------------------------------------

71_Poker/python/pokerhand/evaluation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Hand evaluation logic."""
22

3+
import functools
34
from collections import Counter
45
from collections.abc import Callable
56
from typing import TYPE_CHECKING
@@ -12,6 +13,7 @@
1213
from .hand import Hand
1314

1415

16+
@functools.total_ordering
1517
class HandEvaluation:
1618
"""
1719
Evaluator and Result container for a poker hand.
@@ -156,7 +158,8 @@ def _check_three_of_a_kind(self, ctx: _EvaluationContext) -> bool:
156158
for _, c in reversed(ctx.rank_indexed)
157159
if c.rank == ctx.ranks_by_freq[0]
158160
)
159-
self.discard_indices = [idx for idx, _ in ctx.rank_indexed[0:3]]
161+
keep_rank = ctx.ranks_by_freq[0]
162+
self.discard_indices = [i for i, c in enumerate(ctx.cards) if c.rank != keep_rank]
160163
return True
161164

162165
def _check_two_pair(self, ctx: _EvaluationContext) -> bool:
@@ -183,7 +186,8 @@ def _check_pair(self, ctx: _EvaluationContext) -> bool:
183186
for _, c in reversed(ctx.rank_indexed)
184187
if c.rank == ctx.ranks_by_freq[0]
185188
)
186-
self.discard_indices = [idx for idx, _ in ctx.rank_indexed[0:3]]
189+
keep_rank = ctx.ranks_by_freq[0]
190+
self.discard_indices = [i for i, c in enumerate(ctx.cards) if c.rank != keep_rank]
187191
return True
188192

189193
def _check_partial_straight(self, ctx: _EvaluationContext) -> bool:

0 commit comments

Comments
 (0)