/
blackjack.py
105 lines (85 loc) · 3.45 KB
/
blackjack.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
99
100
101
102
from deck import Card, Deck
from player import Player, User, Dealer
class Round:
"""start a round of blackjack
Arguments
----------
deck : shared deck of cards between classes
Parameters
----------
user : player class controlled by input
dealer : player class controlled by hit rules
mxscore : maximum score without busting
"""
def __init__(self, deck):
self.deck = deck
self.user = User(deck)
self.dealer = Dealer(deck)
self.mxscore = 21
self.deck.shuffle()
def play(self):
def deal(self):
self.user.get_card()
self.user.get_card()
self.dealer.get_card()
print("\nDealer has {0} ??? for a visible total of {1} points".format(str(self.dealer), self.dealer.first_ace()))
self.dealer.get_card() # Dealer gets 2nd card after first print to keep 2nd card hidden
print("You have {0} for a total of {1} points".format(str(self.user), self.user.score()))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
check_blackjack(self)
def check_blackjack(self):
if self.dealer.score() == 21:
print("Dealer has {0} for a total of {1} points".format(str(self.dealer), self.dealer.score()))
print("Dealer got a Blackjack! You Lose!")
elif self.user.score() == 21:
print("You got a Blackjack! You Win!")
else:
user_hit(self)
def user_hit(self):
while self.user.score() <= 21 and self.user.hit():
if self.user.score() > 21:
print("You have {0} for a total of {1} points. \nYOU BUSTED!".format(str(self.user), self.user.score()))
if self.user.score() <= 21:
print("You have {0} for a total of {1} points.".format(str(self.user), self.user.score()))
if self.user.score() <= 21:
print("You stay with {0} for a total of {1} points.".format(str(self.user), self.user.score()))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
dealer_hit(self)
def dealer_hit(self):
print("Dealer has {0} for a total of {1} points.".format(str(self.dealer), self.dealer.score()))
if self.dealer.score() >= 17:
print("Dealer stays at {0}".format(self.dealer.score()))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
compare_score(self)
while self.dealer.score() < 17:
print("Dealer Hits")
self.dealer.hit()
print("Dealer has {0} for a total of {1} points.".format(str(self.dealer), self.dealer.score()))
if 17 <= self.dealer.score() <= 21:
print("Dealer stays at {0}".format(self.dealer.score()))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
compare_score(self)
elif self.dealer.score() > 21:
print("DEALER BUSTED!".format(str(self.dealer), self.dealer.score()))
def compare_score(self):
if self.user.score <= self.dealer.score:
print("The Dealer's {0} beats your {1}. \nYOU LOSE!").format(self.dealer.score(), self.user.score())
if self.user.score > self.dealer.score:
print("Your {0} beats the Dealer's {1}. \nYOU WIN!").format(self.user.score(), self.dealer.score())
deal(self)
# if self.dealer.score() <= 21:
# print("Dealer Hits")
# print("Dealer has {0} for a total of {1} points.".format(str(self.dealer), self.dealer.score()))
# else:
# print("Dealer has {0} for a total of {1} points. DEALER BUSTED!".format(str(self.dealer), self.dealer.score()))
class Blackjack:
"""holds multiple rounds of blackjack"""
def __init__(self):
pass
def test():
def round_test():
player = Round(Deck())
player.play()
round_test()
if __name__ == "__main__":
test()