Skip to content

Commit

Permalink
add function determine_winner_four_of_a_kind to fix bug in determine_…
Browse files Browse the repository at this point in the history
…winner for four of a kind tie break

Former-commit-id: 4595122
  • Loading branch information
ismael-elatifi committed Oct 25, 2020
1 parent f7f25ae commit 2dee7bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
29 changes: 28 additions & 1 deletion rlcard/games/limitholdem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,33 @@ def determine_winner(key_index, hands, all_players, potential_winner_index):
count+=1
return all_players

def determine_winner_four_of_a_kind(hands, all_players, potential_winner_index):
'''
Find out who wins in the situation of having players which all have a four of a kind
Args:
key_index(int): the position of a card in a sorted handcard
hands(list): cards of those players with a four of a kind
e.g. hands = [['CT', 'ST', 'H9', 'B9', 'C2', 'C8', 'C7'], ['CJ', 'SJ', 'H9', 'B9', 'C2', 'C8', 'C7'], ['CT', 'ST', 'H9', 'B9', 'C2', 'C8', 'C7']]
all_players(list): all the players in this round, 0 for losing and 1 for winning or draw
potential_winner_index(list): the positions of those players with same highest hand_catagory in all_players
Returns:
[0, 1, 0]: player1 wins
[1, 0, 0]: player0 wins
[1, 1, 1]: draw
[1, 1, 0]: player1 and player0 draws
'''
ranks = []
for hand in hands:
rank_1 = hand.STRING_TO_RANK[hand.best_five[-1][1]] # rank of the four of a kind
rank_2 = hand.STRING_TO_RANK[hand.best_five[0][1]] # rank of the kicker
ranks.append((rank_1, rank_2))
max_rank = max(ranks)
for i, rank in enumerate(ranks):
if rank == max_rank:
all_players[potential_winner_index[i]] = 1
return all_players

def compare_hands(hands):
'''
Compare all palyer's all seven cards
Expand Down Expand Up @@ -579,7 +606,7 @@ def final_compare(hands, potential_winner_index, all_players):
for _ in potential_winner_index:
equal_hands.append(hands[_])
if hands[potential_winner_index[0]].category == 9 or hands[potential_winner_index[0]].category == 5 or hands[potential_winner_index[0]].category == 8:
return determine_winner([0], equal_hands, all_players, potential_winner_index)
return determine_winner_four_of_a_kind(equal_hands, all_players, potential_winner_index)
if hands[potential_winner_index[0]].category == 7:
return determine_winner([2, 0], equal_hands, all_players, potential_winner_index)
if hands[potential_winner_index[0]].category == 4:
Expand Down
6 changes: 6 additions & 0 deletions tests/utils/test_holdem_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ def test_compare_hands(self):
['H2', 'H3', 'C4', 'D5', 'CQ', 'SK', 'SA']]) # straight beginning with A
self.assertEqual(winner, [0, 1])

winner = compare_hands([['S2', 'D8', 'H8', 'S7', 'S8', 'C8', 'D3'], # 8-four of a kind, kicker 7
['S2', 'D8', 'H8', 'S9', 'S8', 'C8', 'D3'], # 8-four of a kind, kicker 9
['H3', 'C3', 'HT', 'S3', 'ST', 'CT', 'D3'], # 3-four of a kind, kicker T
])
self.assertEqual(winner, [0, 1, 0])


if __name__ == '__main__':
unittest.main()

0 comments on commit 2dee7bc

Please sign in to comment.