-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.py
94 lines (79 loc) · 2.52 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
#/usr/bin/python
# -*- coding: utf-8 -*-
"""
author: Aisha Malik
Blackjack game
"""
import random
class Card(object):
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return "{}{}".format(self.suit, self.rank)
def value(self, total):
cval = 0
if type(self.rank) == int:
cval = self.rank
if self.rank == 'J' or self.rank == 'Q' or self.rank == 'K':
cval = 10
if self.rank == 'A':
if total + 11 > 21:
cval = 1
else:
cval = 11
return cval
def make_deck():
listy = []
suits = ['♠','♣','♦','♥']
for i in suits:
for j in range(2,11):
newcard = Card(i, j)
listy.append(newcard)
listy.append(Card(i, 'A'))
listy.append(Card(i, 'J'))
listy.append(Card(i, 'Q'))
listy.append(Card(i, 'K'))
random.shuffle(listy)
return listy
make_deck()
def main():
deck = make_deck()
ptotal = 0 #player total
dtotal = 0 #dealer total
playersturn = True
while ptotal < 21 and playersturn:
pcard = deck[0] #pcard = player card
ptotal += pcard.value(ptotal)
print("You drew {}".format(pcard))
print("Your sum: {}".format(ptotal))
if ptotal == 21:
print("Yay! You're total is 21. You win! :)")
elif ptotal > 21:
print("Sorry, you lose. Your sum is greater than 21. :(")
else:
player = input("Would you like to draw another card? (y/n) ")
if player.lower() == 'n':
playersturn = False
deck = deck[1:]
if ptotal < 21 and playersturn == False:
print("Now it's my turn.")
while dtotal < 17:
dcard = deck[0]
dtotal += dcard.value(dtotal)
print("I drew {}".format(dcard))
print("My sum: {}".format(dtotal))
deck = deck[1:]
if dtotal == 21:
print("I won! My sum is 21! Take that!")
elif dtotal > 21:
print("I guess you win. My total is greater than 21 :(")
elif dtotal == player:
print("The game is a push. It's a draw.")
else:
if dtotal > ptotal:
print("I win because my total is greater than yours! Huzzah!")
else:
print("You win! Congratulations!")
if __name__ == "__main__":
main()