-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
199 lines (150 loc) · 5.56 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
"""
This is a game of Blackjack. There are two players - a computer dealer and a human player.
There is a full deck of 52 cards.
The Human player has a bankroll with money in it. From that bank, the human places a bet.
The player starts with 2 cards face-up. Dealer starts with 1 face up and 1 face down.
Player goal is to get closer to 21 than the dealer without going over.
Player has two options - Hit (receive another card) and Stay (stop receiving cards).
Once player has finished, computer's turn. If player is still under 21, dealer hits until they beat the player or bust.
Ways game can end:
1. Player hits and goes over 21, dealer gets the money
2. Computer beats the player - higher total than player but under 21, dealer gets the money
3. Player wins after computer done - If player is under 21 and computer dealer busts, human doubles their money
Rules:
1. Face cards count as value of 10
2. Aces can be either 1 or 11, whichever is preferable to the player
"""
import random
# Define card attributes and values
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8,
'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
chip_colors = {1: 'Red', 5: 'Blue', 10: 'Green', 20: 'Black', 50: 'White'}
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
self.value = values[rank]
self.face_up = True
def __str__(self):
if self.face_up:
return f'The {self.rank} of {self.suit}.'
else:
pass
def flip(self):
if self.face_up:
self.face_up = False
else:
self.face_up = True
class Deck:
def __init__(self):
self.all_cards = []
for suit in suits:
for rank in ranks:
self.all_cards.append(Card(suit, rank))
def remove_card(self):
return self.all_cards.pop(0)
def shuffle(self):
return random.shuffle(self.all_cards)
class Chip:
def __init__(self, value):
self.value = value
self.color = chip_colors[value]
def __str__(self):
return f'This chip is {self.color} with a value of {self.value}'
class Hand:
def __init__(self):
self.cards = []
def __str__(self):
return f'The current number of cards in your hand are: {len(self.cards)}'
def print_hand(self):
str_hand = []
for card in self.cards:
str_hand.append(str(card))
return str_hand
def value(self):
# Find total points value for hand
total = 0
has_ace = False
for card in self.cards:
if card.rank == 'Ace': # Determine if player has Ace in their hand
if total + 11 > 21:
total += 1
has_ace = True
else:
total += 11
has_ace = True
else:
total += card.value
if total > 21 and has_ace:
return total - 10
else:
return total
class Player:
# Player has bankroll and hand, and bankroll has a total value equal to adding up all chips
def __init__(self):
self.bankroll = []
self.total_cash = len(self.bankroll)
self.hand = Hand()
# Give the player 20 chips to play with
while self.total_cash < 20:
self.bankroll.append(Chip(1))
def __str__(self):
return f'Money remaining: {self.total_cash}'
def deal_cards(self, cards):
if type(cards) == type([]):
self.hand.extend(cards)
else:
self.hand.append(cards)
def bet_chip(self):
return self.bankroll.pop(0)
class Pot:
def __init__(self):
self.chips = []
total = 0
for i in chips:
total += 1
self.value = total
def __str__(self):
return f'The value of the pot is: {self.value}'
def bet(self, number):
for i in range(number):
self.chips.append(player.bet_chip())
# Set up game
game_on = True
main_deck = Deck()
main_deck.shuffle()
player = Player()
dealer = Player()
pot = Pot()
def deal_cards():
# Deal two face-up cards to player
player.deal_cards([main_deck.remove_card(), main_deck.remove_card()])
# Deal one face-up and one face-down card to dealer
dealer.deal_cards([main_deck.remove_card(), main_deck.remove_card().flip])
# Print the cards on the field for the player
print(f'The dealer is showing {dealer.hand.cards[0]}')
print(f'You have {player.hand.print_hand()}')
def player_turn():
# Player decides to hit or stay
action = input("Would you like to hit or stay? ")
if action.lower() not in ['hit', 'stay']:
print("Invalid input. Please type either 'hit' or 'stay'")
else:
return action
while game_on:
if player.total_cash > 0: # Make sure a player has enough money to bet
bet = int(input("Please enter how many chips you would like to bet: "))
if bet < player.total_cash:
pot.bet(bet)
else:
print("You don't have enough chips for that bet! Please only bet money you have.")
while player.hand.value() < 21:
deal_cards()
if player_turn() == 'stay':
print(f'You stay. Hand value: {player.hand.value()}')
break
else:
print('Player is out of money! Game over.')
game_on = False