forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_238.py
62 lines (46 loc) · 1.36 KB
/
problem_238.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from random import random
card_distribution = dict()
def generate_card_distribution():
for i in range(1, 11):
if i not in card_distribution:
card_distribution[i] = 0
if i == 10:
card_distribution[i] += 4
else:
card_distribution[i] += 1
summed = sum(card_distribution.values())
prev = 0
for key in card_distribution:
card_distribution[key] = prev + (card_distribution[key]/summed)
prev = card_distribution[key]
def get_next_card_value():
rand = random()
lower, upper = 0, None
for key, value in card_distribution.items():
upper = value
if rand >= lower and rand <= upper:
return key
lower = upper
def play_turn(scores, dealer):
if dealer and scores[1] < 16:
next_card = get_next_card_value()
scores[1] += next_card
return True if scores[1] > 21 else False
if scores[0] < 21:
next_card = get_next_card_value()
if scores[0] + next_card < 22:
scores[0] += next_card
return False
def play_game():
scores = [0, 0]
won = False
dealer = False
while not won:
won = play_turn(scores, dealer)
dealer = not dealer
if scores[1] < scores[0]:
print("Player wins")
break
# Tests
generate_card_distribution()
play_game()