-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (158 loc) · 5.81 KB
/
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# oop blackjack
## stretch goals ##
# add a money/betting system
# ui
import random
from random import shuffle
# initializing constants as functions
RANKS = { "Ace":1, "2":2, "3":3, "4":4, "5":5, "6":6,
"7":7, "8":8, "9":9, "10":10, "Jack":10, "Queen":10, "King":10 }
SUITS = ("Clubs", "Diamonds", "Hearts", "Spades")
class Card(object):
# Each card made with its own rank and suite
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def getRank(self):
return self.rank
def getSuite(self):
return self.suit
def show(self):
print(f"{self.rank} of {self.suit}")
class Deck(object):
# every card is put into a deck and shuffled
def __init__(self):
self.cards = [Card(rank, suit) for rank in RANKS for suit in SUITS]
random.shuffle(self.cards)
def show(self):
for c in self.cards:
c.show()
# takes a card off the top of the deck
def drawCard(self):
return self.cards.pop()
def shuffle(self):
random.shuffle(self.cards)
class Hand(object):
# The cards from the deck are pulled from and put into the dealer/players hand
def __init__(self):
self.hand = []
# iterates through cards and adds the rank of each card to variable "value"
def getValue(self):
self.value = 0
for card in self.hand:
self.value += RANKS[card.getRank()]
# TODO test if ACE really does what it's supposed to do
if str(card.getRank()) == "Ace":
if self.value <= 11:
self.value += 10
return self.value
def showValue(self):
self.shownVal = self.getValue()
print(f"Card value totals: {self.shownVal}")
def showHand(self):
for card in self.hand:
card.show()
# appends the popped card to the player/dealers hand
def draw(self, deck):
self.hand.append(deck.drawCard())
return self
def bust(self):
if self.getValue() > 21:
return True
# initializing objects
deck = Deck()
player = Hand()
dealer = Hand()
def main():
gameIsPlaying = True
# welcome the player
print("Welcome to BlackJack")
## explain basic rules if they dont know how to play
tutorial = input("Do you know how to play BlackJack?: (Yes/No)\n")
if tutorial.lower() == 'no':
print("The goal of the game is to reach a score (=sum of the cards) as high as possible but not more than 21. \n A Blackjack (Ace and a card whose value is 10) beats all other combination of cards.\n If the final sum is higher than the sum of the dealer, the player wins.")
print()
print()
turn = player
# drawing 2 initial cards for the player
# 1 for the dealer
player.draw(deck).draw(deck)
dealer.draw(deck)
# showing the full hands to the player
print("Your full hand is:")
player.showHand()
player.getValue()
player.showValue()
print()
# pretending a card is actually drawn and hidden to reduce computations
print("The dealers hand is:")
dealer.showHand()
print("[Hidden Card]")
dealer.getValue()
print("Total card value unknown")
dealer.draw(deck)
while gameIsPlaying:
# player decides whether to stand or hit based off of the total of their current 2 cards
# TODO might be able to break this up into 2 classes to shorten main()
if turn == player:
print()
print("What Would you like to do:")
print("Hit")
print("Stay")
print()
choice = input()
print()
if choice.lower() == "hit":
player.draw(deck)
if player.bust():
print("YOU'RE BUST")
gameIsPlaying = False
else:
turn = dealer # TODO move this
print("Your full hand is:")
player.showHand()
player.getValue()
player.showValue()
# dealer ai
# dealer will draw a card if their total value is under 13
if turn == dealer: # TODO possibly change this to else: to reduce variables
print()
print("The dealer will now play")
print()
while dealer.getValue() < 17:
if dealer.getValue() < 11:
dealer.draw(deck)
if dealer.bust():
print("DEALER BUST")
gameIsPlaying = False
else:
# dealer will randomly pull a new card anyway (> 13) so it's not boring
wildCard = random.randint(0, 1)
if wildCard == 1:
dealer.draw(deck)
if dealer.bust():
print("DEALER BUST")
gameIsPlaying = False
print("The Dealers hand is:")
dealer.showHand()
dealer.getValue()
dealer.showValue()
gameIsPlaying = False
print()
print("Game has ended")
print()
endPl = player.getValue()
endDl = dealer.getValue()
# displays the winner to the user
if endPl > endDl and not player.bust():
print(f"The player has won with {endPl} points over the dealers {endDl} points!")
elif endPl < endDl and not dealer.bust():
print(f"The dealer has won with {endDl} points over the players {endPl} points!")
elif dealer.bust():
print(f"The dealer is Bust and the player wins!")
elif endPl == endDl and not dealer.bust() or not player.bust():
print(f"It's a tie at {endPl} points.")
else:
print(f"The player is Bust and loses with {endPl} points.")
if __name__ == "__main__":
main()