forked from cowgoat88/blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack_main.py
156 lines (114 loc) · 3.63 KB
/
blackjack_main.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
#BLACKJACK
import random
import itertools
#user input - new game / cash out (bitch)
#new hand instance
#initiate(and shuffle) deck. (deck can be a sequential list of cards that
#get chosen with random.choice())
class Card(object):
suits = ['Spades','Hearts','Diamonds','Clubs']
values = range(1,11)+['JACK','QUEEN','KING','ACE']
def __init__(self, suit, value):
self.suit = suit
self.value = value
def __str__(self):
return "%s of %s" % (Card.values[self.value],Card.suits[self.suit])
class Deck(object):
def __init__(self):
# initialize list of cards for a Deck
self.cards = []
for suit in range(4):
for value in range(1,13):
card = Card(suit,value)
self.cards.append(card)
def __str__(self):
#makes human readable string
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
def shuffle(self):
#shuffles deck
random.shuffle(self.cards)
class Player(object):
'''
player attributes:
money
cards
score
'''
def __init__(self):
self.score = 0
self.money = 200
self.cards = []
def addScore(self):
self.score = 0
for card in self.cards:
if card.value <= 10: #for number cards, add one
self.score += card.value
self.score += 1
elif card.value == '14': #ACE
self.score += 11
elif card.value in [11,12,13]: #JACK QUEEN KING
self.score += 10
def displayHand(self):
for card in self.cards:
print str(card), "/ ",
print "\nScore - ", self.score
def displayTable(uName,dealer):
uName.addScore()
dealer.addScore()
print "Your hand: "
uName.displayHand()
print "Dealer shows:"
print str(dealer.cards[0])
def playGame(uName, dealer):
deck = Deck()
deck.shuffle()
uName.cards = [deck.cards.pop(0),deck.cards.pop(1)]
dealer.cards = [deck.cards.pop(0),deck.cards.pop(1)]
bet = input("BET: ")
while True:
displayTable(uName,dealer)
hit = raw_input("'h' to hit, 's' to stay: ")
if hit == 'h':
uName.cards.append(deck.cards.pop(0))
uName.addScore()
if uName.score > 21:
print "BUST!"
break
elif hit == 's':
break
while True:
if dealer.score < 17:
print "Dealer hits, ",
dealer.cards.append(deck.cards.pop(0))
elif dealer.score >=17:
break
dealer.addScore()
print "\nDealer shows: "
dealer.displayHand()
print "Final: "
print "You: ", uName.score
print "Dealer: ", dealer.score
if dealer.score >= uName.score and dealer.score <=21 or uName.score > 21:
print "You Lose!"
uName.money = uName.money - bet
else:
print "You Win!"
uName.money = uName.money + bet
print "$", uName.money
if uName.money < 0:
print "Broke! Go home."
#compare hands
def sitAtTable():
uName = Player()
dealer = Player()
while True:
play = raw_input("What will it be? 'play' or 'cash out': ")
if play == 'play':
playGame(uName, dealer)
else:
print 'BITCH...'
break
sitAtTable()