-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackJack.py
More file actions
executable file
·125 lines (105 loc) · 3.52 KB
/
Copy pathBlackJack.py
File metadata and controls
executable file
·125 lines (105 loc) · 3.52 KB
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
import random
import itertools
# generates the deck and card values
SUIT = 'scdh'
RANK = '23456789TJQKA'
DECK = tuple(''.join(card) for card in itertools.product(RANK, SUIT))
VAL = []
for a in range(9):
VAL += [a + 2] * 4
for _ in range(3):
VAL += [10] * 4
VAL += [1] * 4
DECKVAL = dict(zip(DECK, VAL))
counter = 0
def shuffle():
# shuffles the deck into random order
global s_deck
s_deck = random.sample(DECK, 52)
class Hand:
global counter, s_deck
def __init__(self):
# deals 2 cards
# sets up variables hand_sum and init_sum for totaling
global counter
self.cards = list(s_deck[counter:counter + 2])
counter += 2
self.hand_sum = 0
self.init_sum = 0
def sum_hand(self):
# totals the hand
self.hand_sum = 0
self.init_sum = 0
for b in range(len(self.cards)):
self.init_sum += DECKVAL[self.cards[b]]
if ('As' in self.cards or 'Ac' in self.cards or 'Ad' in self.cards or 'Ah' in self.cards)\
and self.cards and self.init_sum + 10 <= 21: # special ace rule conditions
self.hand_sum = self.init_sum + 10
else:
self.hand_sum = self.init_sum
def draw(self):
# draws a card from the deck
global counter
self.cards += list(s_deck[counter:counter + 1])
counter += 1
def hit_or_stay(self, dealer):
# asks the player if they want to hit or stay
self.bust(dealer)
print("\n\nDealer's Hand:", dealer.cards[0], "--")
print("Your hand is:", self.cards, " Your sum is:", self.hand_sum, "\n")
choice = input("Hit or Stay? ").lower()
if choice == 'hit':
self.draw()
self.hit_or_stay(dealer)
elif choice == 'stay':
self.total(dealer)
else:
print("Please enter hit or stay")
self.hit_or_stay(dealer)
def total(self, dealer):
# determines winner
if self.hand_sum > dealer.hand_sum:
print("You won the hand")
elif self.hand_sum < dealer.hand_sum:
if dealer.hand_sum <= 21:
print("You lost the hand")
else:
print("Dealer busted")
else:
print("You tied")
print("Dealer's hand:", dealer.cards, " Dealer's sum:", dealer.hand_sum)
print("Your hand:", self.cards, "Your sum:", self.hand_sum)
print("\n*******************\n")
def bust(self, dealer):
# checks for player busting
self.sum_hand()
if self.hand_sum > 21:
print("YOU BUSTED\n")
self.hand_sum = 0
self.total(dealer)
else:
pass
class Dealer(Hand):
def __init__(self):
Hand.__init__(self)
self.logic()
def logic(self):
# makes sure the dealer hits "soft 17"
self.sum_hand()
while self.hand_sum < 17:
self.draw()
self.sum_hand()
class Game:
def __init__(self):
# starts the game
print("\aInstructions:\n\n\t for play type yes\n\tfor cancel press ctrl + c\n\tenjoy the game casul\n\n\n")
x = input("Would you like to play?\n").lower()
if x == "yes":
shuffle()
player = Hand()
dealer = Dealer()
player.hit_or_stay(dealer)
else:
pass
if __name__ == '__main__':
game = Game()