FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-projects/caesercipher/caesarCipherAlgorithm.py at master · utsav507/python-projects · GitHub
utsav507
/
python-projects
Public
Notifications
You must be signed in to change notification settings
Fork
18
Star
26
Code
Issues
1
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-projects
/
caesercipher
/
caesarCipherAlgorithm.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
353 lines (250 loc) · 7.27 KB
Breadcrumbs
python-projects
/
caesercipher
/
caesarCipherAlgorithm.py
Copy path
File metadata and controls
353 lines (250 loc) · 7.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/Users/Utsav/downloads/udemy python
# For using the same code in either Python 2 or 3
# from __future__ import print_function
""" milestone_project_1.py: Tic Tac Toe Game """
__author__
=
"Utsav Shah"
__copyright__
=
"Copyright 2017, The Austin Side Project"
__credits__
=
[
"Hustle"
]
__license__
=
"UTS"
__version__
=
"1.1.0"
__maintainer__
=
"Utsav Shah"
__email__
=
"utsavshah507@gmail.com"
__status__
=
"Productive"
## Milestone Project 2
# Importing libraries -- used for shuffling cards
import
random
# Boolean type to know whether play is in hand
playing
=
False
# Amount for buy-in
chip_pool
=
100
# raw_input('Enter the amount for buy-in: ')
print
'Your buy-in amount is: '
,
chip_pool
bet
=
1
restart_phrase
=
"Press d to deal the cards again, or press q to quit."
# Hearts, Diamonds, Clubs, Spades
suits
=
(
'H'
,
'D'
,
'S'
,
'C'
)
# Possible Card Ranks
ranking
=
(
'A'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'10'
,
'J'
,
'Q'
,
'K'
)
# Point Val Dict (Dual existence of Ace is defined later)
card_val
=
{
'A'
:
1
,
'2'
:
2
,
'3'
:
3
,
'4'
:
4
,
'5'
:
5
,
'6'
:
6
,
'7'
:
7
,
'8'
:
8
,
'9'
:
9
,
'10'
:
10
,
'J'
:
10
,
'Q'
:
10
,
'K'
:
10
}
# Creating Card Class
class
Card
:
def
__init__
(
self
,
suit
,
rank
):
self
.
suit
=
suit
self
.
rank
=
rank
def
__str__
(
self
):
return
self
.
suit
+
self
.
rank
def
grab_suit
(
self
):
return
self
.
suit
def
grab_rank
(
rank
):
return
self
.
rank
def
draw
(
self
):
print
(
self
.
suit
+
self
.
rank
)
# Creating Hand Class
# Gives dual existence to Ace
class
Hand
:
def
__init__
(
self
):
self
.
cards
=
[]
self
.
value
=
0
# Aces can be 1 0r 11 as defined below
self
.
ace
=
False
def
__str__
(
self
):
'''Return a string of current hand composition'''
hand_comp
=
""
# List Comprehension
for
card
in
self
.
cards
:
card_name
=
card
.
__str__
()
hand_comp
+=
" "
+
card_name
return
'The hand has %s'
%
hand_comp
def
card_add
(
self
,
card
):
'''Add another card to the hand'''
self
.
cards
.
append
(
card
)
# Checking for Aces
if
card
.
rank
==
'A'
:
self
.
ace
=
True
self
.
value
+=
card_val
[
card
.
rank
]
def
calc_val
(
self
):
'''Calculating value of hand, making aces = 1 if they don't bust the hand'''
if
(
self
.
ace
==
True
and
self
.
value
<
12
):
return
self
.
value
+
10
else
:
return
self
.
value
def
draw
(
self
,
hidden
):
if
hidden
==
True
and
playing
==
True
:
# Don't show first hidden card
starting_card
=
1
else
:
starting_card
=
0
for
x
in
range
(
starting_card
,
len
(
self
.
cards
)):
self
.
cards
[
x
].
draw
()
# Creating Class Deck
class
Deck
:
def
__init__
(
self
):
'''Creating a deck in order'''
self
.
deck
=
[]
for
suit
in
suits
:
for
rank
in
ranking
:
self
.
deck
.
append
(
Card
(
suit
,
rank
))
def
shuffle
(
self
):
'''Shuffles the deck, using python's built-in random library'''
random
.
shuffle
(
self
.
deck
)
def
deal
(
self
):
'''Grabbing the first item in the deck'''
single_card
=
self
.
deck
.
pop
()
return
single_card
def
__str__
(
self
):
deck_comp
=
" "
for
card
in
self
.
cards
:
deck_comp
+=
" "
+
deck_comp
.
__str__
()
return
"The deck has "
+
deck_comp
# End of Classes
# First Bet
def
make_bet
():
'''Ask the player for the bet amount and '''
global
bet
bet
=
0
print
'What amount of chips would you like to bet? (Please enter whole integer) '
# While loop to keep asking for the bet
while
bet
==
0
:
# Using bet_comp as a checker
bet_comp
=
raw_input
()
bet_comp
=
int
(
bet_comp
)
# Check to make sure the bet is within the remaining amount of chips left
if
bet_comp
>=
1
and
bet_comp
<=
chip_pool
:
bet
=
bet_comp
else
:
print
"Invalid bet, you only have "
+
str
(
chip_pool
)
+
" remaining"
def
deal_cards
():
'''This function deals out cards and sets up round'''
# Set up all global variables
global
result
,
playing
,
deck
,
player_hand
,
dealer_hand
,
chip_pool
,
bet
# Creating a deck
deck
=
Deck
()
# Shuffle it
deck
.
shuffle
()
# Set up the bet
make_bet
()
# Set up both player and dealer hands
player_hand
=
Hand
()
dealer_hand
=
Hand
()
# Deal out initial cards
player_hand
.
card_add
(
deck
.
deal
())
player_hand
.
card_add
(
deck
.
deal
())
result
=
"Hit or Stand? Press h for hit or s for stand: "
if
playing
==
True
:
print
'Fold, Sorry'
chip_pool
-=
bet
# Set up to know currently playing hand
playing
=
True
game_step
()
# Hit Function
def
hit
():
'''Implementing the hit button'''
global
playing
,
chip_pool
,
deck
,
player_hand
,
dealer_hand
,
result
,
bet
# If hand is in play add card
if
playing
:
if
player_hand
.
calc_val
()
<=
21
:
player_hand
.
card_add
(
deck
.
deal
())
print
"Player hand is %s"
%
player_hand
if
player_hand
.
calc_val
()
>
21
:
result
=
'Busted!'
+
restart_phrase
chip_pool
-=
bet
playing
=
False
else
:
result
=
"Sorry, can't hit"
+
restart_phrase
game_step
()
# Stand Function
def
stand
():
global
playing
,
chip_pool
,
deck
,
player_hand
,
dealer_hand
,
result
,
bet
'''This function plays the dealers hand, since stand was chosen'''
if
playing
==
False
:
if
player_hand
.
calc_val
()
>
0
:
result
=
"Sorry, you can't stand!"
# Going through all other possible options
else
:
# Sfot 17 Rule
while
dealer_hand
.
calc_val
()
<
17
:
dealer_hand
.
card_add
(
deck
.
deal
())
# Dealer Busts
if
dealer_hand
.
calc_val
()
>
21
:
result
=
'Dealer busts! You win! '
+
restart_phrase
chip_pool
+=
bet
playing
=
False
# Player has better hand than dealer
elif
dealer_hand
.
calc_val
()
<
player_hand
.
calc_val
():
result
=
'You beat the dealer, you win! '
+
restart_phrase
chip_pool
+=
bet
playing
=
False
# Push
elif
dealer_hand
.
calc_val
()
==
player_hand
.
calc_val
():
result
=
'Tied up, push!'
+
restart_phrase
playing
=
False
# Dealer beats player
else
:
result
=
'Dealer Wins! '
+
restart_phrase
chip_pool
-=
bet
playing
=
False
game_step
()
# Function to print results and ask user for next step
def
game_step
():
'''Function to print game step/status on output'''
# Display Player Hand
print
""
print
(
'Player Hand is: '
),
player_hand
.
draw
(
hidden
=
False
)
print
''
print
'Player hand total is: '
+
str
(
player_hand
.
calc_val
())
# Display Dealer Hand
print
''
print
(
'Dealer Hand is: '
),
dealer_hand
.
draw
(
hidden
=
True
)
# If game round is over
if
playing
==
False
:
print
" --- for a total of "
+
str
(
dealer_hand
.
calc_val
())
print
"Chip Total: "
+
str
(
chip_pool
)
# Otherwise, don't know the second card yet
else
:
print
" with another card hidden upside down"
# Print result of hit or stand
print
''
print
result
player_input
()
# Function to exit the game
def
game_exit
():
print
'Thanks for playing!'
exit
()
# Function to read user input
def
player_input
():
'''Read user input, lower case it jsuts to be safe'''
plin
=
raw_input
().
lower
()
if
plin
==
'h'
:
hit
()
elif
plin
==
's'
:
stand
()
elif
plin
==
'd'
:
deal_cards
()
elif
plin
==
'q'
:
game_exit
()
else
:
print
"Invalid Input. Enter h, s, d, or q: "
player_input
()
# Intro to game
def
intro
():
statement
=
'''Welcome to BlackJack! Get as close to 21 as you can without getting over!
Dealer hits until she reaches 17. Aces count as 1 or 11. Card output goes a letter followed by a number of face notation. '''
print
statement
print
''
# Playing the Game
'''The following code will initiate the game!
(Note: Need to Run a 11 Cells)'''
# Create a Deck
deck
=
Deck
()
# Shuffle it
deck
.
shuffle
()
# Create player and dealer hands
print
''
player_hand
=
Hand
()
print
''
deal_hand
=
Hand
()
# Print the intro
intro
()
# Deal out the cards and start the game!
deal_cards
()
Back
|
FazBrowse Home
|
New Git URL