-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
98 lines (80 loc) · 3.19 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from card import Card
from hand import Hand
class Player():
def __init__(self, name, dealer=False, buy_in=100):
self.name = name
self.hand = Hand()
self.split_hand = Hand()
self.dealer = dealer
self.cash = buy_in
self.has_blackjack = False
def add_hand(self, hand):
self.hand = hand
def take_turn(self, deck):
if self.dealer:
while self.hand.sum() <= 17:
self.hand.add(deck.deal())
if self.hand.sum() > 21:
print('Bust!')
else:
return self._take_player_turn(deck)
return 0
def _take_player_turn(self,deck):
action = 'h'
main_done = False
# Obviously wont hit on a blackjack
if self.has_blackjack:
return
print(self)
# offer split
if (self.split_hand == None) and self.hand.can_split():
action = input('Would you like to split your hand? (y/n): ').lower()
if (action != 'y') and (action != 'n'):
print("You're obviously too stupid to split your hand if you can't enter the right character.")
print("Maybe you'll get it right next time...")
print()
elif action == 'y':
self.split_hand = self.hand.split()
self.cash -= self.split_hand.bet
self.cash -= self.split_hand.play_hand(deck)
self.cash -= self.hand.play_hand(deck)
def payout(self, dealer):
if self.has_blackjack: # already paid out
print(f"{self.name}: Blackjack!")
return
if self.hand.sum() <= 21:
print(f"{self.name}: {self.hand.sum()}")
if dealer.hand.sum() <= 21:
if self.hand.sum() >= dealer.hand.sum():
self.cash += self.hand.bet # make back money in tie
if self.hand.sum() > dealer.hand.sum():
self.cash += self.hand.bet # make money on win
else:
self.cash += 2 * self.hand.bet
else:
print(f"{self.name}: {self.hand.sum()} - Bust!")
if (self.split_hand.sum() != 0) and (self.split_hand.sum() <= 21):
print(f"{self.name}: {self.split_hand.sum()}")
if self.split_hand.sum() >= dealer.hand.sum():
self.cash += self.hand.bet # make back money in tie
if self.split_hand.sum() > dealer.hand.sum():
self.cash += self.split_hand.bet # make money on win
elif (self.split_hand.sum() > 21):
print(f"{self.name}: {self.split_hand.sum()} - Bust!")
def cleanup(self, discard):
self.hand.discard(discard)
self.split_hand.discard(discard)
self.has_blackjack = False
def __str__(self) -> str:
string = self.name + ": $" + str(self.cash) + "\n"
string += "\t" + str(self.hand)
if len(self.split_hand) != 0:
string += "\n\t" + str(self.split_hand)
return string
if __name__ == "__main__":
player = Player('austin')
hand = Hand(bet=5)
hand.add(Card('3', 'Spades'))
hand.add(Card('Ace', 'Diamonds'))
player.add_hand(hand)
print(player)