fc + add project with comparaison of two hands

This commit is contained in:
Jean Lapostolle 2023-04-10 16:28:05 +02:00
commit 4782b629a8
2 changed files with 267 additions and 0 deletions

108
hand_utils.py Normal file
View File

@ -0,0 +1,108 @@
from collections import Counter
colorOrder = ['C', 'D', 'H', 'S'] # useless but good for comprehension
valueOrder = ['2', '3', '4', '5','6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
handOrder = ['Hauteur', 'Paire', 'Double Paire', 'Brelan', 'Quinte', 'Couleur', 'Full', 'Carré', 'Quinte Flush']
def has_x_same_value(hand):
""" Return value and count of maximum same value in hand """
return Counter(hand["value"]).most_common(1)[0]
def is_following_value(hand):
""" Return True if value are following in order """
hand_sort = sorted(hand["value"], key= lambda value: valueOrder.index(value))
for valueA, valueB in zip(hand_sort[:-1], hand_sort[1:]):
if valueOrder.index(valueB) - valueOrder.index(valueA) != 1:
return False
return True
def is_same_color(hand):
""" Return True if all cards of hand is same color """
return len(set(hand["color"])) == 1
def has_x_y_same_value(hand):
""" Return value and count of all multiple value in hand """
count = Counter(hand["value"]).most_common()
multiple = [ c for c in count if c[1] > 1]
return multiple
def calcul_hand(hand):
""" Return the value of the hand and all arguments needed in order in case of equality """
multiple_value = has_x_y_same_value(hand)
flush = is_same_color(hand)
straight = is_following_value(hand)
if flush and straight:
return "Quinte Flush", max(hand['value'], key=lambda value: valueOrder.index(value))
if len(multiple_value) == 1 and multiple_value[0][1] == 4:
quad_value = multiple_value[0][0]
high = set(hand['value']) - {quad_value}
high = list(high)[0]
return "Carré", quad_value, high
if len(multiple_value) == 2 and multiple_value[0][1] == 3:
return "Full", multiple_value[0][0], multiple_value[1][0]
if flush:
return "Couleur", *sorted(hand["value"], key=lambda value: valueOrder.index(value), reverse=True)
if straight:
return "Quinte", max(hand['value'], key=lambda value: valueOrder.index(value))
if len(multiple_value) == 1 and multiple_value[0][1] == 3:
set_value = multiple_value[0][0]
high = set(hand['value']) - {set_value}
high = sorted(list(high), key=lambda value: valueOrder.index(value), reverse =True)
return "Brelan", multiple_value[0][0], *high
if len(multiple_value) == 2:
pairs_values = (multiple_value[0][0], multiple_value[1][0])
pairs_sorted = sorted(pairs_values, key= lambda value: valueOrder.index(value), reverse=True)
high = set(hand['value']) - set(pairs_values)
high = list(high)[0]
return "Double Paire", *pairs_sorted, high
if len(multiple_value) == 1:
pair_value = multiple_value[0][0]
high = set(hand['value']) - {pair_value}
high = sorted(list(high), key= lambda value: valueOrder.index(value), reverse=True)
return "Paire", pair_value, *high
return "Hauteur", *sorted(hand["value"], key= lambda value: valueOrder.index(value), reverse=True)
def compare_hand(hand_1, hand_2):
""" Compare who win between two hands
0 : equality,
1 : hand 1 win
2 : hand 2 win
:string: : Win description
"""
hand_1_value, *hand_1_args = calcul_hand(hand_1)
hand_2_value, *hand_2_args = calcul_hand(hand_2)
hand_1_value_ordering = handOrder.index(hand_1_value)
hand_2_value_ordering = handOrder.index(hand_2_value)
if hand_1_value_ordering > hand_2_value_ordering:
return 1, f"La main 1 gagne avec {hand_1_value}"
if hand_2_value_ordering > hand_1_value_ordering:
return 2, f"La main 2 gagne avec {hand_2_value}"
# it is possible to compare because 2 hands with same value have the same number of arguments.
for i in range(len(hand_1_args)):
arg_hand_1_ordering = valueOrder.index(hand_1_args[i])
arg_hand_2_ordering = valueOrder.index(hand_2_args[i])
if arg_hand_1_ordering > arg_hand_2_ordering:
return 1, f"La main 1 gagne avec {hand_1_value} et une hauteur de {hand_1_args[i]}"
if arg_hand_2_ordering > arg_hand_1_ordering:
return 2, f"La main 2 gagne avec {hand_2_value} et une hauteur de {hand_2_args[i]}"
return 0, "Les deux mains sont égales"

159
test_hand_utils.py Normal file
View File

@ -0,0 +1,159 @@
from hand_utils import has_x_same_value, is_following_value, is_same_color, has_x_y_same_value, calcul_hand, compare_hand
hand_royal_flush = {
"color": ['C','C','C','C','C'],
"value": ['A', 'J', 'Q', 'K', 'T']
}
hand_straight_flush = {
"color": ['C','C','C','C','C'],
"value": ['5', '7', '9', '6', '8']
}
hand_quads = {
"color": ['C','D','H','C','S'],
"value": ['A', 'A', 'A', '5', 'A']
}
hand_full = {
"color": ['C', 'D', 'C', 'H', 'S'],
"value": ['3', '3', '5', '5', '3']
}
hand_flush = {
"color": ['C','C','C','C','C'],
"value": ['3', '9', '7', 'T', 'A']
}
hand_straight = {
"color": ['C', 'D', 'C', 'H', 'S'],
"value": ['5','6','7','8','9']
}
hand_set = {
"color": ['C','D','H','C','S'],
"value": ['J', 'K', 'J', '5', 'J']
}
hand_twopair = {
"color": ['C','D','H','D','C'],
"value": ['T', 'T', '6', '6', 'K']
}
hand_onepair = {
"color": ['C','D','H','S','C'],
"value": ['A', '8', 'A', '2', '6']
}
hand_highcard = {
"color": ['C','D','H','S','C'],
"value": ['K', 'J', 'T', '6', '3']
}
def test_has_x_same_value():
assert has_x_same_value(hand_onepair) == ('A', 2)
assert has_x_same_value(hand_set) == ('J', 3)
def test_is_following_value():
assert is_following_value(hand_straight)
assert not is_following_value(hand_full)
def test_is_same_color():
assert is_same_color(hand_flush)
assert not is_same_color(hand_straight)
def test_has_x_y_same_value():
assert has_x_y_same_value(hand_full) == [('3', 3), ('5', 2)]
assert has_x_y_same_value(hand_twopair) == [('T', 2), ('6', 2)]
assert has_x_y_same_value(hand_onepair) == [('A', 2)]
def test_calcul_hand():
assert calcul_hand(hand_royal_flush) == ('Quinte Flush', 'A')
assert calcul_hand(hand_straight_flush) == ('Quinte Flush', '9')
assert calcul_hand(hand_quads) == ('Carré', 'A', '5')
assert calcul_hand(hand_full) == ('Full', '3', '5')
assert calcul_hand(hand_flush) == ('Couleur','A', 'T', '9', '7', '3')
assert calcul_hand(hand_straight) == ('Quinte', '9')
assert calcul_hand(hand_set) == ('Brelan', 'J', 'K', '5')
assert calcul_hand(hand_twopair) == ('Double Paire', 'T', '6', 'K')
assert calcul_hand(hand_onepair) == ('Paire', 'A', '8', '6', '2')
assert calcul_hand(hand_highcard) == ('Hauteur', 'K', 'J', 'T', '6', '3')
# Different hand for make difficulties to compare
hand_quads_ace = {
"color": ['C','D','S','H','S'],
"value": ['A', 'A', '9', 'A', 'A']
}
hand_quads_nine = {
"color": ['C','D','H','C','S'],
"value": ['9', '9', '9', '7', '9']
}
hand_full_five = {
"color": ['C', 'D', 'C', 'H', 'S'],
"value": ['3', '5', '5', '5', '3']
}
hand_full_three_seven = {
"color": ['C', 'D', 'C', 'H', 'S'],
"value": ['3', '3', '7', '7', '3']
}
hand_flush_queen = {
"color": ['C','C','C','C','C'],
"value": ['3', '9', 'Q', 'T', '7']
}
hand_straight_queen = {
"color": ['C', 'D', 'C', 'H', 'S'],
"value": ['Q','T','J','8','9']
}
hand_set_jack_three = {
"color": ['C','D','H','C','S'],
"value": ['J', 'K', 'J', '3', 'J']
}
hand_set_ace = {
"color": ['C','D','H','C','S'],
"value": ['A', 'K', 'A', '5', 'A']
}
hand_twopair_nine_eight = {
"color": ['C','D','H','D','C'],
"value": ['9', '8', '8', '9', 'K']
}
hand_twopair_queen = {
"color": ['C','D','H','D','C'],
"value": ['T', 'T', '6', '6', 'Q']
}
hand_onepair_ace_seven = {
"color": ['C','D','H','S','C'],
"value": ['A', '8', 'A', '2', '7']
}
hand_onepair_queen = {
"color": ['C','D','H','S','C'],
"value": ['Q', 'Q', 'J', '2', '6']
}
hand_highcard_queen = {
"color": ['C','D','H','S','C'],
"value": ['9', 'J', 'Q', 'K', '3']
}
def test_compare_hand():
assert compare_hand(hand_onepair, hand_twopair)[0] == 2
assert compare_hand(hand_flush, hand_highcard)[0] == 1
assert compare_hand(hand_royal_flush, hand_straight_flush)[0] == 1
assert compare_hand(hand_quads, hand_quads_ace)[0] == 2
assert compare_hand(hand_quads, hand_quads_nine)[0] == 1
assert compare_hand(hand_full, hand_full_five)[0] == 2
assert compare_hand(hand_full, hand_full_three_seven)[0] == 2
assert compare_hand(hand_flush, hand_flush_queen)[0] == 1
assert compare_hand(hand_straight, hand_straight_queen)[0] == 2
assert compare_hand(hand_set, hand_set_jack_three)[0] == 1
assert compare_hand(hand_set, hand_set_ace)[0] == 2
assert compare_hand(hand_twopair, hand_twopair_nine_eight)[0] == 1
assert compare_hand(hand_twopair, hand_twopair_queen)[0] == 1
assert compare_hand(hand_onepair, hand_onepair_queen)[0] == 1
assert compare_hand(hand_onepair, hand_onepair_ace_seven)[0] == 2
assert compare_hand(hand_highcard, hand_highcard_queen)[0] == 2
assert compare_hand(hand_flush, hand_flush)[0] == 0