-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBLACK-JACK
152 lines (113 loc) · 3.06 KB
/
BLACK-JACK
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
import random, os, sys
cardName = { 1: 'Ace', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', 11: 'Jack', 12: 'Queen', 13: 'King' }
cardSuit = { 'c': 'Clubs', 'h': 'Hearts', 's': 'Spades', 'd': 'Diamonds' }
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return(cardName[self.rank]+" Of "+cardSuit[self.suit])
def getRank(self):
return(self.rank)
def getSuit(self):
return(self.suit)
def BJValue(self):
if self.rank > 9:
return(10)
else:
return(self.rank)
def showHand(hand):
for card in hand:
print(card)
def showCount(hand):
print("Count: "+str(handCount(hand)))
def handCount(hand):
handCount=0
for card in hand:
handCount += card.BJValue()
return(handCount)
def gameEnd(score):
print("Blackjack! *Final Score* Computer: "+str(score['computer'])+" You: "+str(score['human']))
sys.exit(0)
deck = []
suits = [ 'c','h','d','s' ]
score = { 'computer': 0, 'human': 0 }
hand = { 'computer': [],'human': [] }
for suit in suits:
for rank in range(1,14):
deck.append(Card(rank,suit))
keepPlaying = True
while keepPlaying:
random.shuffle(deck)
random.shuffle(deck)
random.shuffle(deck)
#Deal Cards
hand['human'].append(deck.pop(0))
hand['computer'].append(deck.pop(0))
hand['human'].append(deck.pop(0))
hand['computer'].append(deck.pop(0))
playHuman = True
bustedHuman = False
while playHuman:
os.system('clear')
print("Blackjack! Computer: "+str(score['computer'])+" You: "+str(score['human']))
print()
print('Computer Shows: '+ str(hand['computer'][-1]))
print()
print('Your Hand:')
showHand(hand['human'])
showCount(hand['human'])
print()
inputCycle = True
userInput = ''
while inputCycle:
userInput = input("(H)it, (S)tand, or (Q)uit: ").upper()
if userInput == 'H' or 'S' or 'Q':
inputCycle = False
if userInput == 'H':
hand['human'].append(deck.pop(0))
if handCount(hand['human']) > 21:
playHuman = False
bustedHuman = True
elif userInput == 'S':
playHuman = False
else:
gameEnd(score)
playComputer = True
bustedComputer = False
while not bustedHuman and playComputer:
print(handCount(hand['computer']))
if handCount(hand['computer'])<17:
hand['computer'].append(deck.pop(0))
else:
playComputer = False
if handCount(hand['computer'])>21:
playComputer = False
bustedComputer = True
if bustedHuman:
print('Player Busted')
score['computer'] += 1
elif bustedComputer:
print('Computer Busted')
score['human'] += 1
elif handCount(hand['human']) > handCount(hand['computer']):
print('Player Wins')
score['human'] += 1
else:
print('Computer Wins')
score['computer'] += 1
print()
print('Computer Hand:')
showHand(hand['computer'])
showCount(hand['computer'])
print()
print('Player Hand:')
showHand(hand['human'])
showCount(hand['human'])
print()
if input("(Q)uit or enter for next round").upper() == 'Q':
gameEnd(score)
deck.extend(hand['computer'])
deck.extend(hand['human'])
del hand['computer'][:]
del hand['human'][:]