Skip to content

Commit

Permalink
add function determine_winner_straight to fix bug in determine_winner…
Browse files Browse the repository at this point in the history
… for straight and straight flush

Former-commit-id: a9fc535
  • Loading branch information
ismael-elatifi committed Oct 25, 2020
1 parent f7f25ae commit 7b90126
Show file tree
Hide file tree
Showing 2 changed files with 49 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,31 @@ def determine_winner(key_index, hands, all_players, potential_winner_index):
count+=1
return all_players

def determine_winner_straight(hands, all_players, potential_winner_index):
'''
Find out who wins in the situation of having players all having a straight or straight flush
Args:
key_index(int): the position of a card in a sorted handcard
hands(list): cards of those players which all have a straight or straight flush
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
'''
highest_ranks = []
for hand in hands:
highest_rank = hand.STRING_TO_RANK[hand.best_five[-1][1]] # cards are sorted in ascending order
highest_ranks.append(highest_rank)
max_highest_rank = max(highest_ranks)
for i_player in range(len(highest_ranks)):
if highest_ranks[i_player] == max_highest_rank:
all_players[potential_winner_index[i_player]] = 1
return all_players

def compare_hands(hands):
'''
Compare all palyer's all seven cards
Expand Down Expand Up @@ -578,7 +603,7 @@ def final_compare(hands, potential_winner_index, all_players):
equal_hands = []
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:
if hands[potential_winner_index[0]].category == 8:
return determine_winner([0], 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)
Expand All @@ -590,3 +615,5 @@ def final_compare(hands, potential_winner_index, all_players):
return determine_winner([4, 2, 1, 0], equal_hands, all_players, potential_winner_index)
if hands[potential_winner_index[0]].category == 1 or hands[potential_winner_index[0]].category == 6:
return determine_winner([4, 3, 2, 1, 0], equal_hands, all_players, potential_winner_index)
if hands[potential_winner_index[0]].category in [5, 9]:
return determine_winner_straight(equal_hands, all_players, potential_winner_index)
21 changes: 21 additions & 0 deletions tests/utils/test_holdem_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ def test_compare_hands(self):
['H2', 'H3', 'C4', 'D5', 'CQ', 'SK', 'SA']]) # straight beginning with A
self.assertEqual(winner, [0, 1])

winner = compare_hands([['H5', 'HQ', 'C2', 'D3', 'S4', 'S5', 'HT'], # pair
['D5', 'ST', 'C2', 'D3', 'S4', 'S5', 'HA'], # A,2,3,4,5
['H6', 'HQ', 'C2', 'D3', 'S4', 'S5', 'HT'], # 2,3,4,5,6
['H4', 'HQ', 'C2', 'D3', 'S4', 'S5', 'HT'], # pair
])
self.assertEqual(winner, [0, 0, 1, 0])

winner = compare_hands([['D5', 'ST', 'C2', 'D3', 'S4', 'S5', 'HA'], # A,2,3,4,5
['H6', 'H7', 'CK', 'DQ', 'SJ', 'ST', 'HA'], # T,J,Q,K,A
['HA', 'HT', 'CK', 'DQ', 'SJ', 'ST', 'DT'], # T,J,Q,K,A
['H2', 'H3', 'C4', 'D2', 'CQ', 'S2', 'SA'], # three of a kind
])
self.assertEqual(winner, [0, 1, 1, 0])

winner = compare_hands([['H5', 'HQ', 'C2', 'D3', 'S4', 'S5', 'HT'], # pair
['D5', 'ST', 'S2', 'S3', 'S4', 'S5', 'SA'], # A,2,3,4,5 suited
['C6', 'HQ', 'C2', 'C3', 'C4', 'C5', 'HT'], # 2,3,4,5,6 suited
['H7', 'H6', 'C9', 'C3', 'C4', 'C5', 'HT'], # 3,4,5,6,7 not suited
])
self.assertEqual(winner, [0, 0, 1, 0])


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

0 comments on commit 7b90126

Please sign in to comment.