-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
76 lines (57 loc) · 2.07 KB
/
Player.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Cards
import itertools
class Player:
# setup member variables
def __init__(self, name, max_score):
self.NAME = name
self.MAX_SCORE = max_score
self.hand = []
self.card_suite = Cards.Cards()
# add card to player's hand
def add_card(self, card):
self.hand.append(card)
# get player's hand
def get_hand(self):
return self.hand
# get player's name
def get_name(self):
return self.NAME
# get player's total score
def get_score(self):
score = 0
number_of_aces = 0
# get score of hand
for card in self.hand:
if card == 'A':
number_of_aces += 1
else:
score += int(self.card_suite.get_card_values(card))
# handle multiple aces in hand
if number_of_aces > 0:
score = self.handle_aces(score, number_of_aces)
return score
# handle multiple aces in hand with given score
def handle_aces(self, score, number_of_aces):
# score + optimal ace value
optimal_score = score
# get all possible ace values
ace_points = self.card_suite.get_card_values("A").split('|')
# combination of ace values as a list // [[1, 11], [1, 11], ...]
ace_list = number_of_aces * [list(map(int, ace_points))]
# get all possible combinations of ace values and set hightest score <= 21
for combination in itertools.product(*ace_list):
if sum(combination) + score <= self.MAX_SCORE:
optimal_score = sum(combination) + score
return optimal_score
# player score print with cards
def hand_with_score(self):
_ = ' '.join(self.hand)
return f'{self.NAME} has: {_} = {str(self.get_score())}'
# for dealer score print with hidden cards
def hand_with_hidden_score(self):
_ = self.hand[1:]
unknowns = ' ? '*len(_) if len(_) > 0 else ''
return f'{self.NAME} has: {self.hand[0] + unknowns} = ?'
# reset player's hand
def reset_hand(self):
self.hand = []