-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjack Simulation.py
263 lines (263 loc) · 9.04 KB
/
Blackjack Simulation.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"import random\n",
"class Card:\n",
" \n",
" def __init__(self,suit,rank):\n",
" self.suit = suit\n",
" self.rank = rank\n",
" self.value = values[rank]\n",
" \n",
" def __str__(self):\n",
" return self.rank + ' of ' + self.suit\n",
" \n",
"class Deck:\n",
" \n",
" def __init__(self):\n",
" # Note this only happens once upon creation of a new Deck\n",
" self.all_cards = [] \n",
" for suit in suits:\n",
" for rank in ranks:\n",
" # This assumes the Card class has already been defined!\n",
" self.all_cards.append(Card(suit,rank))\n",
" \n",
" def shuffle(self):\n",
" # Note this doesn't return anything\n",
" random.shuffle(self.all_cards)\n",
" \n",
" def deal_one(self):\n",
" # Note we remove one card from the list of all_cards\n",
" return self.all_cards.pop()\n",
" \n",
"class Dealer():\n",
" \n",
" def __init__(self):\n",
" self.name = \"Dealer\"\n",
" self.all_cards = []\n",
" \n",
" def add_card(self,new_card):\n",
" self.all_cards.append(new_card)\n",
" \n",
" def reset_cards(self):\n",
" self.all_cards = []\n",
" \n",
"class Player():\n",
" \n",
" def __init__(self,):\n",
" self.name = \"Player\"\n",
" self.balance = 100\n",
" self.all_cards = []\n",
" \n",
" def add_card(self,new_card):\n",
" self.all_cards.append(new_card)\n",
" \n",
" def reset_cards(self):\n",
" self.all_cards = []\n",
" \n",
" def bet(self,wager):\n",
" self.balance -= wager\n",
" \n",
" def win(self,wager):\n",
" self.balance += (2*wager)\n",
" \n",
" def push(self,wager):\n",
" self.balance += wager\n",
"\n",
"def hand_value(player):\n",
" total = 0\n",
" #checks for Ace\n",
" for card in player.all_cards:\n",
" total += card.value\n",
" \n",
" if total > 21:\n",
" for card in player.all_cards:\n",
" if card.rank == 'Ace':\n",
" total -= 10\n",
" if total < 21:\n",
" break\n",
" \n",
" return total\n",
"\n",
"def hit(player,deck):\n",
" print(f\"You have the: \")\n",
" for card in player.all_cards:\n",
" print(card)\n",
" \n",
" #Checks to see if the player has bust\n",
" if hand_value(player) > 21:\n",
" return False\n",
" \n",
" else:\n",
" \n",
" turn = ' '\n",
" while turn != 'y' and turn != 'n': #Asks until the player enters y or n\n",
" turn = input(\"Would you like to hit? \").lower()\n",
" \n",
" #If the player has not bust, it will ask the player if they want to hit again\n",
" \n",
" if turn == 'y': #Adds a card to the deck, then recurses\n",
" player.add_card(deck.deal_one())\n",
" return hit(player,deck)\n",
" \n",
" else:\n",
" return True\n",
" \n",
"\n",
"def dealer_operation(player,dealer,deck):\n",
" if hand_value(dealer) > hand_value(player):\n",
" if hand_value(dealer) <= 21:\n",
" print(f\"Dealer stands at {hand_value(dealer)}.\")\n",
" return True\n",
" else:\n",
" print(\"Dealer busts!\")\n",
" return False\n",
" else:\n",
" dealer.add_card(deck.deal_one())\n",
" print(f\"Dealer hits...{dealer.all_cards[-1]}\")\n",
" return dealer_operation(player,dealer,deck)\n",
"\n",
"def reset_game(player,dealer,deck):\n",
" player.reset_cards()\n",
" dealer.reset_cards()\n",
" deck = Deck()\n",
" deck.shuffle()\n",
" \n",
" \n",
"suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
"ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
"values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, \n",
" 'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}\n",
"\n",
"dealer = Dealer()\n",
"player = Player()\n",
"deck = Deck()\n",
"deck.shuffle()\n",
"\n",
"game_on = True\n",
"\n",
"print(\"Welcome to Blackjack!\")\n",
"while game_on:\n",
" \n",
" #Checks before hand if the player is bust\n",
" if player.balance <= 0:\n",
" print(\"You're broke!\")\n",
" answer = ' '\n",
" \n",
" #Prompts player to play again until they enter y or n\n",
" while answer != 'y' and answer != 'n':\n",
" answer = input(\"Play again?(Y/N) \").lower()\n",
" \n",
" if answer == 'y': #Resets entire game (including player.balance)\n",
" dealer = Dealer()\n",
" player = Player()\n",
" deck = Deck()\n",
" deck.shuffle()\n",
"\n",
" game_on = True\n",
" \n",
" else:\n",
" game_on = False\n",
" break\n",
" \n",
" #Player places bet \n",
" wager = int(input(\"Please place your bet: \")) \n",
" while wager > player.balance:\n",
" wager = int(input(\"Bet cannot exceed your current balance.\"+\"\\n\"\n",
" +\"Please place a lower bet: \"))\n",
" player.bet(wager)\n",
" \n",
" print(\"Dealing cards...\"+\"\\n\")\n",
" \n",
" for i in range(2):\n",
" player.add_card(deck.deal_one())\n",
" for i in range(2):\n",
" dealer.add_card(deck.deal_one())\n",
" \n",
" print(f\"Dealer shows {dealer.all_cards[0]}.\"+\"\\n\")\n",
" \n",
" play_again = ' '\n",
" \n",
" if hit(player,deck):\n",
" print(f\"Dealer reveals {dealer.all_cards[0]} and {dealer.all_cards[1]}.\"+\"\\n\")\n",
" \n",
" if dealer_operation(player,dealer,deck) and hand_value(player) != hand_value(dealer):\n",
" while play_again != 'y' and play_again != 'n':\n",
" play_again = input(\"Dealer wins!\"+\"\\n\"+\n",
" \"Would you like to play another hand? \").lower()\n",
" if play_again == 'n':\n",
" game_on = False\n",
" break\n",
" else: \n",
" reset_game(player,dealer,deck)\n",
" \n",
" elif hand_value(player) == hand_value(dealer):\n",
" while play_again != 'y' and play_again != 'n':\n",
" play_again = input(\"Push!\"+\"\\n\"+\n",
" \"Would you like to play another hand? \").lower()\n",
" if play_again == 'n':\n",
" game_on = False\n",
" break\n",
" else:\n",
" player.push(wager)\n",
" reset_game(player,dealer,deck)\n",
" \n",
" else:\n",
" player.win(wager)\n",
" while play_again != 'y' and play_again != 'n':\n",
" play_again = input(\"You win!\"+\"\\n\"+\n",
" \"Would you like to play another hand? \").lower()\n",
" if play_again == 'n':\n",
" game_on = False\n",
" break\n",
" else: \n",
" reset_game(player,dealer,deck)\n",
" \n",
" else:\n",
" while play_again != 'y' and play_again != 'n':\n",
" play_again = input(\"You bust!\"+\"\\n\"+\n",
" \"Would you like to play another hand? \").lower()\n",
" if play_again == 'n':\n",
" game_on = False\n",
" break\n",
" else: \n",
" reset_game(player,dealer,deck)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}