-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.py
181 lines (132 loc) · 4.25 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import random
from random import choice
class Card:
def __init__(self, suit, value):
self.suit = suit
self.value = value
def __repr__(self):
return f"{self.suit} of {self.value}"
class Deck:
def __init__(self,):
self.cards = [Card(s, v) for s in ["Spades", "Clubs", "Hearts", "Diamonds"]
for v in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]]
self.shuffle()
def shuffle(self):
random.shuffle(self.cards)
def deal(self, player):
single_card = self.cards.pop()
player.cards.append(single_card)
return single_card
class Player:
def __init__(self, dealer=False):
self.dealer = dealer
self.cards = []
self.value = 0
def add_card(self, card):
self.cards.append(card)
def calculate_value(self, busted=False):
self.value = 0
for card in self.cards:
self.value += int(card.value)
if self.value > 21:
busted = True
return busted
def get_value(self):
self.calculate_value
return self.value
def display_cards(self):
pass
def set_play(self, player, choice):
if choice == 'hit':
deck.deal_card(player)
player.play = "Hit"
elif choice == 'stay':
player.play = "Stay"
class Dealer(Player):
def __init__(self):
super().__init__()
def display_cards(self, showall=False):
print(self.cards)
for card in self.cards[:1]:
print(card)
self.calculate_value()
class Human(Player):
def __init__(self, name):
self.name = name
super().__init__()
def display_cards(self):
print('\n' f"{self.name} hand after dealing card:")
print(self.cards)
for card in self.cards:
print(card)
self.calculate_value()
class Game:
def __init__(self):
self.playing = True
def check_for_bust(self, player):
if player.value > 21:
print(f"{player.name} BUST! {player.value}")
return True
# This will Show Who Has won the game
def compare(self, player1, player2):
if player1.value:
print(
f"You busted! Dealer wins! Your score is {player1.value} ; Dealer's total is {player2.value}")
elif player2.value:
print(f"You win, Dealer Busted! Your total is {player1.value}")
elif player1.value == player2.value:
print("It is a tie. You both had the same number!")
elif player1.value > player2.value:
print("You win!")
elif player1.value < player2.value:
print("You lose")
game = Game()
player_name = input("What is your name? ")
while game.playing:
player1 = Human(player_name)
player2 = Dealer()
# This should deal the first two cards
deck = Deck()
deck.deal(player1)
deck.deal(player2)
player1.display_cards()
player2.display_cards()
while True:
# This is the Players Hand
if player1.value > 21:
print("You busted")
break
choice = input("Would you like to hit or stay? (Hit/Stay").lower()
if choice == 'hit':
deck.deal(player1)
player1.calculate_value()
elif choice == 'stay':
player1.play = 'stay'
break
else:
print("Please choose a valid option. ")
continue
player1.display_cards()
# This is the Dealers Hand
if player2.value <= 16:
print("Dealer Hits")
deck.deal(player2)
player2.calculate_value()
player2.play = 'hit'
if player2.value > 21:
print("Dealer Busted")
break
elif player2.value > 16:
player2.play = 'stay'
player2.display_cards()
if player2.play == 'stay' and player1.play == 'stay':
break
game.compare(player1, player2)
for card in player2.cards:
print(card, '\n')
play_again = input("would you like to play again> (y/n) ")
if play_again == 'n':
game.playing = False
print("Thanks for playing")
elif play_again == 'y':
game.playing = True